在Windows中创建窗口时出错“未定义引用'WinMain @ 16'”

时间:2013-05-31 17:59:34

标签: c++ windows compiler-errors codeblocks winmain

#ifndef UNICODE
#define UNICODE
#endif

#include <windows.h>

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int     nCmdShow)
{

const wchar_t CLASS_NAME[]  = L"Sample Window Class";

WNDCLASS wc = { };

wc.lpfnWndProc   = WindowProc;
wc.hInstance     = hInstance;
wc.lpszClassName = CLASS_NAME;

RegisterClass(&wc);



HWND hwnd = CreateWindowEx(
    0,
    CLASS_NAME,
    L"Learn to Program Windows",
    WS_OVERLAPPEDWINDOW,


    CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,

    NULL,
    NULL,
    hInstance,
    NULL
    );

if (hwnd == NULL)
{
    return 0;
}

ShowWindow(hwnd, nCmdShow);



MSG msg = { };
while (GetMessage(&msg, NULL, 0, 0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}

return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_DESTROY:
    PostQuitMessage(0);
    return 0;

case WM_PAINT:
    {
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hwnd, &ps);

        FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+1));

        EndPaint(hwnd, &ps);
    }
    return 0;

}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

这是代码,这也是您可以在微软网站上找到的标准示例,它教人们如何编程窗口。 http://msdn.microsoft.com/en-us/library/windows/desktop/ff381409(v=vs.85).aspx

每当我尝试在codeblocks中编译此代码时,我收到的问题如下。

对'WinMain @ 16'的未定义引用

这是什么以及如何编译和运行这段代码?

1 个答案:

答案 0 :(得分:0)

我觉得WinMain被称为wWinMain似乎很奇怪:显然你需要一个名为WinMain的函数。

哦,等等,你正在使用代码块?可能wWinMain特定于视觉工作室。代码块需要标准WinMain