Hide e Show della Taskbar su .NET

In alcune situazioni è necessario nascondere la TaskBar.
Su .NET non esistono ancora comandi managed probabilmente fino alla prossima versione di windows.
E' pertanto necessario ricorrere ai comandi "WIN32".
 
Ho predisposto una classe di interfaccia per le funzioni Win32:
Per NASCONDERE richiamare:
HideTaskBar();
Per MOSTRARE la taskbar richiamare:
ShowTaskBar();
 
C#
using System;
using System.Runtime.InteropServices;
namespace LibCs
{
      public class Win32Bridge
      {
          private const int SWP_HIDEWINDOW = 0x80;
            private const int SWP_SHOWWINDOW = 0x40;
 
            public Win32Bridge()
            {
            }
 
            [DllImport("User32.dll", EntryPoint="FindWindowA")]
            private static int FindWindow(string lpClassName, string lpWindowName)
            {
            }
 
            [DllImport("User32.dll")]
            private static int SetWindowPos(int hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags)
            {
            }
          public static void ShowTaskBar()
            {
                  int intReturn = LibVb.Win32Bridge.FindWindow("Shell_traywnd", "");
                  SetWindowPos(intReturn, 0, 0, 0, 0, 0, SWP_SHOWWINDOW);
            }
 
            public static void HideTaskBar()
            {
                  int intReturn = LibVb.Win32Bridge.FindWindow("Shell_traywnd", "");
                  SetWindowPos(intReturn, 0, 0, 0, 0, 0, SWP_HIDEWINDOW);
            }
      }
}
 
VB.NET
 
Imports System
Imports System.Runtime.InteropServices
 
Namespace LibVb
 
      Public Class Win32Bridge
           
            Private Const SWP_HIDEWINDOW = &H80
            Private Const SWP_SHOWWINDOW = &H40
     
            Public Sub New()
            End Sub
           
              _
            Protected Shared Function FindWindow (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
            End Function
           
            _
            Protected Shared Function SetWindowPos(ByVal hwnd As Integer, _
                  ByVal hWndInsertAfter As Integer, ByVal x As Integer, _
                  ByVal y As Integer, ByVal cx As Integer, ByVal cy As Integer, _
                  ByVal wFlags As Integer) As Integer
            End Function
           
            Public Shared Sub ShowTaskBar()
                 Dim intReturn As Integer = FindWindow("Shell_traywnd", "")
                 SetWindowPos(intReturn, 0, 0, 0, 0, 0, SWP_SHOWWINDOW)
            End Sub
 
            Public Shared Sub HideTaskBar()
                Dim intReturn As Integer = FindWindow("Shell_traywnd", "")
                SetWindowPos(intReturn, 0, 0, 0, 0, 0, SWP_HIDEWINDOW)
            End Sub
           
      End Class
End Namespace
 
Scarica il progetto di esempio  con Option Strict On realizzato con Visual Basic 2005

Print | posted on lunedì 26 febbraio 2007 13:47

Feedback

# re: Hide e Show della Taskbar su .NET

Left by chry2403 at 26/02/2007 13:19
Gravatar Ho copiato il codice versione vb, ma mi dà errore sulle due const, dicendo che Option strict on vuole la clausola As.

# re: Hide e Show della Taskbar su .NET

Left by Luciano Bastianello at 26/02/2007 13:49
Gravatar Non ti perderai mica per un "As" da aggiungere vero?
Comunque ho aggiunto un progetto di esempio .

# re: Hide e Show della Taskbar su .NET

Left by Gabbo at 08/07/2008 15:03
Gravatar Il codice C# non funziona, l'ho corretto e questo gira:

using System;
using System.Runtime.InteropServices;

namespace LibCs
{
public class Win32Bridge
{
private const int SWP_HIDEWINDOW = 0x80;
private const int SWP_SHOWWINDOW = 0x40;

[DllImport("User32.dll", EntryPoint = "FindWindowA")]
private static extern int FindWindow(string lpClassName, string lpWindowName);


[DllImport("User32.dll")]
private static extern int SetWindowPos(int hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);

public Win32Bridge()
{
}


public static void ShowTaskBar()
{
int intReturn = Win32Bridge.FindWindow("Shell_traywnd", "");
SetWindowPos(intReturn, 0, 0, 0, 0, 0, SWP_SHOWWINDOW);
}

public static void HideTaskBar()
{
int intReturn = Win32Bridge.FindWindow("Shell_traywnd", "");
SetWindowPos(intReturn, 0, 0, 0, 0, 0, SWP_HIDEWINDOW);
}
}
}

Your comment:





 
Please add 8 and 6 and type the answer here:

Copyright © Luciano Bastianello

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski