I want to open it in Photoshop when the picture is clicked next to it in my application. I used the following code for this:
// var arg = """ + CurretFolder + "\" + CurrentPicture + """;
var arg = ""D:\pictures\test_01.jpg"";
Process.Start("photoshop.exe", arg);
However, when the image is opened in PhotoShop, I want to keep using my application in the foreground as an active form and I want to continue working with shortcuts etc. I tried something like this:
var prg = Process.GetProcesses().FirstOrDefault(x => x.ProcessName.Contains("MyProgram"));
// var arg = """ + CurretFolder + "\" + CurrentPicture + """;
var arg = ""D:\pictures\test_01.jpg"";
Process.Start("photoshop.exe", arg);
TopMost = true;
prg?.Refresh();
// 🤮
Activate();
Focus();
Select();
But it did not happen. How can I do that?
2
Answers
Process.Start
is going to return well before the application window for that process is displayed, so anything you do immediately after that call will be overridden by what happens later. The first thing to try is to callWaitForInputIdle
on yourProcess
object. That will block until the application reports to Windows that it is ready to receive input, which will be after the application window has been displayed. This will not work for all applications but I suspect that it will for Photoshop.Note that
Activate
is the one and only method you call to activate a form. I understand that you were probably throwing mud at the wall but, ifActivate
doesn’t work, nothing else will. That especially goes forTopMost
, which has nothing at all to do with focus.The simplest way is to force your form to be activated, so call
Activate
in aDeactivate
event handler:Of course, besides Photoshop, other windows will also be affected. You can use the Windows APIs
GetForegroundWindow
andGetWindowThreadProcessId
to filter activated windows.BTW, I don’t know why
Activate
doesn’t always succeed in testing, but it seems to work better when combined withTopMost = true