Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show Desktop button in VB.NET Windows Form

In my vb.net windows form application i want make a button that when a user cliked the button, the Desktop should be showed, (Show Desktop Button) .

Consider a form name as form1 and it got a button like "Show desktop", when user clicked, all the application should be minimized and it should show desktop, is there any Code for VB.NET Windows Form application.

like image 543
Sri Vignesh Avatar asked Feb 17 '26 16:02

Sri Vignesh


1 Answers

Edit

Although my proposed solution work, but I would recommend using Code Gray's answer below, as that's the right way to do it.


In C# it goes like this:

using System;
using System.Runtime.InteropServices;

namespace ConsoleApplication1 {
class Program {
    [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);

    const int WM_COMMAND = 0x111;
    const int MIN_ALL = 419;
    const int MIN_ALL_UNDO = 416;

    static void Main(string[] args) {
        IntPtr lHwnd = FindWindow("Shell_TrayWnd", null);
        SendMessage(lHwnd, WM_COMMAND, (IntPtr)MIN_ALL, IntPtr.Zero); 
        System.Threading.Thread.Sleep(2000);
        SendMessage(lHwnd, WM_COMMAND, (IntPtr)MIN_ALL_UNDO, IntPtr.Zero);
    }
}
}

I used an online converter tool to convert the above code, please verify if it works

Imports System
Imports System.Runtime.InteropServices

Namespace ConsoleApplication1
    Class Program
        <DllImport("user32.dll", EntryPoint := "FindWindow", SetLastError := True)> _
        Private Shared Function FindWindow(lpClassName As String, lpWindowName As String) As IntPtr
        End Function
        <DllImport("user32.dll", EntryPoint := "SendMessage", SetLastError := True)> _
        Private Shared Function SendMessage(hWnd As IntPtr, Msg As Int32, wParam As IntPtr, lParam As IntPtr) As IntPtr
        End Function

        Const WM_COMMAND As Integer = &H111
        Const MIN_ALL As Integer = 419
        Const MIN_ALL_UNDO As Integer = 416

        Private Shared Sub Main(args As String())
            Dim lHwnd As IntPtr = FindWindow("Shell_TrayWnd", Nothing)
            SendMessage(lHwnd, WM_COMMAND, DirectCast(MIN_ALL, IntPtr), IntPtr.Zero)
            System.Threading.Thread.Sleep(2000)
            SendMessage(lHwnd, WM_COMMAND, DirectCast(MIN_ALL_UNDO, IntPtr), IntPtr.Zero)
        End Sub
    End Class
End Namespace
like image 172
Marshal Avatar answered Feb 19 '26 19:02

Marshal