000
02.04.2007, 21:48 Uhr
~fasmat
Gast
|
Ich hab versucht WndProc und WinMain auf Methoden einer Klasse umzuleiten, aber es funktioniert nicht ganz... hier ein Auszug aus meinem Quelltext:
cMain_window.cpp:
C++: |
#include "cMain_window.h" #include <windows.h>
cMain_window::cMain_window(TCHAR *ClassName) { m_ClassName = ClassName; }
cMain_window::~cMain_window(void) { }
int cMain_window::Main(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg; HWND hwnd; WNDCLASSEX windowClass; ZeroMemory(&windowClass, sizeof(WNDCLASSEX)); windowClass.cbSize = sizeof(WNDCLASSEX); windowClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; windowClass.lpfnWndProc = (WNDPROC)(WindowProc); windowClass.hInstance = hInstance; windowClass.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE); windowClass.hCursor = LoadCursor(NULL, IDC_ARROW); windowClass.lpszClassName = m_ClassName; if (RegisterClassEx(&windowClass) == 0) { MessageBox(NULL, TEXT("Error: Unable to execute"), m_ClassName, MB_ICONERROR); return -1; }
hwnd = CreateWindow(m_ClassName, TEXT("Free Talk"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 700, 300, NULL, NULL, hInstance, NULL);
ShowWindow (hwnd, nCmdShow); UpdateWindow (hwnd);
while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg); DispatchMessage (&msg); } UnregisterClass(m_ClassName, hInstance); return 0; }
LRESULT cMain_window::Message(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_SYSCOMMAND: switch (wParam) { case SC_SCREENSAVE: case SC_MONITORPOWER: break; }
case WM_SIZE: switch (wParam) { case SIZE_MINIMIZED: case SIZE_MAXIMIZED: case SIZE_RESTORED: break; }
case WM_CLOSE: { MessageBox(NULL, TEXT("Closing Window"), TEXT("Info"), MB_ICONINFORMATION); PostQuitMessage (0); return 0; } case WM_DESTROY: { PostQuitMessage(0); return 0; } }
return DefWindowProc(hWnd, uMsg, wParam, lParam); }
|
windows.cpp:
C++: |
#include "cMain_window.h"
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { int ret = -1;
cMain_window *appl = new cMain_window(TEXT("free talk")); if (appl) { ret = appl->Main(hInstance, hPrevInstance, lpCmdLine, nCmdShow); delete appl; } else { MessageBox(HWND_DESKTOP, TEXT("Error Creating Application Class"), TEXT("Error"), MB_OK | MB_ICONEXCLAMATION); } return ret; }
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { LONG user_data = GetWindowLong(hWnd, GWL_USERDATA); if (user_data == 0) { if (uMsg == WM_CREATE) { CREATESTRUCT *creation = reinterpret_cast<CREATESTRUCT *>(lParam); cMain_window *appl = reinterpret_cast<cMain_window *>(creation->lpCreateParams); SetWindowLong(hWnd, GWL_USERDATA, reinterpret_cast<LONG>(appl)); return 0; } } else { cMain_window *appl = reinterpret_cast<cMain_window *>(user_data); return appl->Message(hWnd, uMsg, wParam, lParam); } return DefWindowProc(hWnd, uMsg, wParam, lParam); }
|
cMain_window.h:
C++: |
#pragma once
#include <windows.h>
class cMain_window { public: cMain_window (TCHAR *); virtual ~cMain_window (void);
private: // Program Main Loop friend int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow); int Main(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow);
// Message Handler friend LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); LRESULT Message(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
private: TCHAR * m_ClassName; };
|
Das Problem ist folgendes: in cMain_window::Main komm ich rein, das Fenster wird angezeigt. Nur wenn ich auf schließen klicke, wird das Programm nicht beendet. Durch Debuggen hab ich festgestellt, dass cMain_window::Message nie aufgerufen wird, könnt ihr mir sagen warum? |