打开后切换focus/activate另一个应用程序
Switch focus/activate another Application after opening it
我添加了“使用 Photoshop 编辑”按钮,并使用以下代码成功打开了带有文件的 Photoshop:
Type PhotoshopType = Type.GetTypeFromProgID("Photoshop.Application");
object PhotoshopInst = Activator.CreateInstance(PhotoshopType);
PhotoshopType.InvokeMember("Open", BindingFlags.InvokeMethod, null, PhotoshopInst, new object[1] { "c:\files\image.jpg" });
但我未能激活(聚焦)Photoshop,因此用户不必手动切换。
关于如何完成它的任何想法?
这通常是 UIAutomation 技术的 use-case。这是执行此操作的示例 C# 控制台应用程序:
dynamic instance = Activator.CreateInstance(Type.GetTypeFromProgID("Photoshop.Application"));
instance.Open(@"c:\files\image.jpg");
// add a COM reference to "UIAutomationClient"
// uncheck "Embed interop types" in the UIAutomationClient Reference properties (or redeclare the ids manually)
var uia = new UIAutomationClient.CUIAutomation();
var root = uia.GetRootElement();
// find Photoshop window (it's a top level window)
var ps = root.FindFirst(UIAutomationClient.TreeScope.TreeScope_Children,
uia.CreateAndCondition(
uia.CreatePropertyCondition(UIAutomationClient.UIA_PropertyIds.UIA_LocalizedControlTypePropertyId, "window"),
uia.CreatePropertyCondition(UIAutomationClient.UIA_PropertyIds.UIA_ClassNamePropertyId, "Photoshop")
)
);
if (ps != null)
{
// this is not to be confused with Win32's SetFocus API
// it does a lot more
ps.SetFocus();
}
要确定如何搜索 Photoshop,您可以使用 Inspect tool from Windows SDK,您将看到如下内容:
其中“本地化控件类型”为“window”,ClassName 为“Photoshop”。
我添加了“使用 Photoshop 编辑”按钮,并使用以下代码成功打开了带有文件的 Photoshop:
Type PhotoshopType = Type.GetTypeFromProgID("Photoshop.Application");
object PhotoshopInst = Activator.CreateInstance(PhotoshopType);
PhotoshopType.InvokeMember("Open", BindingFlags.InvokeMethod, null, PhotoshopInst, new object[1] { "c:\files\image.jpg" });
但我未能激活(聚焦)Photoshop,因此用户不必手动切换。 关于如何完成它的任何想法?
这通常是 UIAutomation 技术的 use-case。这是执行此操作的示例 C# 控制台应用程序:
dynamic instance = Activator.CreateInstance(Type.GetTypeFromProgID("Photoshop.Application"));
instance.Open(@"c:\files\image.jpg");
// add a COM reference to "UIAutomationClient"
// uncheck "Embed interop types" in the UIAutomationClient Reference properties (or redeclare the ids manually)
var uia = new UIAutomationClient.CUIAutomation();
var root = uia.GetRootElement();
// find Photoshop window (it's a top level window)
var ps = root.FindFirst(UIAutomationClient.TreeScope.TreeScope_Children,
uia.CreateAndCondition(
uia.CreatePropertyCondition(UIAutomationClient.UIA_PropertyIds.UIA_LocalizedControlTypePropertyId, "window"),
uia.CreatePropertyCondition(UIAutomationClient.UIA_PropertyIds.UIA_ClassNamePropertyId, "Photoshop")
)
);
if (ps != null)
{
// this is not to be confused with Win32's SetFocus API
// it does a lot more
ps.SetFocus();
}
要确定如何搜索 Photoshop,您可以使用 Inspect tool from Windows SDK,您将看到如下内容:
其中“本地化控件类型”为“window”,ClassName 为“Photoshop”。