Switch focus/activate another Application after opening it


I added an "Edit with Photoshop" button and succeeded opening Photoshop with a file using this code:

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" });  

But I am failing to activate (give focus to) Photoshop so the user does not have to switch manually.
Any idea on how to accomplish it?


This is typically a use-case for the UIAutomation technology. Here is a sample C# console app that does this:

dynamic instance = Activator.CreateInstance(Type.GetTypeFromProgID("Photoshop.Application"));

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

To determine how to search for Photoshop, you can use the Inspect tool from Windows SDK and you will see something like this:

enter image description here

Where the "localized control type" is "window" and the ClassName" is "Photoshop".

Answered By – Simon Mourier

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published