[FIXED] Switch Focus/Aktivieren Sie eine andere Anwendung nach dem Öffnen

Ausgabe

Ich habe eine Schaltfläche „Mit Photoshop bearbeiten“ hinzugefügt und Photoshop erfolgreich mit einer Datei mit diesem Code geöffnet:

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

Aber ich kann Photoshop nicht aktivieren (Fokus geben), damit der Benutzer nicht manuell wechseln muss. Irgendeine Idee, wie man es bewerkstelligen kann?

Lösung

Dies ist typischerweise ein Anwendungsfall für die UIAutomation -Technologie. Hier ist eine Beispiel-C#-Konsolen-App, die dies tut:

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

Um festzustellen, wie nach Photoshop gesucht werden soll, können Sie das Inspect-Tool von Windows SDK verwenden , und Sie werden so etwas sehen:

Geben Sie hier die Bildbeschreibung ein

Wobei der „lokalisierte Steuerungstyp“ „Fenster“ und „Klassenname“ „Photoshop“ ist.


Beantwortet von –
Simon Mourier


Antwort geprüft von –
Jay B. (FixError Admin)

0 Shares:
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like