使用辅助功能API设置最前面的窗口

使用辅助功能API设置最前面的窗口

本文介绍了使用辅助功能API设置最前面的窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将外部应用程序(例如textedit)中的某个窗口设置为最前面.

I want to set a certain window, from an external app (for example textedit), to be front most.

我可以使用GetFrontProcess成功获得对应用程序本身的引用,并检查它是否位于最前面.如果不是,我可以使用setFrontProcess使其聚焦.

I can successfully get a reference to the app itself using GetFrontProcess, and check whether it is front most. If it is not, I can use setFrontProcess to make it focussed.

然后,我可以使用可访问性API来检查该应用程序下的所有窗口.我正在检查是否存在某个窗口,如果有,则将其与应用程序的最前面的窗口进行比较:

I can then use the the accessibility API to examine all the windows under that application. I am checking that a certain window exists, and if so I compare it against the front most window of the application:

//get the front window of textEditApp and store it in 'currentFrontWindow'
    AXUIElementCopyAttributeValue(textEditApp, kAXFocusedWindowAttribute, (CFTypeRef *)&currentFrontWindow);

如果我感兴趣的窗口不在最前面,则需要设置它.我以为可以使用AXUIElement Set AttributeValue来执行此操作,但是我没有获得任何成功.以下是我尝试执行的操作.

If the window I am interested in is not frontmost, I need to set it so. I thought that I could use AXUIElement Set AttributeValue to do this but I am not getting any success. Below is how I have tried to do it.

//set the front window of textEditApp to be desiredFrontWindow
AXUIElementSetAttributeValue(textEditApp, kAXFocusedUIElementAttribute, desiredFrontWindow);

我已经检查了该窗口是否存在,并且该应用程序已成功切换到".但是为什么这行代码不将指定的窗口置于最前面呢?

I have checked that the window exists, and the application is 'switched to' successfully. But why doesn't this line of code bring the specified window to the front?

谢谢.

推荐答案

因为您尝试设置只读属性.

Because you tried to set a read-only attribute.

为了使窗口位于最前面,您需要设置窗口的适当属性.应用程序也是如此:要使应用程序位于最前面,您需要设置应用程序的适当属性.

In order to make a window frontmost, you need to set the appropriate property of the window. The same goes for an application: To make an application frontmost, you need to set the appropriate property of the application.

最前面的窗口的Cocoa/Mac OS X名称是主窗口".(例如,请参见 NSApplication的与该概念有关的NSWindow方法.)可访问性使用相同的名称,因此要使单个窗口位于最前面,请设置 kAXMainAttribute 的值更改为 kCFBooleanTrue .

The Cocoa/Mac OS X name for the frontmost window is "the main window". (See, for example, NSApplication's and NSWindow's methods relating to that concept.) Accessibility uses the same name, so to make a single window frontmost, set the value of its kAXMainAttribute to kCFBooleanTrue.

使应用程序位于最前面的方法类似:将其 kAXFrontmostAttribute 的值设置为 kCFBooleanTrue .您需要同时执行这两项操作,才能设置应用程序的最前面的窗口并使应用程序处于活动状态.

The way to make the application frontmost is similar: Set the value of its kAXFrontmostAttribute to kCFBooleanTrue. You'll need to do both of these to both set the frontmost window of the application and make the application active.

据我所知,没有办法仅将应用程序的单个窗口置于最前面并给予其会话焦点.

As far as I can tell, there is no way to bring only a single window of an application frontmost and give it session focus.

这篇关于使用辅助功能API设置最前面的窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 10:01