locked
Show desktop function from modal form RRS feed

  • Question

  • I have a modal form opened as a child of the main form of an application. In this modal form I want to have a button that shows the desktop, like the function that Windows has on the quick launch bar. I have read a lot of stuff about how to do it, the most part using this code:

    Shell32.ShellClass shell = new Shell32.ShellClass();
    shell.MinimizeAll();

    But what I get is a different behaviour. All the other applications windows are minimized but my application is still there. What am I supposed to do? With the "Show desktop" icon I just can do it!!!

    Thank you in advance for your help

    cold
    Tuesday, September 8, 2009 2:57 PM

Answers

  • Well, if the "Show desktop" icon does what you want, you can simply use it. Ok, I know, that sounded silly, so let me explain:
    The "Show desktop" icon is nothing more than a .scf file ("Windows Explorer Command"). You can run a .scf file from the code just the same way you can run a batch or .exe file. The Start() method in the System.Diagnostics.Process class is all you need.

    Probably, you will want your application to have its own copy of the file, so you don't need to figure out where the local user's QuickLaunch toolbar folder is located (actually, it can even be removed, so it wouldn't be a reliable option). If you can't find your own .scf original file, you may create a new one on notepad: paste this and save as plain 8-bit ASCII:
    [Shell]
    Command=2
    IconFile=explorer.exe,3
    [Taskbar]
    Command=ToggleDesktop
    You may omit the "IconFile=" line if you want, since it only defines the icon for the file. That way, if a user browses the Program Files folder and sees the file, they'll only see a weird .scf file they won't know what's about :P

    Once you have the file, you just need to Start() it and everything should minimize.

    Now, it's up to you to use this (quite a weird approach, I'll admit), or the Win+D key simulation through raw WinAPI calls. IMO, both options are quite ugly, but they get the job done, so just pick the ugliness that better matches your taste.
    Two warnings, however:
    First, I haven't tested the process-based approach and can't tell for sure if it will work. I have used Process from time to time to run batches and even start document files, so I'd expect this to work, but I have no compiler at hand right now to make some actual testing.
    And second, if you go for paras kumar's approach of simulating Win+D keypress, keep in mind that it is possible to re-configure the Win+whatever key combinations (TweakUI power-toy, manual registry editing, and third-party tools), so in the rare case of power users that have their desktop reconfigured this way, your "Show Desktop" button will do something random, or nothing at all (depending on what has the user configured for that key combination).

    Hope this helps.
    Regards,
    Herenvardö
    • Proposed as answer by insigniya Thursday, September 10, 2009 10:53 PM
    • Marked as answer by Aland Li Friday, September 11, 2009 2:42 AM
    Thursday, September 10, 2009 2:58 PM

All replies

  • Hi codbit,

    I met a similar issue in my testing. Below are the results and my solution.

    Testing results:
    I call the ShowDialog method of a sub form to show it when I click a button on the main form.  When I call the MinimizeAll method to show desktop, I found the main form is still shown. All the other forms including sub form are minimized.

    Solution:
    We can minimize the forms those are still shown by setting the WindowState property to Minimized.
    This is the code snippet:
    Main form:
        public partial class MyMainForm : Form
        {
            public MyMainForm()
            {
                InitializeComponent();
            }
    
            private void showDialogButton_Click(object sender, EventArgs e)
            {
                MyDialogForm form = new MyDialogForm();
                form.ShowDialog(this);
            }
        }

    Sub form:

        public partial class MyDialogForm : Form
        {
            public MyDialogForm()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                //1. Show desktop via shell.
                Shell sh = new Shell();
                sh.MinimizeAll();
                //2. The main form would still show, so we minimize it.
                (this.Owner as Form).WindowState = FormWindowState.Minimized;
                //3. Step 2 would hide MyDialogForm, so we show it again. 
                this.Visible = true;
                //4. Minimize MyDialogForm.
                this.WindowState = FormWindowState.Minimized;
            }
        }

    These are some links:
    MinimizeAll method of Shell: http://msdn.microsoft.com/en-us/library/bb774083(VS.85).aspx.
    WindowState: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.windowstate.aspx.
    A similar thread: http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/7105020c-d04b-44df-96d5-30bb845e83ce.

    Let me know if this does not help.
    Aland Li

    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    Thursday, September 10, 2009 10:09 AM
  • You can simulate "Windows + D" key press event :
    Check this out:

    http://www.dotnetspider.com/resources/21734-Minimizing-all-forms-windows-Show-Desktop.aspx


    - Paras
    Thursday, September 10, 2009 12:12 PM
  • Well, if the "Show desktop" icon does what you want, you can simply use it. Ok, I know, that sounded silly, so let me explain:
    The "Show desktop" icon is nothing more than a .scf file ("Windows Explorer Command"). You can run a .scf file from the code just the same way you can run a batch or .exe file. The Start() method in the System.Diagnostics.Process class is all you need.

    Probably, you will want your application to have its own copy of the file, so you don't need to figure out where the local user's QuickLaunch toolbar folder is located (actually, it can even be removed, so it wouldn't be a reliable option). If you can't find your own .scf original file, you may create a new one on notepad: paste this and save as plain 8-bit ASCII:
    [Shell]
    Command=2
    IconFile=explorer.exe,3
    [Taskbar]
    Command=ToggleDesktop
    You may omit the "IconFile=" line if you want, since it only defines the icon for the file. That way, if a user browses the Program Files folder and sees the file, they'll only see a weird .scf file they won't know what's about :P

    Once you have the file, you just need to Start() it and everything should minimize.

    Now, it's up to you to use this (quite a weird approach, I'll admit), or the Win+D key simulation through raw WinAPI calls. IMO, both options are quite ugly, but they get the job done, so just pick the ugliness that better matches your taste.
    Two warnings, however:
    First, I haven't tested the process-based approach and can't tell for sure if it will work. I have used Process from time to time to run batches and even start document files, so I'd expect this to work, but I have no compiler at hand right now to make some actual testing.
    And second, if you go for paras kumar's approach of simulating Win+D keypress, keep in mind that it is possible to re-configure the Win+whatever key combinations (TweakUI power-toy, manual registry editing, and third-party tools), so in the rare case of power users that have their desktop reconfigured this way, your "Show Desktop" button will do something random, or nothing at all (depending on what has the user configured for that key combination).

    Hope this helps.
    Regards,
    Herenvardö
    • Proposed as answer by insigniya Thursday, September 10, 2009 10:53 PM
    • Marked as answer by Aland Li Friday, September 11, 2009 2:42 AM
    Thursday, September 10, 2009 2:58 PM
  • Good one.. Didn't know about "Show Desktop" .scf file :)..

    Thank you.

    - Paras
    Thursday, September 10, 2009 10:56 PM