none
一个windows程序设计的小问题 RRS feed

  • 问题

  • 本人第一次用这个MSDN,有失误的地方请见谅,还有本人刚刚初学windows程序设计, 问得太白了还请细心的讲解一下, 不胜感激!

    代码是这样的

    #include <windows.h>
    
    LRESULT CALLBACK WintestProc(
    							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,      // pointer to command line
    					   int nCmdShow          // show state of window
    )
    {
    	HWND hwnd;
    	MSG msg;
    	WNDCLASS wndclass;
    	wndclass.style = CS_HREDRAW | CS_VREDRAW; 
        wndclass.lpfnWndProc = WintestProc; 
        wndclass.cbClsExtra = 0; 
        wndclass.cbWndExtra = 0; 
        wndclass.hInstance = hInstance; 
        wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION); 
        wndclass.hCursor = LoadCursor(NULL,IDC_ICON); 
        wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); 
        wndclass.lpszMenuName = NULL; 
        wndclass.lpszClassName = "wintest"; 
    
    	RegisterClass(&wndclass);
    
    	hwnd = CreateWindow("wintest","tempWin",WS_OVERLAPPEDWINDOW,
    		CW_USEDEFAULT,0,CW_USEDEFAULT,0,NULL,NULL,hInstance,NULL);
    
    	ShowWindow(hwnd,SW_SHOWNORMAL);
    	UpdateWindow(hwnd);
    	
    	while(GetMessage(&msg,hwnd,0,0))
    	{
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    	
    	return 0;
    
    }
    
    
    LRESULT CALLBACK WintestProc(
    							 HWND hwnd,      // handle to window
    							 UINT uMsg,      // message identifier
    							 WPARAM wParam,  // first message parameter
    							 LPARAM lParam   // second message parameter
    							 )
    {
    	HDC hdc;
    	PAINTSTRUCT ps;
    	switch(uMsg)
    	{
    	case WM_PAINT:
    		hdc = BeginPaint(hwnd,&ps);
    		TextOut(hdc,50,50,"HELLO WORLD",strlen("HELLO WORLD"));
    		EndPaint(hwnd,&ps);
    		break;
    	case WM_CLOSE:
    		if(IDYES == MessageBox(hwnd,"exit this windows?","eixt", MB_YESNO | MB_ICONQUESTION))
    		{
    			DestroyWindow(hwnd);
    		}
    		break;
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		break;
    	default:
    		return DefWindowProc(hwnd,uMsg,wParam,lParam);
    
    	}
    
    return 0;
    }
    

    问题在于那个消息循环 GetMessage()的第二个参数,我了解的是这第二个参数是获取那个窗口的消息,我设置成了hwnd之后 关闭窗口销毁了,程序还在后台运行。改成NULL ,这样就OK。 请大神给我这迷路的大白讲讲怎么回事??

    • 已移动 yangecnu 2014年1月7日 1:42
    2013年12月28日 2:02

全部回复

  • 问题在于DestroyWindow(hwnd);

    这之后hwnd已经被close了。那么hwnd不再是任何的窗口的句柄。

    GetMessage(&msg,hwnd,0,0)不再能够继续收到该窗口的WM_DESTROY消息。而是返回-1。

    所以while(GetMessage(&msg,hwnd,0,0))是无法推出的。

    如果改用NULL,则会接获所有该线程的窗口消息,这样就可以执行完WM_DESTROY,和WM_QUIT。


    麻烦把正确答案设为解答。

    2014年1月7日 2:38
    版主
  •  关于这一点,MSDN上有说明:
    hWnd
    [in] Handle to the window whose messages are to be retrieved. The window must belong to the current thread.

    If hWnd is NULL, GetMessage retrieves messages for any window that belongs to the current thread, and any messages on the current thread's message queue whose hwnd value is NULL (see the MSG structure). Therefore if hWnd is NULL, both window messages and thread messages are processed.

    If hWnd is -1, GetMessage retrieves only messages on the current thread's message queue whose hwnd value is NULL, that is, thread messages as posted by <mshelp:link keywords="_win32_PostMessage_cpp" tabindex="0" xmlns:mshelp="http://msdn.microsoft.com/mshelp">PostMessage</mshelp:link> (when the hWnd parameter is NULL) or <mshelp:link keywords="_win32_PostThreadMessage_cpp" tabindex="0" xmlns:mshelp="http://msdn.microsoft.com/mshelp">PostThreadMessage</mshelp:link>.


    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.

    2014年1月9日 8:51
    版主