windows 使用 SetWindowPos 移动窗口是“正常方式”吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4631706/
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
Is moving a window with SetWindowPos the 'normal way' to do?
提问by René Nyffenegger
I am wondering if, in order to move a (ms-windows-)window with the Win32 API 20 px to the right and 40 px downwards, the following function call would be how to do it:
我想知道,为了将带有 Win32 API 的 (ms-windows-) 窗口向右移动 20 像素,向下移动 40 像素,以下函数调用是如何执行的:
SetWindowPos(
/* hWnd */ hChildDlg2,
/* hWndInsertAfter */ (HWND) -1,
/* X */ 20,
/* Y */ 40,
/* cx */ -1,
/* cy */ -1,
/* uFlags */ SWP_NOSIZE | // Ignore cx, cy
SWP_NOZORDER // and hWndInsertAfter
);
I ask because it seems to me that there could be a function only taking a HWND
and an x
and y
as parameters.
我问是因为在我看来可能有一个函数只接受 aHWND
和 an x
andy
作为参数。
采纳答案by Anders
Yes, this is the normal way and the window will get a WM_WINDOWPOSCHANGING
message (with the parameters that changed) There is also the older MoveWindow
but it is less flexible and actually forces you to set the size.
是的,这是正常方式,窗口会收到一条WM_WINDOWPOSCHANGING
消息(参数已更改)还有较旧的方式,MoveWindow
但灵活性较差,实际上会强制您设置大小。
To properly save and restore window sizes you should use GetWindowPlacement
and SetWindowPlacement
respectively.
要正确保存和恢复窗口大小,您应该分别使用GetWindowPlacement
和SetWindowPlacement
。
回答by In silico
Yes, that's pretty much how it's done. You should prefer to use SetWindowPos()
since it gives you quite a bit of control over how the window should be moved/resized.
是的,这几乎就是这样做的。您应该更喜欢使用,SetWindowPos()
因为它可以让您对如何移动/调整窗口大小进行相当多的控制。
I typically use it like this (part of a small framework I wrote):
我通常这样使用它(我写的一个小框架的一部分):
// Window member function
void Window::Move(int x, int y)
{
if(hwnd != 0)
{
::SetWindowPos(hwnd, 0, x, y, 0, 0,
SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
}
}
There's also a MoveWindow()
function that does pretty much the same thing. With the SetWindowPos()
function available, it's now more of a convenience function than anything else.
还有一个MoveWindow()
功能几乎可以做同样的事情。有了SetWindowPos()
可用的功能,它现在更像是一个方便的功能,而不是其他任何东西。
回答by Ben Voigt
You mean like MoveWindow
?
你是说喜欢MoveWindow
?
It takes hwnd, x, y, width, height, since there's no SWP_NOSIZE
flag, it's actually more complicated to use it to just move the window, since you also have to fetch the size.
它需要 hwnd, x, y, width, height,因为没有SWP_NOSIZE
标志,使用它来移动窗口实际上更复杂,因为你还必须获取大小。