windows 如何暂时防止窗口调整大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1825868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:31:36  来源:igfitidea点击:

How to prevent window resizing temporarily?

c++windowswinapiresize

提问by Suma

I have a window which can be resized, but there are some situations when resizing is not possible because of the application state. Is there a way to prevent resizing the window temporarily?

我有一个可以调整大小的窗口,但是在某些情况下,由于应用程序状态而无法调整大小。有没有办法暂时防止调整窗口大小?

I want to disable resizing by all means available to the users, which include window menu, dragging edges by mouse, user initiated window tiling performed by OS - and perhaps some other I am not aware of?

我想通过用户可用的所有方式禁用调整大小,包括窗口菜单、通过鼠标拖动边缘、由操作系统执行的用户启动的窗口平铺 - 也许还有其他一些我不知道的?

回答by Anders

To retain the look of the window border and still prevent re-size (and cursor change), catch WM_NCHITTEST, pass it to DefWindowProc, if the returned code is one of the size constants, change the real return to something else, HTCLIENT for example

为了保留窗口边框的外观并仍然防止重新调整大小(和光标更改),catch WM_NCHITTEST,将其传递给 DefWindowProc,如果返回的代码是大小常量之一,则将实际返回值更改为其他内容,例如 HTCLIENT

回答by Nick Dandoulakis

One way is to use GetWindowLong()with GWL_STYLEflag to get the window style and
reset/remove any stylesyou need, ie the WS_THICKFRAMEstyle so that the window can't be resized.

一种方法是使用带有标志的GetWindowLong()GWL_STYLE来获取窗口样式并
重置/删除您需要的任何样式,即WS_THICKFRAME无法调整窗口大小的样式。

You apply the new style with SetWindowLong.

您可以使用SetWindowLong应用新样式

回答by Stefan

Another possibility is to handle the WM_GETMINMAXINFOmessage and set the MINMAXINFO struct so that both min and max size of the window is the current size. Then the user can't resize the window either.

另一种可能性是处理WM_GETMINMAXINFO消息并设置 MINMAXINFO 结构,以便窗口的最小和最大大小都是当前大小。然后用户也不能调整窗口大小。

回答by Suma

Following code in the window procedure seems to handle the case of user dragging the window edge/corner:

窗口过程中的以下代码似乎处理用户拖动窗口边缘/角落的情况:

case WM_SIZING:
    RECT &rc = *(LPRECT) lParam;
    RECT windowRect;
    GetWindowRect(hwnd, &windowRect);
    rc = windowRect;
    return 0;

I did not find anything yet to prevent the system from resizing the window when tiling/cascading windows. I hoped following might do the trick, but it seems it does not:

我还没有发现任何东西可以阻止系统在平铺/级联窗口时调整窗口大小。我希望以下内容可以解决问题,但似乎没有:

case WM_SIZE:
   return TRUE;

I guess I can find similar measure for other cases, but at least I would need to know the exhaustive list of messages which can result in a window changing its size.

我想我可以为其他情况找到类似的度量,但至少我需要知道可能导致窗口改变其大小的详尽消息列表。

Also, while this really prevents the window from resizing, I would rather prevent the user from even initiating the resize, than apparently letting him to resize and then refusing to do so.

此外,虽然这确实阻止了窗口调整大小,但我宁愿阻止用户甚至启动调整大小,也不愿让他调整大小然后拒绝这样做。