[WinAPI]当用户最小化窗口上的按钮时出现陷阱 [英] [WinAPI] Trap when user minimize button on window

查看:58
本文介绍了[WinAPI]当用户最小化窗口上的按钮时出现陷阱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

我有一个带有WindowProc的窗口,我希望当用户单击最小化按钮(标题栏)时,我的窗口将被隐藏而不是最小化.在WindowProc中:

Dear all,

I have a window with WindowProc, I want that when user click on minimize button (title bar), My window will be hided instead of minimize. In WindowProc:

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_SYSCOMMAND:
           switch(wParam)
          {
                  case SC_MINIMIZE:
                           ShowWindow(hwnd, SW_HIDE); // <== Hide window instead of minimize
                          break;
          }
          break;
        
    case WM_DESTROY:
        PostQuitMessage (0);
        return (0);
    }
    return DefWindowProc (hwnd, message, wParam, lParam);
}



当我单击最小化按钮(它将隐藏)时,它可以很好地工作,但是我无法移动或最大化或关闭窗口.发生什么事了?

您有什么意见吗?

谢谢您.



It works well when I click on minimize button (it will hide), but I cannot move or maximize or close window. What''s happen??

Do you have any comment?

Thank you

推荐答案

我相信您发布的代码中有错字.如果我将该代码作为窗口proc运行,则一切正常,因为始终执行的最后一件事是 DefWindowProc .


我相信您看到的问题是由于未处理 WM_SYSCOMMAND 情况下的 DefWindowProc 引起的.

将上面发布的代码更改为以下代码,它应该可以满足您的要求.我在两个switch语句中都放置了 default 处理程序,以确保正确调用 DefWindowProc :
I believe there is a typo in your code as posted. If I run that code as my window proc, everything works properly because the last thing that is always performed is DefWindowProc.


I believe the problem you are seeing is caused by not processing the DefWindowProc in the WM_SYSCOMMAND case.

Change the code posted above to the following and it should do what you are asking for. I have placed a default handler in both of your switch statements to make sure DefWindowProc is called appropriately:
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
  LRESULT lResult = 0;
  switch (message)
  {
  case WM_SYSCOMMAND:
    switch(wParam)
    {
    case SC_MINIMIZE:
      ShowWindow(hwnd, SW_HIDE); // <== Hide window instead of minimize
      break;
    default:
      lResult = DefWindowProc (hwnd, message, wParam, lParam);
    }

    break;

  case WM_DESTROY:
    PostQuitMessage (0);
    return (0);
  default:
    lResult = DefWindowProc (hwnd, message, wParam, lParam);
  }

  return lResult;
}




这样的事情会导致您描述的问题:




Something like this would cause the issue you are describing:

switch(wParam)
case WM_SYSCOMMAND:
  switch (message)
  {
  case SC_MINIMIZE:
    ShowWindow(hwnd, SW_HIDE); // <== Hide window instead of minimize
    break;
  default:
    // If you don't call DefWindowProc for the other WM_SYSCOMMAND messages, 
    // Those modes will cease to function.
    // lResult = DefWindowProc (hwnd, message, wParam, lParam);
    return 0;
  }



问候,
保罗



Regards,
Paul





根据人们的实验,我在此msdn页面的详细评论中了解到,建议使用
Hi,


I''ve learned on peple comments of this msdn page that it is recommanded to use
ShowWindowAsync

代替

ShowWindow

:

ShowWindow msdn引用 [ ^ ]

他们还描述了尝试手动打开窗口前景时发生的意外行为,他们建议使用

, according to people experiments:

ShowWindow msdn ref[^]

They also described an unexspected behavior when trying to bring the window foreground manually, they recommand to make it programmatically using

SetForegroundWindow(hwnd)


以编程方式使其变为前景 查看人们根据其使用此功能的评论,这对您很有趣.

最好的问候.
EL GAABEB.


Take a look at people comments according to their use of this function, it is interesting for you.

Best regards.
EL GAABEB.


您需要处理其他WM_SYSCOMMAND消息.编写带有跟踪语句的默认值以查看它们...
you need to handle these other WM_SYSCOMMAND messages. Write a default with trace statements to see them...


这篇关于[WinAPI]当用户最小化窗口上的按钮时出现陷阱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆