2017-04-21 73 views
0

我不斷收到以下錯誤,當我嘗試編譯我非常簡單的程序:未定義的參考`的WindowProc(HWND __ *,無符號整型,無符號整型,長)@ 16'

未定義引用的WindowProc( HWND __ *,unsigned int,unsigned int,long)@ 16'

我沒有看到問題,它讓我瘋狂!

#include <iostream> 
#include <windows.h> 

using namespace std; 

int sizeX = 500; 
int sizeY = 500; 

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

char szClassName[ ] = "WindowsApp"; 

int WINAPI WinMain(
        HINSTANCE hThisInstance, 
        HINSTANCE hPrevInstance, 
        LPSTR lpszArgument, 
        int nFunsterStil 
        ) 
{ 
    HWND hwnd; 
    MSG messages; 
    WNDCLASSEX wincl; 

    wincl.hInstance = hThisInstance; 
    wincl.lpszClassName = szClassName; 
    wincl.lpfnWndProc = WindowProc; 
    wincl.style = CS_DBLCLKS; 
    wincl.cbSize = sizeof(WNDCLASSEX);` 

    wincl.hIcon = LoadIcon(NULL, IDI_APPLICATION); 
    wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION); 
    wincl.hCursor = LoadCursor(NULL, IDC_ARROW); 
    wincl.lpszMenuName = NULL; 
    wincl.cbClsExtra = 0; 
    wincl.cbWndExtra = 0; 

    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND; 

    if(!RegisterClassEx(&wincl)) 
     return 0; 

    hwnd = CreateWindowEx(
          0, 
          szClassName, 
          "Windows App", 
          WS_SYSMENU, 
          CW_USEDEFAULT, 
          CW_USEDEFAULT, 
          sizeX, 
          sizeY, 
          HWND_DESKTOP, 
          NULL, 
          hThisInstance, 
          NULL 
        ); 
} 

代碼有什麼問題?

+2

哪裏是'WindowProc'?你宣佈它,但實際功能在哪裏? – PaulMcKenzie

回答

1

您沒有提供WindowProc()的定義。你宣佈它,並將其分配給您的WNDCLASSEX,現在你必須真正實現它的身體,如:

#include <iostream> 
#include <windows.h> 

using namespace std; 

int sizeX = 500; 
int sizeY = 500; 

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

const char szClassName[ ] = "WindowsApp"; 

int WINAPI WinMain(
        HINSTANCE hThisInstance, 
        HINSTANCE hPrevInstance, 
        LPSTR lpszArgument, 
        int nFunsterStil 
        ) 
{ 
    HWND hwnd; 
    MSG messages; 
    WNDCLASSEX wincl; 

    wincl.hInstance = hThisInstance; 
    wincl.lpszClassName = szClassName; 
    wincl.lpfnWndProc = WindowProc; 
    wincl.style = CS_DBLCLKS; 
    wincl.cbSize = sizeof(WNDCLASSEX);` 

    wincl.hIcon = LoadIcon(NULL, IDI_APPLICATION); 
    wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION); 
    wincl.hCursor = LoadCursor(NULL, IDC_ARROW); 
    wincl.lpszMenuName = NULL; 
    wincl.cbClsExtra = 0; 
    wincl.cbWndExtra = 0; 

    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND; 

    if(!RegisterClassEx(&wincl)) 
     return 0; 

    hwnd = CreateWindowEx(
          0, 
          szClassName, 
          "Windows App", 
          WS_SYSMENU, 
          CW_USEDEFAULT, 
          CW_USEDEFAULT, 
          sizeX, 
          sizeY, 
          HWND_DESKTOP, 
          NULL, 
          hThisInstance, 
          NULL 
        ); 

    // message loop here ... 

    return 0; 
} 

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 
{ 
    // your code here ... 

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

查看MSDN文檔以獲取更多詳細信息:

Window Procedures

相關問題