Home > Net >  Possible to start process, minimize window, on click open same window process? [duplicate]
Possible to start process, minimize window, on click open same window process? [duplicate]

Time:10-07

I have a fullscreen application, that is hiding Windows own taskbar (as it should). I have a button method where i start IE process

 Process.Start(pathWayToIE);

Which works fine, but when minimizing that window, I can't access it again, since the taskbar is hiding, as i want it to do. Is there a way to find the same process and maximize that window on the same button click, instead of open a new window of IE?

I do have this statement to see if a window is already open

var proc = Process.GetProcesses();
            

            for(var i = 0; i < proc.Length;   i)
            {
                if(proc[i].ProcessName == "msedge")
                {
                    appName = proc[i].ProcessName;
                    appId = proc[i].Id;
                }
              
            }
int count = Process.GetProcesses().Where(p => p.ProcessName == appName).Count();

if (count > 1)
            {
                MessageBox.Show("A window is already open!");
            }

So to see if a window is actually open works, but how to maximize it again i struggle with.

The whole method looks like this:

private void IE_Button_Click(object sender, RoutedEventArgs e)
        {

            var startEdge = @"pathwayToIE";
            
            var appName = "";
            var appId = 0;

            var proc = Process.GetProcesses();
            

            for(var i = 0; i < proc.Length;   i)
            {
                if(proc[i].ProcessName == "msedge")
                {
                    appName = proc[i].ProcessName;
                    appId = proc[i].Id;
                }
              
            }

            Process myEdgeProcess = new Process();
            ProcessStartInfo procStartInfo = new ProcessStartInfo(startEdge);

            myEdgeProcess.StartInfo.FileName = startEdge;

            int count = Process.GetProcesses().Where(p => p.ProcessName == appName).Count();

            int currentRunningInstance = Process.GetProcesses().Where(c => c.Id == appId).Count();

            if (count > 1)
            {
                MessageBox.Show("A window is already open!");
            }
            else
            {
              
                Process.Start(procStartInfo);
              
            }

         }

Thanks in advance for tips

CodePudding user response:

You can maximize process window through P/Invoke ShowWindow method. Add this code to your class:

const int SW_SHOWMAXIMIZED = 3;

[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

You need to get process handle, which could be achived like:

IntPtr handle = Process.GetProcesses()
                       .FirstOrDefault(p => p.ProcessName == "YourProcessName")
                       .MainWindowHandle;

Then call ShowWindow with passing handle and const value to it:

ShowWindow(handle, SW_SHOWMAXIMIZED);

Complete:

static void Main(string[] args)
{
    Process yourProcess = Process.GetProcesses()
                                 .FirstOrDefault(p => p.ProcessName == "Viber");
    IntPtr handle = yourProcess.MainWindowHandle;

    ShowWindow(handle, SW_SHOWMAXIMIZED);

    Console.WriteLine("Process "   yourProcess.ProcessName   " window maximized!");
    Console.ReadKey();
}

About ShowWindow you can read here: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow

  • Related