Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

programmatically minimize an application using c#

Tags:

c#

minimize

I have the following code snippet

List<String> sensitiveApps = testConnection.SelectSensitive();

foreach (string sensitiveApp in sensitiveApps)
        {
            Console.Write(sensitiveApp);

            // retrieve applications to minimize handle (connect to database and systematically minimize all applications?)
            IntPtr hwnd = UnsafeNativeMethods.FindWindow(sensitiveApp, null);
            StringBuilder stringBuilder = new StringBuilder(256);
            UnsafeNativeMethods.GetWindowText(hwnd, stringBuilder, stringBuilder.Capacity);
            Console.WriteLine(stringBuilder.ToString());

            if (!hwnd.Equals(IntPtr.Zero))
            {
                // SW_SHOWMAXIMIZED to maximize the window
                // SW_SHOWMINIMIZED to minimize the window
                // SW_SHOWNORMAL to make the window be normal size
                ShowWindowAsync(hwnd, SW_SHOWMINIMIZED);
            }
        }

Where sensitiveApps is a list containing the strings "Notepad", "Recuva" and "VLC media player 2.0.3".

However, the only application that can be minimized using this code is Notepad. Debugging the program finds that

Console.WriteLine(stringBuilder.ToString());

would not return any value for the last 2 programs, but would return a Untitled - Notepad.

Is there anything I'm doing wrong?

like image 242
David Ten Avatar asked May 28 '26 15:05

David Ten


1 Answers

Try using Spy++ and check the FindWindow names are correct.

MS Word is OpusApp and VLC is QWidget.

like image 54
Jeremy Thompson Avatar answered May 30 '26 03:05

Jeremy Thompson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!