Switch focus/activate another Application after opening it
See the question and my original answer on StackOverflowThis 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"));
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();
}
To determine how to search for Photoshop, you can use the Inspect tool from Windows SDK and you will see something like this:
Where the "localized control type" is "window" and the ClassName" is "Photoshop".