2017-04-20 227 views
-1

我在这里做错了什么,但错误信息并没有给我任何线索。对CreateWindow的调用总是失败(返回NULL,并且GetLastError()返回50)。我只想要一个简单的空白窗口,但显然我的要求是“不支持”。CreateWindow失败,错误50(该请求不受支持。)

// gcc basic.c -o basic.exe -mwindows 
#include <stdio.h> 
#include <windows.h> 

// Function prototypes. 
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int); 
BOOL InitApplication(HINSTANCE); 
BOOL InitInstance(HINSTANCE, int); 
LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM); 

// Application entry point. 
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 
{ 
    MSG msg; 
    if (!InitApplication(hinstance)) 
     return FALSE; 

    if (!InitInstance(hinstance, nCmdShow)) 
     return FALSE; 

    BOOL fGotMessage; 
    while ((fGotMessage = GetMessage(&msg, (HWND) NULL, 0, 0)) != 0 && fGotMessage != -1) 
    { 
     TranslateMessage(&msg); 
     DispatchMessage(&msg); 
    } 
    return msg.wParam; 
} 

BOOL InitApplication(HINSTANCE hinstance) 
{ 
    WNDCLASSEX wcx; 
    wcx.cbSize = sizeof (wcx);   // size of structure 
    wcx.style = CS_HREDRAW | CS_VREDRAW; 
    wcx.lpfnWndProc = MainWndProc;  // points to window procedure 
    wcx.cbClsExtra = 0;    // no extra class memory 
    wcx.cbWndExtra = 0;    // no extra window memory 
    wcx.hInstance = hinstance;   // handle to instance 
    wcx.hIcon = NULL; 
    wcx.hCursor = NULL; 
    wcx.hbrBackground = GetStockObject(WHITE_BRUSH);     // white background brush 
    wcx.lpszMenuName = NULL; // name of menu resource 
    wcx.lpszClassName = "MainWClass"; // name of window class 
    wcx.hIconSm = NULL; // small class icon 
    return RegisterClassEx(&wcx); 
} 

BOOL InitInstance(HINSTANCE hinstance, int nCmdShow) 
{ 
    HWND hwnd; 

    // Create the main window. 
    hwnd = CreateWindow(
     "MainWClass",  // name of window class 
     "Sample",   // title-bar string 
     WS_OVERLAPPEDWINDOW, // top-level window 
     CW_USEDEFAULT,  // default horizontal position 
     CW_USEDEFAULT,  // default vertical position 
     CW_USEDEFAULT,  // default width 
     CW_USEDEFAULT,  // default height 
     (HWND) NULL,   // no owner window 
     (HMENU) NULL, 
     hinstance,   // handle to application instance 
     (LPVOID) NULL);  // no window-creation data 

    if (!hwnd) { 
     int error_code = GetLastError(); 
     char caption[256]; 
     snprintf(caption, sizeof caption, "CreateWindow: error %d", error_code); 
     MessageBox(NULL, caption, "CreateWindow error", MB_OK); 
     return FALSE; 
    } 

    ShowWindow(hwnd, nCmdShow); 
    UpdateWindow(hwnd); 
    return TRUE; 
} 

LRESULT CALLBACK 
MainWndProc(HWND hwnd, UINT uMsg, WPARAM wparam, LPARAM lparam) 
{ 
    switch (uMsg) { 
    case WM_CLOSE: 
     DestroyWindow(hwnd); 
     return 1; 
    case WM_DESTROY: 
     PostQuitMessage(0); 
     break; 
    default: 
     DefWindowProc(hwnd, uMsg, wparam, lparam); 
    } 
    return 0; 
} 
+0

不要编辑出问题中的问题。如果你这样做,它变得毫无用处:*“我曾经有过一个代码问题,我没有显示,这是工作代码。”*我们知道工作代码已经看起来如何。 – IInspectable

+0

没有人回答我的问题。他们在谈论一些完全不同的错误。 – Terran

+1

我相当确信,从'WM_NCCREATE'返回'0'(就像你的代码所做的那样)会导致'CreateWindow'返回'NULL'。你为什么认为没有人解决你的问题? – IInspectable

回答

0

您的窗口过程已损坏。取而代之的

DefWindowProc(hwnd, uMsg, wparam, lparam); 

你必须写

return DefWindowProc(hwnd, uMsg, wparam, lparam); 

此外,WM_CLOSE的文件说:

如果应用程序处理此消息时,它应返回零。

您不遵守该规则。

+0

好的,我会解决这些问题,谢谢。 – Terran

相关问题