我刚刚遇到问题,同时使用WinAPI创建了一个窗口,这让我感到非常尴尬。
对CreateWindowEx的调用失败,返回0.之后立即调用的GetLastError也返回0。
当我在DefWindowProc上设置断点时,我的调试器确认它正在被调用,因为它应该是。
我在下面列出了一个最小的例子。 为什么对CreateWindowEx的调用失败?
的WndProc
LRESULT CALLBACK WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (Msg) { case WM_DESTROY: PostQuitMessage(EXIT_SUCCESS); return 0; } // LRESULT WINAPI DefWindowProc(HWND, UINT, WPARAM, LPARAM); return DefWindowProc(hWnd, msg, lParam, wParam); }对CreateWindowEX的调用
hWnd = CreateWindowEx( WS_EX_OVERLAPPEDWINDOW, // dwExStyle CLASS_NAME, // lpClassName WINDOW_NAME, // lpWindowName WS_OVERLAPPEDWINDOW, // dwStyle CW_USEDEFAULT, 0, // x, y CW_USEDEFAULT, 0, // width, height NULL, // hWndParent NULL, // hMenu hInstance, // hInstance NULL // lpParam ); if (NULL == hWnd) { //GetLastError(); return 1; }I just had a problem, while creating a window with WinAPI, which took me an embarrassing long time to solve.
The call to CreateWindowEx failed, returning 0. GetLastError, called immediately afterwards, also returned 0.
When I set a breakpoint on DefWindowProc, my debugger confirmed that it was being called, as it should be.
I've included a minimal example below. Why is the call to CreateWindowEx failing?
WndProc
LRESULT CALLBACK WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (Msg) { case WM_DESTROY: PostQuitMessage(EXIT_SUCCESS); return 0; } // LRESULT WINAPI DefWindowProc(HWND, UINT, WPARAM, LPARAM); return DefWindowProc(hWnd, msg, lParam, wParam); }The call to CreateWindowEX
hWnd = CreateWindowEx( WS_EX_OVERLAPPEDWINDOW, // dwExStyle CLASS_NAME, // lpClassName WINDOW_NAME, // lpWindowName WS_OVERLAPPEDWINDOW, // dwStyle CW_USEDEFAULT, 0, // x, y CW_USEDEFAULT, 0, // width, height NULL, // hWndParent NULL, // hMenu hInstance, // hInstance NULL // lpParam ); if (NULL == hWnd) { //GetLastError(); return 1; }最满意答案
我不小心切换了传递给DefWindowProc的wParam和lParam值。
以正确的顺序给出函数参数解决了我的问题。
return DefWindowProc(hWnd, msg, wParam, lParam);I accidentally switched the wParam and lParam values I was passing to DefWindowProc.
Giving the function arguments in the proper order solved my problem.
return DefWindowProc(hWnd, msg, wParam, lParam);更多推荐
发布评论