Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
// Von dieser KLasse wird geerbt class Panel { public: Panel(); virtual ~Panel(); inline HWND getHandle() const { return (m_HWnd); } virtual LRESULT windowPorc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) =0; // <-- das wird dann später aufgerufen und soll das Event behandeln // Static static LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); protected: void init(); HWND m_HWnd; }; ////////////////////////////////////////////////////////////////////////////////////////////////////////////// Panel::Panel() { } Panel::~Panel() { } void Panel::init() { if(!m_HWnd) throw InitException("Was not able to create window !"); } LRESULT CALLBACK Panel::WindowProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { Panel* p = NULL; try { // Wir müssen nur den Pointer auf das Panel-Objekt bekommen if(msg == WM_CREATE) { // Wir nehmen den this-pointer aus CreateWindow und setzen ihn als Userdata CREATESTRUCT* cs = (CREATESTRUCT*)lParam; p = (Panel*)cs->lpCreateParams; SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)p); }else{ // Wir holen den this-pointer direkt aus der userdata p = (Panel*)GetWindowLongPtr(hwnd, GWLP_USERDATA); if(!p) return DefWindowProc(hwnd, msg, wParam, lParam); // Wenn kein this-pointer gesetzt ist, standardmäßig fortfahren (muss ja nicht jedes Fenster so nen Panel haben) } // Und schon können wir den Objekthandler (Panel) aufrufen, welches zum WindowHandle gehört return p->windowPorc(hwnd, msg, wParam, lParam); }catch(std::exception& e) { // Fehler abfangen (Exceptions die hier nicht gefangen werden gehen in der WinApi unter, werden ignoriert) MessageBox(NULL, e.what(), "Exception !", MB_OK | MB_ICONERROR); }catch(...) { MessageBox(hwnd, "Unknown error occured !", "Exception !", MB_OK | MB_ICONERROR); } return 0; } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
class SimpleWindow : public Panel { public: SimpleWindow(); ~SimpleWindow(); LRESULT windowPorc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); private: }; /////////////////////////////////////////////////////////////////////////////////////////////// SimpleWindow::SimpleWindow() { // Das hier kann man bestimmt auch sehr gut in die Panelklasse verlegen m_HWnd = CreateWindowEx( WS_EX_TOOLWINDOW | WS_EX_TOPMOST, /* .... */, "Abgeleitet von Panel", WS_POPUP, 0, 0, 100, 100, NULL, NULL, extInstance, this); // <---- das hier ist besonders wichtig, denn hier wird der Pointer auf dieses Objekt WM_CREATE überreicht init(); } StatusBar::~StatusBar() { } LRESULT StatusBar::windowPorc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch(msg) { case WM_CREATE: { }break; case WM_PAINT: { /* ...*/ }return 0; case WM_DESTROY: { PostQuitMessage(0); }break; case WM_CLOSE: { }return 0; default: break; }; return DefWindowProc(hwnd, msg, wParam, lParam); } |
Administrator
Wie soll ich allgemein Events zu einem Fensterhandle fangen, wenn der direkt auf die Fensterregistrierung folgende Befehl möglicherweise noch nach den ersten Events für dieses Fenster ausgeführt wird?
Du fängst die Events in einem anderen Thread?
Administrator
Community-Fossil
Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer
Genau.Ich vermute mal dein Problem ist dass Windows schon Nachrichten (z.B. WM_CREATE) an das Fenster schickt bevor CreateWindowEx() returned.
Werbeanzeige