1

I use the following program for Hide/Show desktop Items using c#. It was working Fine.

public partial class Form1 : Form
{
    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr GetWindow(IntPtr hWnd, GetWindow_Cmd uCmd);
    enum GetWindow_Cmd : uint
    {
        GW_HWNDFIRST = 0,
        GW_HWNDLAST = 1,
        GW_HWNDNEXT = 2,
        GW_HWNDPREV = 3,
        GW_OWNER = 4,
        GW_CHILD = 5,
        GW_ENABLEDPOPUP = 6
    }
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

    private const int WM_COMMAND = 0x111;

    static void ToggleDesktopIcons()
    {
        var toggleDesktopCommand = new IntPtr(0x7402);
        IntPtr hWnd = GetWindow(FindWindow("Progman", "Program Manager"), GetWindow_Cmd.GW_CHILD);
        SendMessage(hWnd, WM_COMMAND, toggleDesktopCommand, IntPtr.Zero);
    }

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click_1(object sender, EventArgs e)
    {
        ToggleDesktopIcons();

    }

}

But my problem is I have only one button in form. If I press the button the hide and show desktop items will happen on the same button.

I need to separate those show and Hide. Which means i need to create one more button in a form, so totally i have 2 buttons now. If I press first button I need to hide desktop items. If I press second button I need to show Desktop Items. How can I do this?

zed
  • 2,298
  • 4
  • 27
  • 44
V.V
  • 875
  • 3
  • 25
  • 54

2 Answers2

0

You can store internal state inside Form1 class, for example some bool property. If you click on Show button and bool property indicates, that your item is already shown - do nothing, else ToggleDesktopIcons. The same for hide button.

0

See the answer in the this question.

The answer contains an IsVisible method that allows you to see if the icons are hidden or shown.

In the show button: call IsVisible, if it returns false, execute ToggleDesktopIcons, otherwise return.

In the hide button: call IsVisible, if it returns true, execute ToggleDesktopIcons, otherwise return.

You can also use the result of calling IsVisible to decide if you want to enable/disable the show and hide buttons.

Community
  • 1
  • 1
Yacoub Massad
  • 27,509
  • 2
  • 36
  • 62