Minimize All the Applications Running

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Atran
    Contributor
    • May 2007
    • 319

    Minimize All the Applications Running

    Hello:
    How I can minimize all the applications using C#.

    Thanks for anyhelp.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Well I am not sure what specific message you send (you can look it up)
    I would say something like this maybe?
    (Not you will have to replace AMessageID, TheWParam, and TheLParam)
    Code:
    foreach (Process p in Process.GetProcesses())
                {
                    if (p.MainWindowHandle != null)
                    {
                        Message.Create(p.MainWindowHandle, AMessageID, TheWParam, TheLParam);
                    }
                }

    Comment

    • Motoma
      Recognized Expert Specialist
      • Jan 2007
      • 3236

      #3
      Originally posted by Atran
      Hello:
      How I can minimize all the applications using C#.

      Thanks for anyhelp.
      This is how I do it:

      You use DllImport on user32.dll and get EnumWindows, GetWindowText, GetWindowLong, and SetWindowLong.

      You can use EnumWindows and EnumChildWindow s to iterate through your open windows getting their handles. Then you check visibility with GetWindowLong on WS_MINIMIZE and check the title of the window (optional) with GetWindowText. Then you set the minimized flag on the window with SetWindowLong.

      Comment

      • Motoma
        Recognized Expert Specialist
        • Jan 2007
        • 3236

        #4
        Although, Platter's method is a much nicer way to grab window handles.

        Comment

        • Motoma
          Recognized Expert Specialist
          • Jan 2007
          • 3236

          #5
          Played around a little bit, and whipped up a nice bit of code that works (somewhat).

          [code=c]
          #region Using Statements
          using System;
          using System.Diagnost ics;
          using System.Runtime. InteropServices ;
          using System.Windows. Forms;
          #endregion

          namespace WindowHide
          {
          public static class UnManagedMethod s
          {
          [DllImport("user 32.dll", CharSet = CharSet.Auto)]
          public static extern bool ShowWindow(IntP tr hWnd, int cmdshow);
          }

          public class Hide
          {
          public Hide()
          {
          foreach (Process p in Process.GetProc esses())
          {
          UnManagedMethod s.ShowWindow(p. MainWindowHandl e, 6);
          }
          }
          }
          }
          [/code]
          Last edited by Motoma; Jul 9 '07, 07:43 PM. Reason: removed a useless 'using' statement from the code

          Comment

          • Atran
            Contributor
            • May 2007
            • 319

            #6
            Thanks to you all...........

            Comment

            • TRScheel
              Recognized Expert Contributor
              • Apr 2007
              • 638

              #7
              Originally posted by Motoma
              [code=c]
              ...
              UnManagedMethod s.ShowWindow(p. MainWindowHandl e, 6);
              ...
              [/code]

              Why 6?

              What does 0 - 5 do? And when does it end?

              Comment

              • Motoma
                Recognized Expert Specialist
                • Jan 2007
                • 3236

                #8
                Originally posted by TRScheel
                Why 6?

                What does 0 - 5 do? And when does it end?
                Oh, I went spelunking through windows.h to grep up the value associated with SW_MINIIMIZE. The answer value is 6.

                Comment

                • TRScheel
                  Recognized Expert Contributor
                  • Apr 2007
                  • 638

                  #9
                  Originally posted by Motoma
                  Oh, I went spelunking through windows.h to grep up the value associated with SW_MINIIMIZE. The answer value is 6.
                  And for awhile there, I thought it was 42, =P. Nice find, I will have to 'spelunk' through there myself...

                  Comment

                  • TRScheel
                    Recognized Expert Contributor
                    • Apr 2007
                    • 638

                    #10
                    I tried this solution, and it gave me an access denied. So then i placed it within a try catch block. It then, actually showed me a hidden form used for one of my programs (XFire). Other than that... nothing happened.

                    [code=cpp]
                    namespace SandBoxTest
                    {
                    public static class UnManagedMethod s
                    {
                    [DllImport("user 32.dll", CharSet = CharSet.Auto)]
                    public static extern bool ShowWindow(IntP tr hWnd, int cmdshow);

                    }

                    class Program
                    {
                    [STAThread]
                    static void Main(string[] args)
                    {
                    foreach (Process p in Process.GetProc esses())
                    {
                    try
                    {
                    UnManagedMethod s.ShowWindow(p. Handle, 6);
                    }
                    catch { }
                    }
                    }
                    }
                    }
                    [/code]


                    EDIT:

                    Just realized I needed to use MainWindowHandl e instead of Handle. Even then, it didnt grab all the forms, and a new form appeared (probably another hidden form, my guess is MSN Messenger).

                    Comment

                    • Motoma
                      Recognized Expert Specialist
                      • Jan 2007
                      • 3236

                      #11
                      Originally posted by TRScheel
                      And for awhile there, I thought it was 42, =P. Nice find, I will have to 'spelunk' through there myself...
                      Yeah, even with C# taking over, it is still good to have wome Win32 API knowledge kicking around in the back of your mind.

                      Comment

                      • TRScheel
                        Recognized Expert Contributor
                        • Apr 2007
                        • 638

                        #12
                        Ya, i am getting some really strange results depending on the the different applications states. Especially messengers, or 'hidden apps' like outlook. For instance, I just threw in the original call I did with just p.Handle after p.MainHandle, and it got everything, but then a few hidden forms were shown. MSN Messenger showed a weird form with 0C6C1D2B-etc-etc-etc... it goes on as its name.

                        Comment

                        • Motoma
                          Recognized Expert Specialist
                          • Jan 2007
                          • 3236

                          #13
                          Originally posted by TRScheel
                          Ya, i am getting some really strange results depending on the the different applications states. Especially messengers, or 'hidden apps' like outlook. For instance, I just threw in the original call I did with just p.Handle after p.MainHandle, and it got everything, but then a few hidden forms were shown. MSN Messenger showed a weird form with 0C6C1D2B-etc-etc-etc... it goes on as its name.
                          Yep, not all windows are supposed to be set to the minimized state, particularly ones set to hidden, as well as explorer and the taskbar. The code I gave will certainly need checks to ensure that the window is in the correct state, my first post mentions how one would start to do this.

                          Comment

                          Working...