CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Deploying Windows Server 2008 with System Center
  • Remote Desktop Protocol Performance Improvements in Windows Server 2008 R2 and Windows 7
  • The Microsoft Dynamics CRM Security Model
  • SQL Server Modeling Services with Microsoft Visual Studio 2010 Beta 2

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > .NET Programming > C-Sharp Programming
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    C-Sharp Programming Post questions, answers, and comments about C#.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old November 23rd, 2009, 10:39 AM
    pethil pethil is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 2
    pethil is an unknown quantity at this point (<10)
    Question Minimize all open windows

    Hello,

    I'm trying to minimize all open windows.

    I have tried to get a few api's to find all open windows and close them, but i cant manage to do it...

    first i thought i had a working app, i was getting a list of processes and close their open windows. altho that didnt work wid a few windows, like explorer (windows 7).

    anyway i can use EnumWindow to loop throw all windows and minimize them?

    a little example would be very helpful.

    Thanks in advance,
    Peter
    Reply With Quote
      #2    
    Old November 23rd, 2009, 11:49 AM
    HanneSThEGreaT's Avatar
    HanneSThEGreaT HanneSThEGreaT is offline
    The Unstable Mod
    Power Poster
     
    Join Date: Jul 2001
    Location: Vereeniging, South Africa
    Posts: 8,008
    HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+)
    Re: Minimize all open windows

    Include this ontop of your class :
    Code:
    using System.Runtime.InteropServices; //For APIs
    Add the following 2 APIs, with their Constants :
    Code:
            [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
            static extern IntPtr FindWindow(string lpClassName, string lpWindowName); //Find A Window
            [DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true)]
            static extern IntPtr SendMessage(IntPtr hWnd, Int32 Msg, IntPtr wParam, IntPtr lParam); //Send System Message
    
            const int WM_COMMAND = 0x111;
            const int MIN_ALL = 419;
            const int MIN_ALL_UNDO = 416;
    Add this in your Button's click event :
    Code:
                IntPtr lHwnd = FindWindow("Shell_TrayWnd", null); //Get  Access To TaskBar Window
                SendMessage(lHwnd, WM_COMMAND, (IntPtr)MIN_ALL, IntPtr.Zero); //Minimize All
                System.Threading.Thread.Sleep(2000); //Let It Do Its Stuff
                SendMessage(lHwnd, WM_COMMAND, (IntPtr)MIN_ALL_UNDO, IntPtr.Zero);
    Voila!

    I hope it helps!
    __________________
    My Latest Articles :
    Changing Mouse Settings with VB.NET || Creating Your Own Encryption / Decryption Program Using VB.NET 2005 || Autorun Menu Creator in VB.NET || Creating Your Own Tetris Game With VB.NET Part 1
    Find All My Articles : Here

    FAQs :
    .NET FrameWork FAQs || Visual Basic.NET FAQs || C# FAQs

    Be The Change That You Want To See In The World - Michael Scofield, Prison Break; Season 1 Episode 1

    Read This Before You Post || Acceptable Use Policy
    Reply With Quote
      #3    
    Old November 23rd, 2009, 12:40 PM
    Arjay's Avatar
    Arjay Arjay is offline
    Moderator / MS MVP
    Power Poster
     
    Join Date: Aug 2004
    Posts: 6,914
    Arjay has a reputation beyond repute (3000+) Arjay has a reputation beyond repute (3000+) Arjay has a reputation beyond repute (3000+) Arjay has a reputation beyond repute (3000+) Arjay has a reputation beyond repute (3000+) Arjay has a reputation beyond repute (3000+) Arjay has a reputation beyond repute (3000+) Arjay has a reputation beyond repute (3000+) Arjay has a reputation beyond repute (3000+) Arjay has a reputation beyond repute (3000+) Arjay has a reputation beyond repute (3000+)
    Re: Minimize all open windows

    Hannes, you may want to use SendMessageTimeout instead.

    If you don't, SendMessage will cause your app to hang if any target app (window) is not responding.
    Reply With Quote
      #4    
    Old November 23rd, 2009, 01:29 PM
    zips zips is offline
    Member
     
    Join Date: Oct 2004
    Location: Rocket City
    Posts: 165
    zips will become famous soon enough (65+)
    Re: Minimize all open windows

    Quote:
    I'm trying to minimize all open windows.
    If you are talking about all the window forms being part of the same app, you can make one of the forms (usually your main form) the Owner of the others. It is a property of each form. Then if you minimize any one of them, all will minimize together, and they will all Restore together.
    Code:
    //  somewhere in your main form:
    
    Form SomeForm = new Form();
    SomeForm.Owner = this;
    Reply With Quote
      #5    
    Old November 24th, 2009, 12:56 AM
    HanneSThEGreaT's Avatar
    HanneSThEGreaT HanneSThEGreaT is offline
    The Unstable Mod
    Power Poster
     
    Join Date: Jul 2001
    Location: Vereeniging, South Africa
    Posts: 8,008
    HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+)
    Re: Minimize all open windows

    Quote:
    Originally Posted by Arjay View Post
    Hannes, you may want to use SendMessageTimeout instead.
    Good call!

    I'll see how I can modify this
    __________________
    My Latest Articles :
    Changing Mouse Settings with VB.NET || Creating Your Own Encryption / Decryption Program Using VB.NET 2005 || Autorun Menu Creator in VB.NET || Creating Your Own Tetris Game With VB.NET Part 1
    Find All My Articles : Here

    FAQs :
    .NET FrameWork FAQs || Visual Basic.NET FAQs || C# FAQs

    Be The Change That You Want To See In The World - Michael Scofield, Prison Break; Season 1 Episode 1

    Read This Before You Post || Acceptable Use Policy
    Reply With Quote
      #6    
    Old November 24th, 2009, 01:08 AM
    MadHatter MadHatter is offline
    Senior Member
     
    Join Date: Mar 2004
    Location: 33°11'18.10"N 96°45'20.28"W
    Posts: 1,777
    MadHatter is a splendid one to behold (750+) MadHatter is a splendid one to behold (750+) MadHatter is a splendid one to behold (750+) MadHatter is a splendid one to behold (750+) MadHatter is a splendid one to behold (750+) MadHatter is a splendid one to behold (750+) MadHatter is a splendid one to behold (750+)
    Re: Minimize all open windows

    couldn't you also use postmessage and the broadcast handle to accomplish the same thing.
    __________________
    /bb|[^b]{2}/ my deeply padded corner of the internet
    Reply With Quote
      #7    
    Old November 24th, 2009, 01:35 AM
    HanneSThEGreaT's Avatar
    HanneSThEGreaT HanneSThEGreaT is offline
    The Unstable Mod
    Power Poster
     
    Join Date: Jul 2001
    Location: Vereeniging, South Africa
    Posts: 8,008
    HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+)
    Re: Minimize all open windows

    OK, here's the Modified code, now using SendMessageTimeOut :

    Code:
            [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
            static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
           // [DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true)]
           // static extern IntPtr SendMessage(IntPtr hWnd, Int32 Msg, IntPtr wParam, IntPtr lParam);
            [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
            static extern IntPtr SendMessageTimeout(
                IntPtr windowHandle,
                uint Msg,
                IntPtr wParam,
                IntPtr lParam,
                SendMessageTimeoutFlags flags,
                uint timeout,
                out IntPtr result);
    
            [Flags]
            enum SendMessageTimeoutFlags : uint
            {
                SMTO_NORMAL = 0x0,
                SMTO_BLOCK = 0x1,
                SMTO_ABORTIFHUNG = 0x2,
                SMTO_NOTIMEOUTIFNOTHUNG = 0x8
            }
    
            const int WM_COMMAND = 0x111;
            const int MIN_ALL = 419;
            const int MIN_ALL_UNDO = 416;
    
            private void button1_Click(object sender, EventArgs e)
            {
                IntPtr OutResult;
                IntPtr lHwnd = FindWindow("Shell_TrayWnd", null);
                SendMessageTimeout(lHwnd, WM_COMMAND, (IntPtr)MIN_ALL, IntPtr.Zero, SendMessageTimeoutFlags.SMTO_ABORTIFHUNG, 2000, out OutResult);
                System.Threading.Thread.Sleep(2000);
                SendMessageTimeout(lHwnd, WM_COMMAND, (IntPtr)MIN_ALL_UNDO, IntPtr.Zero, SendMessageTimeoutFlags.SMTO_ABORTIFHUNG, 2000, out OutResult);
            }
        }
    Quote:
    Originally Posted by MadHatter
    couldn't you also use postmessage and the broadcast handle to accomplish the same thing.
    Yes
    __________________
    My Latest Articles :
    Changing Mouse Settings with VB.NET || Creating Your Own Encryption / Decryption Program Using VB.NET 2005 || Autorun Menu Creator in VB.NET || Creating Your Own Tetris Game With VB.NET Part 1
    Find All My Articles : Here

    FAQs :
    .NET FrameWork FAQs || Visual Basic.NET FAQs || C# FAQs

    Be The Change That You Want To See In The World - Michael Scofield, Prison Break; Season 1 Episode 1

    Read This Before You Post || Acceptable Use Policy
    Reply With Quote
      #8    
    Old November 24th, 2009, 01:58 AM
    HanneSThEGreaT's Avatar
    HanneSThEGreaT HanneSThEGreaT is offline
    The Unstable Mod
    Power Poster
     
    Join Date: Jul 2001
    Location: Vereeniging, South Africa
    Posts: 8,008
    HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+)
    Re: Minimize all open windows

    Easiest way yet, without using APIs, is to make use of the Shell32 class directly

    Set a reference to Microsoft Shell Controls And Automation, to do this follow these steps :
    Click Project
    Click Add Reference
    Click the COM tab
    Scroll down and select Microsoft Shell Controls And Automation
    Click OK

    Add a Button and give it the Text : Show Desktop
    Switch to code View, add the following using statement :
    Code:
    using Shell32;
    Just above your Button's code ( or where ever ), add the following variable :
    Code:
    private static Shell32.Shell shellInterface = new Shell32.Shell();
    In your Button's Click event, type the following :
    Code:
            private void button1_Click(object sender, EventArgs e)
            {
                shellInterface.MinimizeAll(); 
    
            }
    Your whole project should look like this :
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using Shell32;
    
    
    namespace ShowDesktop
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private static Shell32.Shell shellInterface = new Shell32.Shell(); 
            private void button1_Click(object sender, EventArgs e)
            {
                shellInterface.MinimizeAll(); 
    
            }
        }
    }
    I am attaching the sample here as well

    I hope this also helps!
    Attached Files
    File Type: zip ShowDesktop.zip (62.5 KB, 23 views)
    __________________
    My Latest Articles :
    Changing Mouse Settings with VB.NET || Creating Your Own Encryption / Decryption Program Using VB.NET 2005 || Autorun Menu Creator in VB.NET || Creating Your Own Tetris Game With VB.NET Part 1
    Find All My Articles : Here

    FAQs :
    .NET FrameWork FAQs || Visual Basic.NET FAQs || C# FAQs

    Be The Change That You Want To See In The World - Michael Scofield, Prison Break; Season 1 Episode 1

    Read This Before You Post || Acceptable Use Policy
    Reply With Quote
      #9    
    Old November 25th, 2009, 08:29 AM
    pethil pethil is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 2
    pethil is an unknown quantity at this point (<10)
    Re: Minimize all open windows

    Thank you all for your help!

    I managed to create my little app that can minimize all windows on just one screen in my 4 screen setup.

    mapped to Ctrl + 1, 2, 3 and 4. makes it a quite usefull app.

    attaching source if someone wants to take a look at it.
    Definitely badly written but it does what it's suppose to.

    Thanks again!
    Attached Files
    File Type: zip hide all.zip (107.6 KB, 19 views)
    Reply With Quote
      #10    
    Old November 25th, 2009, 08:49 AM
    HanneSThEGreaT's Avatar
    HanneSThEGreaT HanneSThEGreaT is offline
    The Unstable Mod
    Power Poster
     
    Join Date: Jul 2001
    Location: Vereeniging, South Africa
    Posts: 8,008
    HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+) HanneSThEGreaT has a reputation beyond repute (3000+)
    Re: Minimize all open windows

    Thanx for sharing!

    Everyone starts with writing "badly written programs"
    With experience, it imporves, keep on keeping on !
    __________________
    My Latest Articles :
    Changing Mouse Settings with VB.NET || Creating Your Own Encryption / Decryption Program Using VB.NET 2005 || Autorun Menu Creator in VB.NET || Creating Your Own Tetris Game With VB.NET Part 1
    Find All My Articles : Here

    FAQs :
    .NET FrameWork FAQs || Visual Basic.NET FAQs || C# FAQs

    Be The Change That You Want To See In The World - Michael Scofield, Prison Break; Season 1 Episode 1

    Read This Before You Post || Acceptable Use Policy
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > .NET Programming > C-Sharp Programming


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 01:57 PM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.