本文介绍了Win32 防止窗口“卡住"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为我的应用程序禁用 Windows 7 的捕捉功能(以编程方式)?或者有什么方法可以检测应用程序是否已被捕捉,并专门调用 API 函数来取消捕捉?

How can I disable the snap feature of Windows 7 for my application (progmatically)? Or is there any way to detect if the application has been snapped, and specifically call an API function to unsnap it?

调用 SetWindowPos() 或 ShowWindow() 不能正确解开它*(SW_MAXIMIZE 可以).调用 SetWindowPos() 实际上会导致以后调用 SetWindowPos() 和 MoveWindow() 时出现奇怪的行为.同样的不一致不适用于最大化的窗口.

Calling SetWindowPos() or ShowWindow() does not unsnap it correctly *(SW_MAXIMIZE does). Calling SetWindowPos() actually causes strange behavior in future calls to SetWindowPos() and MoveWindow(). The same inconsistencies do not apply to a window that is maximized.

推荐答案

#define WM_RESTOREORIGINALSTYLE WM_USER+... /* your first free USER message */

case WM_SYSCOMMAND:
{
   if(wParam==(SC_MOVE|2)) wParam=SC_SIZE|9;
   if((wParam&0xFFE0)==SC_SIZE && (wParam&0x000F)) // handles MOVE and SIZE in one "if"
   {
      long int oldStyle=GetWindowLongW(hwnd,GWL_STYLE);
      PostMessageW(hwnd,WM_RESTOREORIGINALSTYLE,GWL_STYLE,oldStyle);
      SetWindowLongW(hwnd,GWL_STYLE,oldStyle &0xFEFEFFFF); // disable WS_MAXIMIZE and WS_MAXIMIZEBOX
      DefWindowProcW(hwnd,WM_SYSCOMMAND,wParam,lParam);
      return 0;
   }
   return DefWindowProcW(hwnd,WM_SYSCOMMAND,wParam,lParam);
}
case WM_RESTOREORIGINALSTYLE:
{
   if((long int)wParam==GWL_STYLE)
      SetWindowLongW(hwnd,GWL_STYLE,lParam);
   return 0;
}

PostMessage 将在随后的消息循环中处理 - 这意味着进入移动大小循环后尽快.如果您使用自己的框架绘制方法,请不要忘记在 WM_STYLECHANGED 消息中正确重绘您的框架,内部将 oldStyle 存储在您的类中.为什么有效?Windows 在移动/大小操作开始时检查捕捉条件.如果 WS_MAXIMIZEWS_MAXIMIZEBOX 在开始时被禁用,则捕捉行为被禁用.

The PostMessage will be processed in subsequent message loop - it means ASAP after entering into move-size loop.If you use own drawing method of frame, please do not forget to redraw your frame correctly on WM_STYLECHANGED message, internally store oldStyle in your class.Why it works? Windows check snap condition at start of move/size action. If WS_MAXIMIZE and WS_MAXIMIZEBOX are disabled at start, the snap behaviour is disabled.

SC_SIZE|9 等价于 SC_MOVE|2 没有阻塞重绘半秒.

The SC_SIZE|9 is equivalent of SC_MOVE|2 without blocking redrawing for half a second.

如果你不想在完全最大化的情况下启用拖动最大化窗口,请检查系统菜单中SC_MOVE项的状态,如果启用,则在WM_SYSCOMMAND中直接返回0.

If you don't want to enable dragging maximized windows if they are fully maximized, check state of SC_MOVE item in system menu and if it is enabled, directly return 0 in WM_SYSCOMMAND.

已在 Windows 8.1 上验证.

Verified on Windows 8.1.

这篇关于Win32 防止窗口“卡住"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 08:00