我收到了这个错误:
LNK1120:第1行有1个未解析的外部
错误LNK2019:函数_WinMain @ 16 C:\ Users \ giorgi \ Documents \ Visual Studio 2013 \ Projects \ Hello \ Hello \ Source.obj Hello
中引用了未解析的外部符号_winproc @ 20我是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);
}
答案 0 :(得分:1)
正如其他人所说,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
)。