jankustosz Temat założony przez niniejszego użytkownika |
To jest cały kod jeżeli chcesz. Ja pokazałem tylko to w czym jest problem. » 2015-06-08 19:39:29 Jak co to dopiero się uczę, wczoraj przerobiłem kontrolki. Nie mówcie mi o tym że coś mogłoby się inaczej nazywać. Piszcie tylko jeżeli wiecie dlaczego metoda UstawNaWszystkieElementy() nie działa jak trzeba. PS Korzystam z visual studio. =======================================WinApi.h======================================= #pragma once #include <windows.h> #include <string> #include <Windowsx.h> #include <vector>
#define Wmain() int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) #define WinApiNULL Element(false)
using std::string; using std::vector;
class Element { WNDCLASSEX wc; HWND el; public: void operator =( Element & ele ); bool operator !(); bool operator ==( bool jak ); bool operator ==( LPARAM lParm ); friend bool operator ==( LPARAM lParm, Element & ele ); friend int komunikat( Element & okno, LPCSTR text, LPCSTR title, UINT style ); Element( bool dodac = true ); void wyswietl(); void StworzWinClass( HINSTANCE hInstance, LPCSTR nazwa_klasy, WNDPROC winproc, UINT styl, HICON ikona_okien, HICON mala_ikona, HCURSOR kursor, HBRUSH kolor, LPCTSTR menu = 0 ); void StworzObjekt( DWORD rozszerzony_styl, LPCSTR nazwa_klasy, LPCSTR title, DWORD podstawowy_styl, INT szerokosc, INT wysokosc, HINSTANCE hInstance, INT x = CW_USEDEFAULT, INT y = CW_USEDEFAULT, Element & glowne_okno = WinApiNULL, HMENU menu = 0, LPVOID dodatkowe_parametry = 0 ); void SetText( string s ); string getText(); void wyslij_message( UINT rodzaj, WPARAM wParam, char * lParam ); void CheckBoxZmienStan( bool tryb ); bool CheckBoxStan(); int ComboBoxWybrano(); string ComboBoxGetText(); int ComboBoxGetLenght(); void RadioButtonZaznacz( int odkad, int dokad, int wybierz ); };
class Msg { MSG Komunikat; public: BOOL pobiez(); void wyslij(); WPARAM returnuj(); };
class Font { HFONT font; public: Font(); void UstawNaElement( Element & ele ); void UstawNaWszystkieElementy(); };
=======================================WinApi.cpp======================================= #include "WinApi.h" #include <string> #include <Windowsx.h> #include <vector> #include <sstream>
using std::string; using std::vector; using std::stringstream;
vector < Element *> elementy;
int komunikat( Element & okno, LPCSTR text, LPCSTR title, UINT style ) { return MessageBox( okno.el, text, title, style ); }
void Element::operator =( Element & ele ) { el = ele.el; }
bool Element::operator !() { return !el; } bool Element::operator ==( bool jak ) { return !el == !jak; } bool Element::operator ==( LPARAM lParm ) { return( el ==( HWND ) lParm ); }
bool operator ==( LPARAM lParm, Element & ele ) { return(( HWND ) lParm == ele.el ); }
Element::Element( bool dodac ) { el = NULL; if( dodac ) { elementy.push_back( this ); string s; stringstream ss; ss << elementy.size(); ss >> s; komunikat( WinApiNULL, s.c_str(), "dodaje", MB_OK | MB_ICONINFORMATION ); } }
void Element::StworzWinClass( HINSTANCE hInstance, LPCSTR nazwa_klasy, WNDPROC winproc, UINT styl, HICON ikona_okien, HICON mala_ikona, HCURSOR kursor, HBRUSH kolor, LPCTSTR menu ) { wc.hInstance = hInstance; wc.lpszClassName = nazwa_klasy; wc.lpfnWndProc = winproc; wc.style = styl; wc.cbSize = sizeof( WNDCLASSEX ); wc.hIcon = ikona_okien; wc.hIconSm = mala_ikona; wc.hCursor = kursor; wc.lpszMenuName = menu; wc.hbrBackground = kolor; wc.cbWndExtra = 0; wc.cbClsExtra = 0; if( !RegisterClassEx( & wc ) ) { komunikat( WinApiNULL, "Nieprawidłowa klasa okna!", "Problem!", MB_OK | MB_ICONERROR ); exit( 1 ); } }
void Element::StworzObjekt( DWORD rozszerzony_styl, LPCSTR nazwa_klasy, LPCSTR title, DWORD podstawowy_styl, INT szerokosc, INT wysokosc, HINSTANCE hInstance, INT x, INT y, Element & glowne_okno, HMENU menu, LPVOID dodatkowe_parametry ) { el = CreateWindowEx( rozszerzony_styl, nazwa_klasy, title, podstawowy_styl, x, y, szerokosc, wysokosc, glowne_okno.el, menu, hInstance, dodatkowe_parametry ); if( el == NULL ) { string tmp = title; komunikat( WinApiNULL,( "Objekt <" + tmp + "> odmówił przyjścia na świat!" ).c_str(), "Problem!", MB_OK | MB_ICONERROR ); exit( 1 ); } } void Element::wyswietl() { ShowWindow( el, SW_SHOW ); UpdateWindow( el ); }
void Element::SetText( string s ) { SetWindowText( el, s.c_str() ); }
string Element::getText() { DWORD dlugosc = GetWindowTextLength( el ); LPSTR Bufor =( LPSTR ) GlobalAlloc( GPTR, dlugosc + 1 ); GetWindowText( el, Bufor, dlugosc + 1 ); string * s = new string; * s = Bufor; GlobalFree( Bufor ); return * s; }
void Element::wyslij_message( UINT rodzaj, WPARAM wParam, char * lParam ) { SendMessage( el, rodzaj, wParam,( LPARAM ) lParam ); }
void Element::CheckBoxZmienStan( bool tryb ) { int id = GetDlgCtrlID( el ); if( tryb ) CheckDlgButton( el, id, BST_CHECKED ); else if( !tryb ) CheckDlgButton( el, id, BST_UNCHECKED ); } bool Element::CheckBoxStan() { int id = GetDlgCtrlID( el ); return( IsDlgButtonChecked( el, id ) == BST_CHECKED ); }
int Element::ComboBoxWybrano() { return ComboBox_GetCurSel( el ); } string Element::ComboBoxGetText() { int dlugosc = ComboBox_GetTextLength( el ); LPSTR NapisZcomboBoxa =( LPSTR ) GlobalAlloc( GPTR, dlugosc + 1 ); ComboBox_GetText( el, NapisZcomboBoxa, dlugosc + 1 ); string * s = new string; * s = NapisZcomboBoxa; return * s; } int Element::ComboBoxGetLenght() { return ComboBox_GetTextLength( el ); }
void Element::RadioButtonZaznacz( int odkad, int dokad, int wybierz ) { CheckRadioButton( el, odkad, dokad, wybierz ); }
BOOL Msg::pobiez() { return GetMessage( & Komunikat, NULL, 0, 0 ); }
void Msg::wyslij() { TranslateMessage( & Komunikat ); DispatchMessage( & Komunikat ); } WPARAM Msg::returnuj() { return Komunikat.wParam; }
Font::Font() { font =( HFONT ) GetStockObject( DEFAULT_GUI_FONT ); }
void Font::UstawNaElement( Element & ele ) { ele.wyslij_message( WM_SETFONT,( WPARAM ) font, 0 ); }
void Font::UstawNaWszystkieElementy() { string s; stringstream ss; ss << elementy.size(); ss >> s; komunikat( WinApiNULL, "Oto rozmiar vectora :(", s.c_str(), MB_OK ); for( int i = 0; i < elementy.size(); i++ ) { UstawNaElement( * elementy[ i ] ); } }
=======================================main.cpp======================================= #include "WinApi.h" #include <Windows.h>
LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wPar, LPARAM lPar );
Element okno; Element CheckBox1; Element lista; Wmain() { int const X = 800, Y = 600; okno.StworzWinClass( hInstance, "glowanaklasa", WndProc, CS_DROPSHADOW, LoadIcon( 0, IDI_APPLICATION ), LoadIcon( 0, IDI_APPLICATION ), LoadCursor( 0, IDC_ARROW ),( HBRUSH ) GetStockObject( WHITE_BRUSH ) ); okno.StworzObjekt( WS_EX_CLIENTEDGE, "glowanaklasa", "tytuloknona", WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, 800, 600, hInstance ); CheckBox1.StworzObjekt( WS_EX_CLIENTEDGE, "BUTTON", "test", WS_CHILD | WS_VISIBLE | BS_CHECKBOX, 200, 25, hInstance, 300, 470, okno ); lista.StworzObjekt( WS_EX_CLIENTEDGE, "COMBOBOX", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | CBS_DROPDOWN, 700, 400, hInstance, 40, 50, okno ); lista.wyslij_message( CB_ADDSTRING, 0, "Magiczne uderzenie" ); lista.wyslij_message( CB_ADDSTRING, 0, "Zwykły strzał" ); Font font; font.UstawNaWszystkieElementy(); okno.wyswietl(); Msg m; while( m.pobiez() ) { m.wyslij(); } return m.returnuj(); }
LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wPar, LPARAM lPar ) { switch( msg ) { case WM_COMMAND: break; case WM_PAINT: break; case WM_CLOSE: PostQuitMessage( 0 ); break; default: return DefWindowProc( hwnd, msg, wPar, lPar ); } return 0; }
|