-1

我一直在尝试让 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;
           }
4

1 回答 1

1

你说,

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

简单的答案是接收的窗口UINT messageHWND mainwin.

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


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

于 2012-12-06T13:20:25.197 回答