작업표시줄(TaskBar) 만 숨기기 #1

교교 2014.03.24 08:24:43 *: Option Explicit On
Option Strict Off
Imports System.Text
Imports System.Runtime.InteropServices
Imports System.Diagnostics

Public NotInheritable Class Taskbar
Private Sub New()
End Sub
<DllImport("user32.dll")> _
Private Shared Function GetWindwText(ByVal hWnd As IntPtr, ByVal text As StringBuilder, ByVal count As Integer) As Integer
End Function
<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Private Shared Function EnumThreadWindows(ByVal threadId As Integer, ByVal pfnEnum As EnumThreadProc, lparam As IntPtr) As Boolean
End Function
<DllImport("user32.dll", setlasterror:=True)> _
Private Shared Function FindWindow(ByVal lpClassName As String, ByVal WindowName As String) As System.IntPtr
End Function
<DllImport("user32.dll", setlasterror:=True)> _
Private Shared Function FindWindowEx(ByVal paranHandle As IntPtr, ByVal childAfter As IntPtr, ByVal ClassName As String, ByVal windowTitle As String) As IntPtr
End Function
<DllImport("user32.dll")> _
Private Shared Function FindWindowEx(ByVal parenthWnd As IntPtr, ByVal ChildAfterHWndhWnd As IntPtr, ByVal ClassName As IntPtr, ByVal windowtext As String) As IntPtr
End Function

<DllImport("user32.dll")> _
Private Shared Function ShowWindow(ByVal hWnd As IntPtr, ByVal nCmdShow As Integer) As Integer
End Function

<DllImport("user32.dll")> _
Private Shared Function GetWindowThreadProcessId(ByVal hWnd As IntPtr, ByRef lpdwProcessId As Integer) As UInteger
End Function

Private Const SW_HIDE As Integer = 0
Private Const SW_SHOW As Integer = 5

Private Const VistaStartMenuCaption As String = "Start"
Private Shared VistaStartMenuWnd As IntPtr = IntPtr.Zero
Private Delegate Function EnumThreadProc(ByVal hWnd As IntPtr, ByVal lParam As IntPtr) As Boolean


Public Shared Sub show()
SetVisibility(True)
End Sub

Public Shared Sub hide()
SetVisibility(False)
End Sub

Public Shared WriteOnly Property Visible() As Boolean
Set(ByVal value As Boolean)
SetVisibility(value)
End Set
End Property

Private Shared Sub SetVisibility(ByVal show As Boolean)
Dim taskBarWnd As IntPtr = FindWindow("Shell_TrayWnd", Nothing)
Dim startWnd As IntPtr = FindWindowEx(taskBarWnd, IntPtr.Zero, "Button", "Start")
If startWnd = IntPtr.Zero Then
startWnd = FindWindowEx(IntPtr.Zero, IntPtr.Zero, CType(&HC017, IntPtr), "Start")
End If
If startWnd = IntPtr.Zero Then
startWnd = FindWindow("Button", Nothing)
If startWnd = IntPtr.Zero Then
startWnd = GetVistaStartMenuWnd(taskBarWnd)
End If
End If

ShowWindow(taskBarWnd, If(show, SW_SHOW, SW_HIDE))
ShowWindow(startWnd, If(show, SW_SHOW, SW_HIDE))
End Sub

Private Shared Function GetVistaStartMenuWnd(ByVal taskBarWnd As Integer) As IntPtr
Dim procId As Integer
GetWindowThreadProcessId(taskBarWnd, procId)
Dim p As Process = Process.GetProcessById(procId)
If p IsNot Nothing Then
For Each t As Process In p.Threads
EnumThreadWindows(t.Id, AddressOf MyEnumThreadWindowProc, IntPtr.Zero)
Next
End If
Return VistaStartMenuWnd
End Function

Private Shared Function MyEnumThreadWindowProc(ByVal hWnd As IntPtr, ByVal lParam As Integer) As Boolean
Dim buffer As New StringBuilder(256)
If GetWindwText(hWnd, buffer, buffer.Capacity) > 0 Then
Console.WriteLine(buffer)
If buffer.ToString() = VistaStartMenuCaption Then
VistaStartMenuWnd = hWnd
Return False
End If
End If
Return True
End Function

Private Shared Function GetVistaStartMenuWnd(taskBarWnd As IntPtr) As IntPtr
Throw New NotImplementedException
End Function

Private Shared Sub GetWindowThreadProcessId(taskBarWnd As Integer, procId As Integer)
Throw New NotImplementedException
End Sub

End Class