HELLOWIN程序中遇到的问题

来源:百度知道 编辑:UC知道 时间:2024/05/19 03:42:17
以下这段代码编译能够通过,执行时不产生窗口,但在任务管理器中有显示,有谁知道是出了什么问题?

#include <windows.h>

LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);

int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow // show state
)
{
static TCHAR szAppName[] = TEXT ("HELLOWIN") ;
MSG msg ;
HWND hwnd ;
WNDCLASS wndclass ;

wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hInstance = hIns

根本就不是break的问题,你的程序少了一个return,倒数第四行改成:
return DefWindowProc (hwnd, uMsg, wParam, lParam) ;
程序就OK了.原因是你没有返回系统其它消息的处理方式.Windows不知道你的程序Createwindow之后,除了PAINT和DESTROY之外,你的程序要还干什么.第一个消息循环结束的时候,windows就在等待,就在想怎么处理这个非程序指定的消息呢?消息都被卡在队列里,却不知道如何处理,所以有进程.
而return DefWindowProc (hwnd, uMsg, wParam, lParam) ; 就告诉了windows如果不知道怎么处理,就按默认的方式处理,于是OK了,消息循环活了,窗口可重绘了.

给你消息了,你从“我的消息”里就能看见。至于为什么写到main里就会出现0x00000000,我也不知道。可是结果确实是写成函数调用就可以了。建议你追下hInstance的变化。我没试过。
直接在main中注册窗口类可以成功,可是继续CreateWindow的时候总是失败。你可以单步追下你的hwnd,为0x00000000。
此外,你的消息循环也有问题怎么能够“return 0之后再写break;”请好好理解一下return和switch的用法。不明白的再问我。

可以改成如下方法:

#include<windows.h>

LRESULT CALLBACK WndProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);

BOOL InitWindowsClass(HINSTANCE hInstance);
BOOL InitWindows(HINST