windows BringWindowToTop、SetForegroundwindow、SetWindowPos 等有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1544179/
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
What are the differences between BringWindowToTop, SetForegroundwindow, SetWindowPos etc.?
提问by Chris Becke
BringWindowToTop
, SetForegroundWindow
, SwitchToThisWindow
, SetWindowPos
, ShowWindow
all are described as showing and activating a window.
BringWindowToTop
, SetForegroundWindow
, SwitchToThisWindow
, SetWindowPos
,ShowWindow
都被描述为显示和激活一个窗口。
What are the real differences between them? When and why would BringWindowToTop
be preferred to SetForegroundWindow
, or SwitchToThisWindow
or even SetWindowPos
with the flags set to activate and show?
它们之间的真正区别是什么?什么时候以及为什么会BringWindowToTop
更喜欢SetForegroundWindow
,或者SwitchToThisWindow
甚至SetWindowPos
将标志设置为激活和显示?
回答by Adrian McCarthy
There are many closely-related concepts involved, and related terms are often misused, even in the official documentation.
涉及到许多密切相关的概念,相关术语经常被滥用,即使在官方文档中也是如此。
Important types of windows:
重要的窗户类型:
top-level windows: Windows that have no parent window. The main window for an application is almost always a top-level window. It does not have anything to do with z-order.
child windows: Windows that are contained by a parent window. Their position is always relative to the parent window's area. Child windows are often "controls": UI things like buttons and edit boxes.
parent windows: Windows that have child windows. Top-level windows often have children. But note that child windows may also have children and thus be both parent and child windows.
owned windows: Windows that are controlled by a another window, but aren't necessarily children of the other window. An example is a floating tool palette: it's owned by another window in the application, but it's not locked to that other window's area.
owner windows: Windows that own an owned window.
顶级窗口:没有父窗口的窗口。应用程序的主窗口几乎总是顶级窗口。它与 z-order 没有任何关系。
子窗口:父窗口包含的窗口。它们的位置总是相对于父窗口的区域。子窗口通常是“控件”:诸如按钮和编辑框之类的 UI 东西。
父窗口:具有子窗口的窗口。顶层窗口通常有子级。但请注意,子窗口也可能有子窗口,因此既是父窗口又是子窗口。
拥有的窗口:由另一个窗口控制的窗口,但不一定是另一个窗口的子窗口。一个例子是浮动工具选项板:它由应用程序中的另一个窗口拥有,但它没有锁定到另一个窗口的区域。
owner windows: 拥有自有窗口的 Windows。
Often the distinction between an owner/owned relationship and a parent/child relationship isn't important, so the parent and child terms are often used for both contexts, even in documentation. In some cases, parent fields and parameters are overloaded to mean parent and/or owner.
通常所有者/拥有关系和父/子关系之间的区别并不重要,因此父和子术语通常用于这两种上下文,即使在文档中也是如此。在某些情况下,父字段和参数被重载以表示父和/或所有者。
Important concepts:
重要概念:
top of the z-order: This literally means the window that displays above other windows.
active window: A fuzzy concept, but it typically means the top-level window the user would consider the "current" window. The active window is typically drawn with a distinctive border and its tile on the task bar is highlighted. The active window is usually at or near to the top of the z-order among all other top-level windows, and it is the parent or owner (perhaps indirectly) of the window with keyboard focus.
keyboard focus: Indicates the window that will receive the keyboard messages. Conceptually, there is one window with keyboard focus. Often the window with focus is a child (or grandchild, etc.) of the active window.
foreground: The active window is typically in the foreground. The name seems to suggest that it's at the top of the z-order, but it really means that the thread that created the window gets a slight priority boost. That active window is usually also the foreground window.
z 顺序的顶部:字面意思是显示在其他窗口上方的窗口。
活动窗口:一个模糊的概念,但它通常意味着用户会认为“当前”窗口的顶级窗口。活动窗口通常用独特的边框绘制,并突出显示其在任务栏上的图块。在所有其他顶级窗口中,活动窗口通常位于或接近 z 顺序的顶部,并且它是具有键盘焦点的窗口的父级或所有者(可能是间接的)。
键盘焦点:指示将接收键盘消息的窗口。从概念上讲,有一个带有键盘焦点的窗口。通常具有焦点的窗口是活动窗口的子窗口(或孙窗口等)。
foreground:活动窗口通常位于前台。这个名字似乎暗示它位于 z 顺序的顶部,但它实际上意味着创建窗口的线程获得了轻微的优先级提升。该活动窗口通常也是前景窗口。
So let's say you've got this browser window open, and you've also got an instance of Notepad running. If you click on the document in Notepad, a whole flurry of messages and state changes occur. You're actually clicking on a big edit box, which is a child window of Notepad's top-level window. That click causes the edit box to get activated, but child windows can't really be the "active" window, so it just takes the keyboard focus and passes the activation message up through its ancestors until it gets to a top-level window. The top-level window "activates" by moving to the top of the z-order, highlighting its border, etc. It also becomes the foreground window, so its thread gets a little boost to make the UI a little more responsive than any other windows.
因此,假设您打开了这个浏览器窗口,并且还运行了一个记事本实例。如果您在记事本中单击文档,则会发生一连串的消息和状态更改。您实际上是在单击一个大编辑框,它是记事本顶级窗口的子窗口。该点击会导致编辑框被激活,但子窗口不能真正成为“活动”窗口,因此它只是获取键盘焦点并将激活消息向上传递到其祖先,直到它到达顶级窗口。顶层窗口通过移动到 z-order 的顶部、突出显示其边框等来“激活”。它也成为前景窗口,因此它的线程得到了一点提升,使 UI 比任何其他窗口更具响应性视窗。
With these terms in mind, you can parse the MSDN descriptions for the functions you listed to tease out the subtle differences.
记住这些术语后,您可以解析您列出的函数的 MSDN 描述以梳理出细微的差异。
If you're trying to lay out your window's children, just use SetWindowPos (or MoveWindow, SizeWindow, and ShowWinow). Of the remaining functions, SwitchToThisWindow looks deprecated and essentially the same as SetForegroundWindow. (Note that, in many cases, SetForegroundWindow won't do what you want unless you're the active application or the active application has given you permission to use it.) BringWindowToTop is mostly about bringing a window to the top of the z-order (which you can do with SetWindowPos), with extra side effects that make it behave like SetForegroundWindow if you call it on a top-level window.
如果您尝试布置窗口的子项,只需使用 SetWindowPos(或 MoveWindow、SizeWindow 和 ShowWinow)。在其余函数中,SwitchToThisWindow 看起来已被弃用,并且与 SetForegroundWindow 基本相同。(请注意,在许多情况下,除非您是活动应用程序或活动应用程序已授予您使用它的权限,否则 SetForegroundWindow 不会执行您想要的操作。)BringWindowToTop 主要是将窗口带到 z- 的顶部顺序(您可以使用 SetWindowPos 执行),如果您在顶级窗口上调用它,则具有额外的副作用,使其表现得像 SetForegroundWindow。
Update:Raymond Chen posted a clearer distinction between the active window and the foreground window. To quote:
更新:Raymond Chen在活动窗口和前景窗口之间发布了更清晰的区别。报价:
The concept of the foreground window was introduced when input was desynchronized in order to express the "really global active window", as opposed to Set-Active-Window, which continued to refer to the local active window.
前台窗口的概念是在输入不同步时引入的,以表达“真正的全局活动窗口”,而不是 Set-Active-Window,后者继续引用本地活动窗口。
回答by Don Dickinson
Use setwindowpos
if you need to change the size of the window (not just its state)
使用setwindowpos
,如果你需要改变窗口的大小(不只是它的状态)
Use showwindow
to change just the state of the window
用于showwindow
仅更改窗口的状态
Use bringwindowtotop
to activate a parent window via the child. if you send it a child window (perhaps a floating toolbar) the parent will be brought to front and have focus instead of the child.
用于bringwindowtotop
通过子窗口激活父窗口。如果你给它发送一个子窗口(可能是一个浮动工具栏),父窗口将被带到前面并获得焦点而不是子窗口。
They all have their place and obviously have duplicate functionality, but each does things just a little different depending on what you want to do.
它们都有自己的位置并且显然具有重复的功能,但是根据您想要做的事情,每个做的事情都略有不同。