Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on C# code which minimize all open instances of Notepad. This code works fine on Windows 10, but when I run the same exe with administrator privilege, it minimize only one instance of Notepad (out of open 3..).

My code for this is as follows. any help is greatly appreciated. Thanks

What I have tried:

C#
class Program
{
    private const int SW_SHOWNORMAL = 1;
    private const int SW_SHOWMINIMIZED = 2;
    private const int SW_SHOWMAXIMIZED = 3;

    [DllImport("user32.dll")]
    private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);

    [DllImport("user32.dll")]
    private static extern bool MinimizeWindow(System.IntPtr hwnd);

    static void Main(string[] args)
    {
        System.Diagnostics.Process thisProcess =
        System.Diagnostics.Process.GetCurrentProcess();
        System.Diagnostics.Process[] processes =
        System.Diagnostics.Process.GetProcessesByName("notepad");

        foreach (System.Diagnostics.Process process in processes)
        {
            if (process == thisProcess) 
                continue;

            System.IntPtr handle = process.MainWindowHandle;

            if (handle == System.IntPtr.Zero) 
                continue;

            IntPtr s1 = process.MainWindowHandle;
            ShowWindowAsync(s1, SW_SHOWMINIMIZED);
        }
    }
}
Posted

Look in Task Manager, on the Details tab, and you'll find that no matter how many Notepad WINDOWS you have open, there is only ever one instance of Notepad.exe, and only ever one "MainWindowHandle."

On Windows 10, there will be one instance of Notepad.exe FOR EVERY NOTEPAD WINDOW, each with its own "MainWindowHandle."

Basically, you have to do some work to find all of the child windows of Notepad and minimize each of those. No, there is nothing directly in the .NET BCL that will do that for you.
 
Share this answer
 
As explained by @Dave you need to identify all child windows of a parent window, once you get child window handle you can send message to get them minimize.

You can get a working sample from how can i get the child windows of a window given its hwnd[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900