如何让wndproc处理到多个窗口的消息

时间:2012-12-06 13:11:07

标签: c++ winapi wndproc

我一直在尝试让wndproc成功接收来自消息队列的消息并根据我的代码处理它们但是它不按照我想要的方式工作,我希望它做的是确定哪个窗口收到WM_DESTROY或WM_CLOSE,并调用特定于哪个窗口关闭的代码,但由于某种原因,目前它什么也没做。经过大量的实验,我已经让它以不同的方式部分运行,但似乎每个实现都不能以正确的方式运行。这是我最新的不成功的代码:

    while (GetMessage (&messages, NULL, 0, 0))
{
    /* Translate virtual-key messages into character messages */
    TranslateMessage(&messages);
    /* Send message to WindowProcedure */
    DispatchMessage(&messages);
}

/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */


LRESULT CALLBACK Proc2(HWND mainwin, UINT message, WPARAM wParam, LPARAM lParam )
{
switch (message)                  /* handle the messages */
{
                       case WM_DESTROY:
                       const HWND active_window = GetActiveWindow();
                       if (active_window == mainwin)
                                {
                                   PostQuitMessage(0);
                                }
                                   if (active_window == hwnd2)
                                   {
                                                 PostQuitMessage(0);
                                                EnableWindow (mainwin, true);
                                   }
                                   break;
} 
 switch (wParam)                  /* handle the messages */
{
              case ID_2 :
            PostQuitMessage(0);
            break;
              case ID_1 :
               ShowWindow (hwnd2, SW_SHOW);     
               break;
                            default:                      /* for messages that we don't deal with */
        return DefWindowProc (mainwin, message, wParam, lParam); 
                    break;
}  

    return 0;
}    

以下是我尝试使用多个窗口过程的代码

while (GetMessage (&messages, NULL, 0, 0))
{
    /* Translate virtual-key messages into character messages */
    TranslateMessage(&messages);
    /* Send message to WindowProcedure */
    DispatchMessage(&messages);
}

/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */


LRESULT CALLBACK Proc2(HWND mainwin, UINT message, WPARAM wParam, LPARAM lParam )
{
switch (message)                  /* handle the messages */
{
                       case WM_DESTROY:
                                   PostQuitMessage(0);
                                   break;
} 
 switch (wParam)                  /* handle the messages */
{
              case ID_2 :
            PostQuitMessage(0);
            break;
              case ID_1 :
               ShowWindow (hwnd2, SW_SHOW);     
               break;
                            default:                      /* for messages that we don't deal with */
        return DefWindowProc (mainwin, message, wParam, lParam); 
                    break;
}  

    return 0;
}    
LRESULT CALLBACK Proc3(HWND hwnd2, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
           case WM_DESTROY:
EnableWindow (mainwin, false);
                break;
case WM_CLOSE:
EnableWindow (mainwin, false);
break;
}
switch (wParam)
{
   default:
           return DefWindowProc (hwnd2, message, wParam, lParam);
           break;
           }
           return 0;
           }

1 个答案:

答案 0 :(得分:1)

你说,

  

“我想要它做的是确定哪个窗口收到WM_DESTROY或WM_CLOSE”

简单的答案是,正在接收UINT message的窗口是HWND mainwin

如果您想知道“是的,但我的哪个窗口是HWND mainwin?”那么一种方法是,当你创建一个窗口时,你会记住它的窗口句柄是什么,并将它存储在某个地方(例如作为一个全局变量)。


除此之外,不清楚为什么要调用GetActiveWindow(),也不清楚要实现哪些功能。