PDA

View Full Version : Solved: How to raise/lower the Windows Taskbar with code



xltrader100
08-05-2010, 08:44 AM
I have a form that I want to show full screen, but the Windows Taskbar remains showing and my form docks to the top of the Taskbar. If I manually lower the Taskbar then everything is good, so I need a way to hide the Taskbar before showing my form and then unhide it after closing the form. Using Excel 2003 with XP.

I found KB 290150 that almost works. http://support.microsoft.com/kb/290150/EN-US/

But this only hides the Taskbar for a couple of seconds and then it snaps back into view again. Could anybody suggest a way to do this?

Simon Lloyd
08-05-2010, 11:07 AM
I'm not sure about accessing the windows properties as it usually requires api knowledge, there's a kb article here http://www.vbaexpress.com/kb/archive.php/k-54.html, but this will show your userform fullscreen regardless of the users settings.
Private Sub UserForm_Initialize()
With Application
.WindowState = xlMaximized
Zoom = Int(.Width / Me.Width * 100)
Width = .Width
Height = .Height
End With
End Sub

xltrader100
08-05-2010, 11:43 AM
Thanks, Simon, but your KB article is the same one I referenced above. And it does work, as does your code snippet, but the problem is that something in Windows is immediately switching the Task Bar back up, so that's probably a dead end.

I wonder is there any way to keep the form in front of the Task Bar?

Simon Lloyd
08-05-2010, 12:16 PM
This isn't my code but should work for you:
Public Declare Function SHAppBarMessage Lib "shell32" (ByVal dwMessage As Long, pData As APPBARDATA) As Long

Public Const ABM_GETSTATE = &H4
Public Const ABM_SETSTATE = &HA
Public Const ABS_AUTOHIDE = &H1
Public Const ABS_ALWAYSONTOP = &H2

Public Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Public Type APPBARDATA
cbSize As Long
hwnd As Long
uCallbackMessage As Long
uEdge As Long
rc As RECT
lParam As Long
End Type

Sub TaskbarAutohideOn()
Dim ABD As APPBARDATA
ABD.cbSize = Len(ABD)
SHAppBarMessage ABM_GETSTATE, ABD
ABD.lParam = ABS_AUTOHIDE Or ABS_ALWAYSONTOP
SHAppBarMessage ABM_SETSTATE, ABD
End Sub 'TaskbarAutohideOn

Sub TaskbarAutohideOff()
Dim ABD As APPBARDATA
ABD.cbSize = Len(ABD)
SHAppBarMessage ABM_GETSTATE, ABD
ABD.lParam = ABS_ALWAYSONTOP
SHAppBarMessage ABM_SETSTATE, ABD
End Sub

xltrader100
08-05-2010, 12:54 PM
Thanks, Simon. That's exactly what I was looking for.

Simon Lloyd
08-05-2010, 01:29 PM
If you don't want to set the Auto hide property this one simply hides it:Private Declare Function ShowWindow Lib "user32" _
(ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" _
Alias "FindWindowExA" (ByVal hWnd1 As Long, _
ByVal hWnd2 As Long, _
ByVal lpsz1 As String, _
ByVal lpsz2 As String) As Long
Sub TaskBar(blnValue As Boolean)
Dim lngHandle As Long
Dim lngStartButton As Long
lngHandle = FindWindow("Shell_TrayWnd", "")
If blnValue Then
ShowWindow lngHandle, 5
Else
ShowWindow lngHandle, 0
End If
End Sub
Sub Cmdhide()
Dim A As Boolean
A = False
TaskBar (A)
End Sub
Sub Cmdunhide()
Dim A As Boolean
A = True
TaskBar (A)
End Sub

xltrader100
08-09-2010, 12:25 PM
Using your shorter version, how would I find the current state of the Task Bar, so I could leave it as I found it?