BringWindowToTop、SetForegroundwindow、SetWindowPos 等有什么区别? [英] What are the differences between BringWindowToTop, SetForegroundwindow, SetWindowPos etc.?

查看:34
本文介绍了BringWindowToTop、SetForegroundwindow、SetWindowPos 等有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

BringWindowToTop, SetForegroundWindowSwitchToThisWindow, SetWindowPos, ShowWindow 都被描述为显示和激活一个窗口.

BringWindowToTop, SetForegroundWindow, SwitchToThisWindow, SetWindowPos, ShowWindow all are described as showing and activating a window.

它们之间的真正区别是什么?什么时候以及为什么 BringWindowToTopSetForegroundWindowSwitchToThisWindow 或什至 SetWindowPos 更受欢迎,并将标志设置为激活和显示?

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?

推荐答案

涉及到许多密切相关的概念,相关术语经常被误用,即使在官方文档中也是如此.

There are many closely-related concepts involved, and related terms are often misused, even in the official documentation.

重要的窗口类型:

  • 顶级窗口:没有父窗口的窗口.应用程序的主窗口几乎总是顶级窗口.它与 z-order 无关.

  • 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.

子窗口:父窗口包含的窗口.它们的位置总是相对于父窗口的区域.子窗口通常是控件":按钮和编辑框等 UI 事物.

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.

所有者/拥有关系和父/子关系之间的区别通常并不重要,因此父子术语通常用于这两种上下文,即使在文档中也是如此.在某些情况下,父字段和参数被重载以表示父和/或所有者.

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.

重要概念:

  • z 顺序的顶部:字面意思是显示在其他窗口上方的窗口.

  • top of the z-order: This literally means the window that displays above other windows.

活动窗口:一个模糊的概念,但它通常表示用户会认为是当前"窗口的顶级窗口.活动窗口通常用独特的边框绘制,并突出显示其在任务栏上的图块.在所有其他顶级窗口中,活动窗口通常位于或接近 z 顺序的顶部,并且它是具有键盘焦点的窗口的父级或所有者(可能是间接的).

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.

前景:活动窗口通常位于前景中.这个名字似乎暗示它位于 z 顺序的顶部,但它实际上意味着创建窗口的线程获得了轻微的优先级提升.该活动窗口通常也是前景窗口.

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-order 的顶部、突出显示其边框等来激活".它也变成了前景窗口,因此它的线程得到了一点提升,使 UI 比任何其他都更具响应性窗户.

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.

记住这些术语后,您可以解析您列出的函数的 MSDN 描述,以梳理出细微的差异.

With these terms in mind, you can parse the MSDN descriptions for the functions you listed to tease out the subtle differences.

如果您尝试布置窗口的子项,只需使用 SetWindowPos(或 MoveWindow、SizeWindow 和 ShowWinow).在其余函数中,SwitchToThisWindow 看起来已被弃用,并且与 SetForegroundWindow 基本相同.(请注意,在许多情况下,除非您是活动应用程序或活动应用程序已授予您使用它的权限,否则 SetForegroundWindow 不会执行您想要的操作.)BringWindowToTop 主要是将窗口带到 z- 的顶部顺序(您可以使用 SetWindowPos 执行),如果您在顶级窗口上调用它,则具有额外的副作用,使其表现得像 SetForegroundWindow.

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.

更新: Raymond Chen 发布了一个更清晰活动窗口和前景窗口的区别.引用:

Update: Raymond Chen posted a clearer distinction between the active window and the foreground window. To quote:

前台窗口的概念是在输入不同步时引入的,目的是表达真正的全局活动窗口",而不是 SetActiveWindow,后者继续引用本地活动窗口.

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.

这篇关于BringWindowToTop、SetForegroundwindow、SetWindowPos 等有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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