I found this code on the web for hiding the windows taskbar.
It works as advertisedOption Explicit Private Declare Function FindWindowEx& Lib "user32" Alias "FindWindowExA" _ (ByVal hWnd1&, ByVal hWnd2&, ByVal lpsz1$, ByVal lpsz2$) Private Declare Function ShowWindow& Lib "user32" (ByVal hwnd&, ByVal nCmdShow&) Sub TaskBar_Hide() ShowWindow FindWindowEx(0, 0, "Shell_TrayWnd", vbNullString), 0 End Sub Sub TaskBar_Show() ShowWindow FindWindowEx(0, 0, "Shell_TrayWnd", vbNullString), 5 End Sub
What I would like to do is read a value so that I can toggle between show/hide.
Any help is appreciated
Read a value from Where?
Please Read Forum Rules Before Posting
Wrap VBA code by selecting the code and clicking the # icon or Read This
How To Cross Post politely
Top Excel links for beginners to Experts
If you are pleased with a member's answer then use the Scales icon to rate it
If my reply has assistedor failed to assist you
I welcome your Feedback.
That is my question
how do I test for whether the taskbar is visible ?
Hello SuitedAces,
Here is the code to check if a task bar is visible. However, this not what you really need. The TaskBar is always visible, even when the Atuo Hide is enabled. It is really reduced in size to only a few pixels. What you really need the second macro which displays a message box if it is hidden.
Macro to test if the Taskbar is MinimizedPrivate Const GWL_STYLE As Long = (-16) Private Const WS_VISIBLE As Long = &H10000000 Private Declare Function FindWindowEx _ Lib "user32" Alias "FindWindowExA" _ (ByVal hwndParent As Long, _ ByVal hwndChildAfter As Long, _ ByVal lpszClass As String, _ ByVal lpszWindow As String) _ As Long Private Declare Function GetWindowLong _ Lib "user32" Alias "GetWindowLongA" _ (ByVal hWnd As Long, _ ByVal nIndex As Long) _ As Long Public Function IsTaskbarVisible() As Boolean Dim hWnd As Long Dim Ret As Long hWnd = FindWindowEx(0, 0, "Shell_TrayWnd", vbNullString) Ret = GetWindowLong(hWnd, GWL_STYLE) If Ret And WS_VISIBLE Then IsTaskbarVisible = True End Function
Sincerely,Sub CheckTaskbar() Dim hWnd As Long Dim Tray_Rect As API_RECT Dim DskTop_Rect As API_RECT Dim Ret As Long hWnd = FindWindowEx(0&, 0&, "Shell_TrayWnd", vbNullString) Ret = GetWindowRect(hWnd, Tray_Rect) hWnd = GetDesktopWindow() Ret = GetWindowRect(hWnd, DskTop_Rect) BarHeight = DskTop_Rect.Bottom - Tray_Rect.Top If BarHeight = 2 Then MsgBox "Task Bar is Hidden", vbInformation End Sub
Leith Ross
Hey, thank you Leith you always seem to come through.
And I appreciate your help.
This gives me a way of determining the state of the taskbar without the need to store a variable and make sure that it is in sync with the last action taken (hide/show taskbar).
Last edited by SuitedAces; 10-29-2007 at 01:09 PM.
Hello SuitedAces,
vbInformation is MessageBox constant that displays the information icon and plays the information sound when it is displayed. Other constants are vbExclamation, vbWarning, and vbCritical.
Sincerely,
Leith Ross
Leith I'm trying to run the code and I am getting an error..... Dim Tray_Rect As API_RECT......user defined type not defined
Last edited by SuitedAces; 10-29-2007 at 04:14 PM.
Hello SuitedAces,
Sorry, I didn't see that the rectangle structure was missing. Here is the correct macro listing for testing if the Task Bar is hidden...
Sincerely,Private Type API_RECT Left As Long Top As Long Right As Long Bottom As Long End Type Private Declare Function FindWindowEx _ Lib "user32" Alias "FindWindowExA" _ (ByVal hwndParent As Long, _ ByVal hwndChildAfter As Long, _ ByVal lpszClass As String, _ ByVal lpszWindow As String) _ As Long Declare Function GetDesktopWindow _ Lib "user32.dll" () As Long Declare Function GetWindowRect _ Lib "user32.dll" _ (ByVal hWnd As Long, _ ByRef lpRect As API_RECT) As Long Sub CheckTaskbar() Dim hWnd As Long Dim Tray_Rect As API_RECT Dim DskTop_Rect As API_RECT Dim Ret As Long hWnd = FindWindowEx(0&, 0&, "Shell_TrayWnd", vbNullString) Ret = GetWindowRect(hWnd, Tray_Rect) hWnd = GetDesktopWindow() Ret = GetWindowRect(hWnd, DskTop_Rect) BarHeight = DskTop_Rect.Bottom - Tray_Rect.Top If BarHeight = 2 Then MsgBox "Task Bar is Hidden", vbInformation End Sub
Leith Ross
Leith it's still not working.
What I don't understand is the variable 'ret' .
It is set twice without any operation in between.
In both cases when I hide or show the taskbar , BarHeight = 30
Then I checked the dimensions of Tray_Rect and they do not change when I hide/show the taskbar
Top = 770 , Bottom = 800 in both cases
Last edited by SuitedAces; 10-30-2007 at 09:03 AM.
Leith after doing some searching I found this function
So I tried this :Public Declare Function IsWindowVisible Lib "user32" ( ByVal hwnd As Long)As Long
It seems to be working.Private Declare Function FindWindowEx& Lib "user32" Alias "FindWindowExA" _ (ByVal hWnd1&, ByVal hWnd2&, ByVal lpsz1$, ByVal lpsz2$) Private Declare Function ShowWindow& Lib "user32" (ByVal hwnd&, ByVal nCmdShow&) Public Declare Function IsWindowVisible Lib "user32" ( ByVal hwnd As Long) As Long Sub ToggleTaskbar() If IsWindowVisible(FindWindowEx(0, 0, "Shell_TrayWnd", vbNullString)) = 0 Then ShowWindow FindWindowEx(0, 0, "Shell_TrayWnd", vbNullString), 5 Else ShowWindow FindWindowEx(0, 0, "Shell_TrayWnd", vbNullString), 0 End If End Sub
I don't know if there is anything that could make it fail because this is guesswork on my part.
If you have any input I appreciate it .
Last edited by SuitedAces; 10-30-2007 at 10:27 AM.
Hello SuitedAces,
The macro I wrote runs fine on my machine (Windows Xp and Excel 2000). You didn't mention, and I didn't ask what your OS is and which version of Office you are running. API calls are often sensitive to these.
I didn't use the ShowWindow, which directly controls how a Window is displayed, because you wanted to know its current displayed state (I thought). The macro works by retrieving the size of the Desktop area, and then the area of the TaskBar. The relavent co-ordinates are subtracted to determine the current height of the Taskbar. When it is hidden, it is generally only a few pixels high.
The Ret variables holds the return value of the API function call. This is proper coding practice with API calls. The return value generally tells you whether the function succeeded, failed, or if an error was encountered. Since API is low level code, it is never a good idea or practice to take shortcuts. The results can be severe.
Sincerely,
Leith Ross
Last edited by Leith Ross; 10-30-2007 at 12:03 PM.
Leith
I have office 2003 running on Xp
So I don't know if that is the cause but I ran the code exactly as you posted it with no success.
I used the messagebox to read back the top and bottom properties of the taskbar and they didn't change with the state of the taskbar.
To answer your question ...yes I was looking to return the current state of the taskbar so that I could write a toggle routine.
Leith are you saying that the method I came up with is not recommended ?
As far as the Ret variable I was just trying to understand the structure because it's foreign to me.
It is different than anything I'm familiar with in the sense that the two variables Tray_Rect and DskTop_Rect are being assinged value but
instead of ; DskTop_Rect = ... they are arguments of the function instead.
It's just different enough where I'm little confused by it.
Last edited by SuitedAces; 10-30-2007 at 01:10 PM.
Hello SuitedAces,
I ran the macro on my other computer which has Office 2003 with Windows XP, and it works with no problems. Post the code you are using so I can check it.
The RECT structure is a Type declaration. This allows you to create an object whose structure is composed of other object types that can be referenced individually within the structure. The name that follows Type is then entered into the general component library. This construct is used in C as well. The API has many such type structures.
Sincerely,
Leith Ross
Leith this is the code I was running
Option Explicit Private Type API_RECT Left As Long Top As Long Right As Long Bottom As Long End Type Private Declare Function FindWindowEx _ Lib "user32" Alias "FindWindowExA" _ (ByVal hwndParent As Long, _ ByVal hwndChildAfter As Long, _ ByVal lpszClass As String, _ ByVal lpszWindow As String) _ As Long Declare Function GetDesktopWindow _ Lib "user32.dll" () As Long Declare Function GetWindowRect _ Lib "user32.dll" _ (ByVal hwnd As Long, _ ByRef lpRect As API_RECT) As Long Sub CheckTaskbar() Dim hwnd As Long Dim Tray_Rect As API_RECT Dim DskTop_Rect As API_RECT Dim Ret As Long Dim Barheight As Long hwnd = FindWindowEx(0&, 0&, "Shell_TrayWnd", vbNullString) Ret = GetWindowRect(hwnd, Tray_Rect) hwnd = GetDesktopWindow() Ret = GetWindowRect(hwnd, DskTop_Rect) Barheight = DskTop_Rect.Bottom - Tray_Rect.Top If Barheight = 2 Then MsgBox "Task Bar is Hidden" Else MsgBox "Task Bar is Visible" End If End Sub
this is the code I used to hide the taskbar
Private Declare Function FindWindowEx& Lib "user32" Alias "FindWindowExA" _ (ByVal hWnd1&, ByVal hWnd2&, ByVal lpsz1$, ByVal lpsz2$) Private Declare Function ShowWindow& Lib "user32" (ByVal hwnd&, ByVal nCmdShow&) Sub TaskBar_Hide() ShowWindow FindWindowEx(0, 0, "Shell_TrayWnd", vbNullString), 0 End Sub Sub TaskBar_Show() ShowWindow FindWindowEx(0, 0, "Shell_TrayWnd", vbNullString), 5 End Sub
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks