我刚刚开始学习C ++ for Win,现在我在tut的三个部分。所以我有这个代码:
#include <Windows.h>
#include <stdio.h>
#define WIN_WIDTH 300
#define WIN_HEIGHT 200
#define class_name L"HDC"
LRESULT CALLBACK WinProc(HWND hwnd , UINT Message ,WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hinstance ,HINSTANCE hPrev,PSTR cmdline ,int ishow)
{
HWND hwnd;
MSG msg;
WNDCLASSEX wndclassex ={0};
wndclassex.style = CS_HREDRAW | CS_VREDRAW;
wndclassex.cbSize = sizeof(wndclassex);
wndclassex.lpfnWndProc;
wndclassex.hInstance = hinstance;
wndclassex.lpszClassName = class_name;
wndclassex.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
RegisterClassEx(&wndclassex);
hwnd = CreateWindow(class_name,L"My Second WinDow Application",
WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
WIN_WIDTH,WIN_HEIGHT,NULL,NULL,hinstance,NULL);
if(!hwnd)
return EXIT_FAILURE;
HDC hdc = GetDC(hwnd);
if(!hdc)
return EXIT_FAILURE;
ShowWindow(hwnd,ishow);
UpdateWindow(hwnd);
RECT shen;
GetClientRect(hwnd,&shen);
FillRect(hdc,&shen,(HBRUSH)GetStockObject(WHITE_BRUSH));
while(1)
{
if(PeekMessage(&msg,hwnd,0,0,PM_REMOVE))
{
if(msg.message==WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
ReleaseDC(hwnd,hdc);
UnregisterClass(class_name,hinstance);
return msg.wParam;
}
LRESULT CALLBACK DefWinProc(HWND hwnd,UINT Message,WPARAM wParam ,LPARAM lParam)
{
switch(Message)
{
case WM_DESTROY:
case WM_CLOSE:
PostQuitMessage(0);
return 0;
}
return DefWinProc(hwnd,Message,wParam,lParam);
}
但我不幸运,系统注意到:
'device context.exe': Loaded 'C:\Windows\SysWOW64\lpk.dll', Symbols loaded (source information stripped).
'device context.exe': Loaded 'C:\Windows\SysWOW64\usp10.dll', Symbols loaded (source information stripped).
'device context.exe': Loaded 'C:\Windows\SysWOW64\msvcrt.dll', Symbols loaded (source information stripped).
'device context.exe': Loaded 'C:\Windows\SysWOW64\advapi32.dll', Symbols loaded (source information stripped).
'device context.exe': Loaded 'C:\Windows\SysWOW64\sechost.dll', Symbols loaded (source information stripped).
'device context.exe': Loaded 'C:\Windows\SysWOW64\rpcrt4.dll', Symbols loaded (source information stripped).
'device context.exe': Loaded 'C:\Windows\SysWOW64\sspicli.dll', Symbols loaded (source information stripped).
'device context.exe': Loaded 'C:\Windows\SysWOW64\cryptbase.dll', Symbols loaded (source information stripped).
'device context.exe': Loaded 'C:\Windows\SysWOW64\msvcr100d.dll', Symbols loaded.
'device context.exe': Loaded 'C:\Windows\SysWOW64\imm32.dll', Symbols loaded (source information stripped).
'device context.exe': Loaded 'C:\Windows\SysWOW64\msctf.dll', Symbols loaded (source information stripped).
'device context.exe': Loaded 'C:\Windows\SysWOW64\uxtheme.dll', Symbols loaded (source information stripped).
First-chance exception at 0x00000000 in device context.exe: 0xC0000005: Access violation.
First-chance exception at 0x00000000 in device context.exe: 0xC0000005: Access violation.
First-chance exception at 0x00000000 in device context.exe: 0xC0000005: Access violation.
The program '[15148] device context.exe: Native' has exited with code 1 (0x1).
请有人知道错误只是帮我修复它,我只是想靠窗口编程。先谢谢你。
答案 0 :(得分:5)
wndclassex.lpfnWndProc;
你确实意识到这确实没什么,wndclassex.lpfnWndProc
指针仍然是NULL,对吗?
答案 1 :(得分:2)
这只是一个建议,但请考虑一下:
wndclassex.lpfnWndProc;
有些东西告诉我,没有为您的注册功能分配窗口过程。 (或做任何其他远程建设性的事情)。
尝试:
wndclassex.lpfnWndProc = WndProc;
答案 2 :(得分:1)
在这种情况下你的问题很可能是你在这里为窗口过程指定一个空指针:
wndclassex.lpfnWndProc;
这很糟糕,因为您实际上需要一个窗口过程来处理任何事情,至少您必须响应WM_CREATE消息以确保实际创建窗口(link)
然后,您尝试重新定义DefWinProc,这是一个Windows库中定义的函数,它只实现了您不想处理的消息的默认行为。
你需要做的第一件事就是找到窗口函数的另一个名称,我看到你定义了一个名为WinProc的函数原型,将其命名为,然后改变我之前向你展示的行
wndclassex.lpfnWndProc = WinProc;
它应该有用。