Bonjour,
ça faisait longtemps que je n'avais pas fait du win32 pur et dur (merci wxWidgets).
Voulant un peu structurer mon code, j'ai voulu essayer de faire le
maximum avec des classes, et je me suis cassé les dents dès le début:
Voici en gros le code :
class CApp
{
public:
CApp(HINSTANCE hInst);
~CApp(void);
bool Initialize(WNDPROC WndProc);
private:
HINSTANCE p_hInst;
HWND p_hWnd;
};
CApp::CApp(HINSTANCE hInst)
{
if (hInst==NULL)
p_hInst=GetModuleHandle(NULL);
else
p_hInst=hInst;
p_hWnd=NULL;
}
bool CApp::Initialize(WNDPROC WndProc)
{
WNDCLASSEX wcex;
ZeroMemory(&wcex,sizeof(WNDCLASSEX));
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = p_hInst;
wcex.hIcon = LoadIcon(p_hInst,(LPCTSTR)IDI_MAINICON);
wcex.hCursor = LoadCursor(NULL,IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(p_hInst,(LPCTSTR)IDI_SMALL);
RegisterClassEx(&wcex);
p_hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, p_hInst, NULL);
if (!p_hWnd) return false;
ShowWindow(p_hWnd,SW_SHOW);
UpdateWindow(p_hWnd);
return true;
}
J'ai ensuite (dans une autre fichier cpp), une fonction WNDPROC toute simple.
et je fais ceci dans la WinMain:
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInst,LPTSTR lpCmdLine,int nCmdShow)
{
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDS_WNDCLASS, szWindowClass, MAX_LOADSTRING);
// Création de l'application
CApp TheApp(hInstance);
if (TheApp.Initialize(MainWindowProc)==false)
return FALSE;
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_ACCELL);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
Le problème, c'est que j'obtiens un Acces
Violation lors de l'appel au RegisterClassEx dans CApp::Initialize, et
je n'arrive pas à savoir pourquoi.
Est-ce dû au fait que je passe la WNDPROC en paramètre ?
Merci d'avance.
Afficher la suite