首页
社区
课程
招聘
[旧帖] [求助]编写的第一个WIN32窗口占用100% 0.00雪花
发表于: 2009-4-6 19:30 3429

[旧帖] [求助]编写的第一个WIN32窗口占用100% 0.00雪花

2009-4-6 19:30
3429
最近看孙鑫的VC教程,跟着写了下面的窗口代码,但是运行后占用100%,请大家指教。

#include <windows.h>
#include <stdio.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
                                   )
{
        WNDCLASS wndcls;
        wndcls.cbClsExtra=0;
        wndcls.cbWndExtra=0;
        wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
        wndcls.hCursor=LoadCursor(NULL,IDC_ARROW);
        wndcls.hIcon=LoadIcon(NULL,IDI_QUESTION);
        wndcls.hInstance=hInstance;
        wndcls.lpfnWndProc=WindowProc;
        wndcls.lpszClassName="classname";
        wndcls.lpszMenuName=NULL;
        wndcls.style=CS_HREDRAW|CS_VREDRAW;
   
        RegisterClass (&wndcls);
   
        HWND hWnd;
        hWnd=CreateWindow("classname","chuangkoubiaoti",WS_OVERLAPPEDWINDOW,100,100,100,100,NULL,NULL,hInstance,NULL);
    ShowWindow(hWnd,SW_SHOWNORMAL);
        UpdateWindow(hWnd);

        MSG msg;
       
        while (GetMessage(&msg,hWnd,0,0))
        {
                TranslateMessage(&msg);
                DispatchMessage(&msg);

        }
        return 0;

}

LRESULT CALLBACK WindowProc(
                                                        HWND hwnd,      // handle to window
                                                        UINT uMsg,      // message identifier
                                                        WPARAM wParam,  // first message parameter
                                                        LPARAM lParam   // second message parameter
                                                        )
{
   switch(uMsg)
   {
   case WM_CHAR:
           char *szchar;
           sprintf(szchar,"char is %d",wParam);
           MessageBox(hwnd,szchar,"biaoti",MB_OK);
           break;
   case WM_LBUTTONDOWN:
           MessageBox(hwnd,"you have pressed lbutton!","tishi",0);
           HDC hdc;
           hdc=GetDC(hwnd);
           TextOut(hdc,12,42,"ni an xia le zuo jian.",strlen("ni an xia le zuo jian."));
           ReleaseDC(hwnd,hdc);
                   break;
   case WM_PAINT:
           HDC hdc2;
           PAINTSTRUCT ps;
           hdc2=BeginPaint(hwnd,&ps);
           TextOut(hdc2,0,0,"chuang kou chong hui le;",strlen("chuang kou chong hui le;"));
           EndPaint(hwnd,&ps);
                           break;
   case WM_CLOSE:
           if (IDYES==MessageBox(hwnd,"你是否要关闭窗口?","TISHI",MB_YESNO))
           {
                     DestroyWindow(hwnd);
           }
           break;
   case WM_DESTROY:
           PostQuitMessage(0);
           break;
   default:
           return DefWindowProc(hwnd,uMsg,wParam,lParam);

   }
   return 0;
}

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
免费 0
支持
分享
最新回复 (6)
雪    币: 2384
活跃值: (766)
能力值: (RANK:410 )
在线值:
发帖
回帖
粉丝
2
// 这一句
while (GetMessage(&msg,hWnd,0,0))
// 改成
while (GetMessage(&msg,0,0,0))
// 即可
2009-4-6 19:42
0
雪    币: 232
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
果然是这样,感谢加膜拜大牛了

孙鑫在这设为NULL的解释是获得所有窗口的消息,问什么设为本窗口句柄就会占这么多CPU啊
2009-4-6 19:52
0
雪    币: 239
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
GetMessage()你传hWnd进去的话,返回值是-1
If there is an error, the return value is -1.

这样写好一点:
MSG msg;
BOOL bRet;

while((bRet = GetMessage(&msg, NULL, 0, 0))!=0)
{
    if(bRet==-1)
    {
        ;
    }
    else
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
}
2009-4-6 20:16
0
雪    币: 232
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
5
谢谢LS了,学到不少东西!!
2009-4-6 20:19
0
雪    币: 2384
活跃值: (766)
能力值: (RANK:410 )
在线值:
发帖
回帖
粉丝
6
当程序运行时hWnd是有效的窗口句柄,因此消息可以得到有效的处理,这时如果程序是正常运行的话,cpu应该也没什么问题。不过当程序关闭之后,因为当你点击了关闭命令并系统处理了WM_CLOSE消息之后销毁了窗口,这时hWnd窗口句柄不再有效,而这一结果也就导致了窗口的WM_DESTROY消息得不到处理,窗口无法正常退出,而消息又造成堵塞而引起CPU繁忙。
2009-4-6 20:20
0
雪    币: 232
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
7
原来是这样啊,受教了,再次谢谢了
2009-4-6 20:39
0
游客
登录 | 注册 方可回帖
返回
//