2017-04-20 38 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

相關問題