我收到此错误:
LNK1120:第 1 行上有 1 个未解析的外部
错误 LNK2019:未解析的外部符号_winproc@20在函数 _WinMain@16 C:\Users\giorgi\Documents\Visual Studio 2013\Projects\Hello\Hello\Source.obj 你好
我是WinApi的新手,请帮忙。
#include <windows.h>
LRESULT CALLBACK winproc(WNDPROC lpPrevWndFunc, HWND hwnd, UINT msg, WPARAM wp, LPARAM lp);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR LpCmdLine, int nCmdShow)
{
WNDCLASSEX class;
ZeroMemory(&class, sizeof(WNDCLASSEX));
class.cbSize = sizeof(WNDCLASSEX);
class.style = CS_HREDRAW | CS_VREDRAW;
class.lpfnWndProc = (WNDPROC)winproc;
class.cbClsExtra = 0;
class.cbWndExtra = 0;
class.hInstance = hInstance;
class.hIcon = NULL;
class.hCursor = LoadCursor(NULL, IDC_ARROW);
class.hbrBackground = (HBRUSH)COLOR_WINDOW;
class.lpszClassName = "window class";
class.lpszMenuName = NULL;
class.hIconSm = NULL;
RegisterClassEx(&class);
HWND hwnd = CreateWindowEx
(
WS_EX_ACCEPTFILES,
"window class",
"window",
WS_OVERLAPPED,
200,
200,
800,
600,
NULL,
NULL,
hInstance,
NULL
);
ShowWindow(hwnd, nCmdShow);
MSG msg;
ZeroMemory(&msg, sizeof(MSG));
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
switch (msg)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
break;
}
return DefWindowProc(hwnd, msg, wp, lp);
}
正如其他人所说,C 区分大小写,因此 winproc
和 WinProc
将是两个不同的函数。 您还需要确保 Windows 过程的签名与 Windows 的预期相匹配,因此请进行以下更改:
-
将
LRESULT CALLBACK winproc(WNDPROC lpPrevWndFunc, HWND hwnd, UINT msg, WPARAM wp, LPARAM lp);
更改为LRESULT CALLBACK winProc(HWND, UINT, WPARAM, LPARAM);
-
将
class.lpfnWndProc = (WNDPROC)winproc;
更改为class.lpfnWndProc = (WNDPROC)winProc;
-
将
LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
更改为LRESULT CALLBACK winProc(HWND hwnd, UINT mgs, WPARAM wp, LPARAM lp)
最后,自从我在win32-API级别编程以来已经有一段时间了,但我相信你的Windows过程应该看起来像:
LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
switch (msg)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
break;
default:
return DefWindowProc(hwnd, msg, wp, lp);
}
}
换句话说,如果您不自己处理消息,则只想返回默认窗口过程(DefWindowProc
)。