我来我网
https://5come5.cn
 
您尚未 登录  注册 | 菠菜 | 软件站 | 音乐站 | 邮箱1 | 邮箱2 | 风格选择 | 更多 » 
 

本页主题: [求助]关于Win32编程的一个问题 显示签名 | 打印 | 加为IE收藏 | 收藏主题 | 上一主题 | 下一主题

albert





性别: 保密 状态: 该用户目前不在线
等级: 栋梁之材
家族: 唯war独尊
发贴: 634
威望: 0
浮云: 1116
在线等级:
注册时间: 2005-10-04
最后登陆: 2012-02-12

5come5帮你背单词 [ triangle /'traiængl/ n. 三角,三角形 ]


[求助]关于Win32编程的一个问题

#include <windows.h>    

LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam);
int WINAPI WinMain(   HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int   nCmdShow)    
{
  MSG     msg;
  WNDCLASS   wc;
  HWND hWnd;

  wc.style         = CS_HREDRAW | CS_VREDRAW;  
  wc.lpfnWndProc     = (WNDPROC) WndProc;          
  wc.cbClsExtra     = 0;                        
  wc.cbWndExtra     = 0;                        
  wc.hInstance     = hInstance;                  
  wc.hIcon         = LoadIcon(NULL, IDI_WINLOGO);        
  wc.hCursor         = LoadCursor(NULL, IDC_ARROW);    
  wc.hbrBackground   = (HBRUSH)GetStockObject(BLACK_BRUSH);                        
  wc.lpszMenuName     = NULL;                    
  wc.lpszClassName   = "Demo";    

  if (!RegisterClass(&wc))                        
  {
    MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);
    return FALSE;                          
  }
  hWnd=CreateWindow(
                    "Demo",                
                    "Demo1",                                        
                    WS_OVERLAPPEDWINDOW,          
                    CW_USEDEFAULT,
                    CW_USEDEFAULT,                    
                    CW_USEDEFAULT,  
                    CW_USEDEFAULT,  
                    NULL,                  
                    NULL,                    
                    hInstance,            
                    NULL    
    );
  ShowWindow(hWnd,nCmdShow);
  UpdateWindow(hWnd);

  //这里为什么改成GetMessage(&msg,hWnd,0,0)程序就关不了了?    
  while(GetMessage(&msg,NULL,0,0))
  {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }

  return (msg.wParam);                  
}

LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)        
{
  switch (uMsg)                        
  {
    case WM_CLOSE:
        PostQuitMessage(0);          
        return 0;

    default:
        break;
  }

  return DefWindowProc(hWnd,uMsg,wParam,lParam);
}

如果将GetMessage中的第2个NULL参数改成和hWnd程序就关不了了,我查过MSDN上边讲当第2个参数是NULL时,表示为属于该程序的消息队列的所有窗口检索信息。但问题是,我这个程序里就只有一个窗口,用该窗口的句柄作为参数应该也可以吧。
望高手指点一二。
顶端 Posted: 2006-11-13 21:12 | [楼 主]
电子青蛙



性别: 帅哥 状态: 该用户目前不在线
等级: 人见人爱
家族: 单身贵族
发贴: 2924
威望: 0
浮云: 1197
在线等级:
注册时间: 2005-09-22
最后登陆: 2008-06-28

5come5帮你背单词 [ tolerant /'tolərənt/ a. 忍受的,容忍的,宽容的 ]


GetMessage() will return -1 if it encounters an error. Make sure you remember this, or it will catch you out at some point... even though GetMessage() is defined as returning a BOOL, it can return values other than TRUE or FALSE, since BOOL is defined as UINT (unsigned int). The examples of code like this while(GetMessage(&msg, NULL, 0, 0)) may seem to work, but will not process certian conditions correctly. So the correct way of doing this is while(GetMessage(&msg, NULL, 0, 0)>0), it will work perfectly well
顶端 Posted: 2006-11-15 19:55 | [1 楼]
电子青蛙



性别: 帅哥 状态: 该用户目前不在线
等级: 人见人爱
家族: 单身贵族
发贴: 2924
威望: 0
浮云: 1197
在线等级:
注册时间: 2005-09-22
最后登陆: 2008-06-28

5come5帮你背单词 [ wood /wud/ n. 木材,木头,木料,(pl.)森林,林地 ]


Your problem is while you click button close, it will send a message WM_CLOSE to the message queue and then WM_DESTROY will also be post, what will actually destory the window is message WM_DESTORY, so it will not work while you click the close button with out handling the message WM_DESTORY. You should write like this:
switch (uMsg)
{
  case WM_CLOSE:
    DestroyWindow(hwnd);
    break;
  case WM_DESTORY:
    PostQuitMessage(0);
    return (0);   //this is not essential
  default:
    return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

Remark: PostQuitMessage() is designed for message WM_DESTORY; while handling message WM_CLOSE you should use DestroyWindow() and do something else to free the memory, but windows XP has its own garbage collector.
To get rid of these complex message loop, you can use message analyser. in this way you need to "#include <windowx.h>"


[ 此贴被电子青蛙在2006-11-16 10:49重新编辑 ]
顶端 Posted: 2006-11-15 20:09 | [2 楼]
我来我网·5come5 Forum » 程序员之家

Total 0.012427(s) query 6, Time now is:10-27 16:56, Gzip enabled
Powered by PHPWind v5.3, Localized by 5come5 Tech Team, 黔ICP备16009856号