skip to Main Content

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


  1. 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 call WaitForInputIdle on your Process 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.

    Process.Start("photoshop.exe", arg).WaitForInputIdle();
    
    Activate();
    

    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, if Activate doesn’t work, nothing else will. That especially goes for TopMost, which has nothing at all to do with focus.

    Login or Signup to reply.
  2. The simplest way is to force your form to be activated, so call Activate in a Deactivate event handler:

    private void Form1_Deactivate(object sender, EventArgs e)
        => Activate();
    

    Of course, besides Photoshop, other windows will also be affected. You can use the Windows APIs GetForegroundWindow and GetWindowThreadProcessId to filter activated windows.

    private void Form1_Deactivate(object sender, EventArgs e)
    {
        GetWindowThreadProcessId(GetForegroundWindow(), out int processId);
        var p = Process.GetProcessById(processId);
        if(p != null && p.MainModule.FileName.EndsWith("photoshop.exe", StringComparison.OrdinalIgnoreCase))
            Activate();
    }
    

    BTW, I don’t know why Activate doesn’t always succeed in testing, but it seems to work better when combined with TopMost = true

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search