2

这是代码

#include <windows.h>
const wchar_t g_szClassName[] = L"myWindowClass";
// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{/*...*/
    return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;
    //Step 1: Registering the Window Class
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, L"Window Registration Failed!", L"Error!",
        MB_ICONEXCLAMATION | MB_OK);
    return 0;
    }
    // Step 2: Creating the Window...
    return Msg.wParam;
}

此代码直接来自 Forgers Win32 教程(L""需要时带有和 wchar_t)。但是我不能让它在 WinXP SP3 x86 和 VC2008Express SP1 上运行。

4

2 回答 2

2

例如,您没有设置样式成员(取自向导创建的代码):

wc.style = CS_HREDRAW | CS_VREDRAW;
于 2011-08-29T10:11:43.660 回答
1

声明后WNDCLASSEX wc;的内存将只包含随机值。

你可以这样做

WNDCLASSEX wc = { 0 };

或者你可以这样做

ZeroMemory(&wc, sizeof(WNDCLASSEX));

确保每个字段至少设置为零。

于 2018-10-24T22:28:52.283 回答