-
[RESOLVED] Task Bar Metrics - when is the Taskbar Auto Hidden?
Hi, I tested this code and it works on XP and Seven, the only thing it doesn't do is adjust when the autohide property is set on and the taskbar is hidden. I found a way to determine if the taskbar is set to autohide, although is there a way to determine if the taskbar is hidden at the moment (right click the taskbar -> properties -> autohide the taskbar)?
Declarations in a module
Code:
Option Explicit
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Type APPBARDATA
cbSize As Long
hwnd As Long
uCallbackMessage As Long
uEdge As Long
rc As RECT
lParam As Long
End Type
Const ABS_ALWAYSONTOP = &H2
Const ABS_AUTOHIDE = &H1
Const ABS_BOTH = &H3
Const ABM_GETSTATE = &H4
Const ABM_GETAUTOHIDEBAR = &H7
Const ABM_GETTASKBARPOS = &H5
Const ABE_LEFT = 0
Const ABE_TOP = 1
Const ABE_RIGHT = 2
Const ABE_BOTTOM = 3
Private Declare Function SHAppBarMessage Lib "shell32.dll" (ByVal dwMessage As Long, pData As APPBARDATA) As Long
Functions in the module:
Code:
Public Function GetTaskBarMetrics() As String
Dim ABD As APPBARDATA
ABD.cbSize = Len(ABD)
SHAppBarMessage ABM_GETTASKBARPOS, ABD
GetTaskBarMetrics = "TaskBarMetrics In Pixels (not twips)" & vbCrLf & _
"Left: " & ABD.rc.Left & ", Right: " & ABD.rc.Right & ", Bottom: " & ABD.rc.Bottom & ", Top: " & ABD.rc.Top & vbCrLf & _
"Width:" & ABD.rc.Right - ABD.rc.Left & vbCrLf & _
"Height:" & ABD.rc.Bottom - ABD.rc.Top & vbCrLf & _
"Location: " & ABD.uEdge & " (0-left, 1-top, 2-right, 3-bottom)"
End Function
Public Function TaskBarState() As String
Dim ABD As APPBARDATA, res&
ABD.cbSize = Len(ABD)
res = SHAppBarMessage(ABM_GETSTATE, ABD)
If res = 0 Then
TaskBarState = "TaskBar not in auto-hide or always-on-top mode"
ElseIf res = ABS_ALWAYSONTOP Then TaskBarState = "TaskBar always-on-top mode"
ElseIf res = ABS_AUTOHIDE Then TaskBarState = "TaskBar is in auto-hide mode"
ElseIf res = ABS_BOTH Then TaskBarState = "TaskBar is always-on-top and auto-hide modes"
Else
TaskBarState = "Unknown"
End If
End Function
Example of usage:
Code:
Private Sub Command1_Click()
MsgBox GetTaskBarMetrics
MsgBox TaskBarState
End Sub
I modified the code from here http://support.microsoft.com/kb/143117 and here http://vbnet.mvps.org/index.html?cod...barmessage.htm
-
Re: Task Bar Metrics - when is the Taskbar Auto Hidden?
Look at this MSDN page for proper taskbar messages
1. Your TaskBarLocation function is really the taskbar auto-hide query, not position.
2. When you declare a variable of APPBARDATA, you must set the APPBARDATA.cbSize member (i.e., ABD.cbSize = Len(ABD)); else unexpected results may occur.
3. The GetTaskBarMetrics contains more info than just the location; ABD also includes the uEdge member filled in for that taskbar. Note that per MSDN when sending this message, the hWnd must be provided. Evidently, since you are not providing it, a value of zero must default to the desktop's main taskbar; but if more than one taskbar exists, is it first found, first served, or always the main taskbar?
-
Re: Task Bar Metrics - when is the Taskbar Auto Hidden?
You make some good points LaVolpe, I have updated the functions accordingly. Now to test the multiple taskbars issue that you raised, can I add an extra taskbar manually or does it have to be done via code, and can there even be more than one taskbar running at any one time?
-
Re: Task Bar Metrics - when is the Taskbar Auto Hidden?
Microsoft Office had one before 2003, called the MS Office Shortcut bar.
But here's a VB example of creating your own. I haven't looked at that code in sooooo many years, but if memory serves, it is a legit taskbar and may be worth downloading, compiling and test with.
Edited: Did you answer your own question regarding whether the auto-hide taskbar is hidden or not at any given moment? The easiest method I think is to get the hande of the taskbar (easy and searching forum for: taskbar handle, should show examples), then use IsWindowVisible API on that handle. I would think it would return false if the taskbar is hidden. And if it returns true (taskbar is visible but not shown on screen), then it is positioned off the screen. Again, with the handle you can query its position with GetWindowRect API and test the coordinates against the physical screen coordinates.
-
Re: Task Bar Metrics - when is the Taskbar Auto Hidden?
I downloaded that code and it runs a custom taskbar which docks onto the windows taskbar which looks ok with only minor issues. I could be doing it wrong, although I couldn't get the metrics of the custom taskbar in the same way - this is the code I tried:
Code:
Public Function GetTaskBarMetricsByHwnd() As String
Dim lhwnd&, ABD As APPBARDATA
lhwnd = FindWindow(vbNullString, "Appbar Example")
If lhwnd = 0 Then GetTaskBarMetricsByHwnd = "Error - could not locate taskbar": Exit Function
ABD.hwnd = lhwnd
ABD.cbSize = Len(ABD)
SHAppBarMessage ABM_GETTASKBARPOS, ABD
GetTaskBarMetricsByHwnd = "TaskBarMetrics In Pixels (not twips)" & vbCrLf & _
"Left: " & ABD.rc.Left & ", Right: " & ABD.rc.Right & ", Bottom: " & ABD.rc.Bottom & ", Top: " & ABD.rc.Top & vbCrLf & _
"Width:" & ABD.rc.Right - ABD.rc.Left & vbCrLf & _
"Height:" & ABD.rc.Bottom - ABD.rc.Top & vbCrLf & _
"Location: " & ABD.uEdge & " (0-left, 1-top, 2-right, 3-bottom)"
End Function
Although even if I can get the metrics of the second appbar, how would one enumerate the taskbars currently running?
-
Re: Task Bar Metrics - when is the Taskbar Auto Hidden?
I'm about to head to bed, so I'll leave these parting thoughts for you.
1) Are you sure you got the handle correct? I don't have that source code in front of me, but the main form used should be the hWnd I would think. And if that is true, have it pop up a message box after form is loaded/shown/registered itself as appbar (maybe in a click event somewhere): MsgBox "My hWnd is " & Me.hWnd. Ok, now instead of FindWindow, use an InputBox and type in what the msgbox gave you. Just an idea to avoid any uncertainties.
2) If you know you got the hWnd correct and you don't get any metrics, then either the API doesn't work with that app bar or something else is off. The ABD settings you provided appear ok. I'll take a sneak peak tomorrow. Maybe the app bar needs to handle that message in a subclassing routine?
Enumeration; that's another question worth thinking about. If I recall, an app bar doesn't have to be on the screen edge. It can be docked side by side or top to bottom with other app bars/task bars.
FYI: If that sample project worked correctly, then any window you have at the far right of the screen should adjust its position if you drag and dock that app bar to the right side of the screen.
-
Re: Task Bar Metrics - when is the Taskbar Auto Hidden?
A question of curiosity. Why do you need to know the positions and auto-hide abilities of taskbars/appbars? Asking 'cause you may be going about something the hard way.
-
Re: Task Bar Metrics - when is the Taskbar Auto Hidden?
Hi LaVolpe
I checked to make sure I was getting the correct hwnd with spy++ and double checked with msgbox me.hwnd on load, although the metrics returned are those of the windows taskbar, not the custom taskbar. I presume the hwnd property of the APPBARDATA type cannot be set and always points at the Windows taskbar.
At this stage not that many user will be running a custom taskbar so the code in post 1 will probably cover 90%+ of all situations. I was going to have my application adjust according to whether the user had the taskbar visible or not, as when the taskbar is showing the user cannot interact with the part of the form covered by the taskbar. That said it may be too much trouble for too small a return, and I should just set my form's position above where the taskbar will display when it is showing.
-
Re: Task Bar Metrics - when is the Taskbar Auto Hidden?
Quote:
Originally Posted by
Witis
...I was going to have my application adjust according to whether the user had the taskbar visible or not, as when the taskbar is showing the user cannot interact with the part of the form covered by the taskbar. That said it may be too much trouble for too small a return, and I should just set my form's position above where the taskbar will display when it is showing.
Maybe best. Otherwise, matter of using FindWindow("Shell_traywnd", vbNullString) to get the handle of the taskbar and maybe on a timer checking visibility with IsWindowVisible API. One cannot assume just because an app checks if the auto-hide feature is turned off, it will remain turned off for the time your app is running. Logic for this is filled with potential "gotchas"
-
Re: Task Bar Metrics - when is the Taskbar Auto Hidden?
I tired this, unfortunately no luck, always returns True:
Code:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Long
Private m_trayHwnd&
Private Sub Timer1_Timer()
m_trayHwnd = FindWindow("Shell_traywnd", vbNullString)
Debug.Print IIf(IsWindowVisible(m_trayHwnd) <> 0, "True", "False")
End Sub
-
Re: Task Bar Metrics - when is the Taskbar Auto Hidden?
My second guess in post #4 appears correct then...
Use GetWindowRect API to retrieve its X,Y. You'll see that the X and/or Y are off the screen when it is visible
-
Re: Task Bar Metrics - when is the Taskbar Auto Hidden?
Yep that works, thanks LaVolpe :thumb: Here is the code in case anyone else needs it:
Add to a form with a timer:
declarations
Code:
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function SHAppBarMessage Lib "shell32.dll" (ByVal dwMessage As Long, pData As APPBARDATA) As Long
Private Type APPBARDATA
cbSize As Long
hwnd As Long
uCallbackMessage As Long
uEdge As Long
rc As RECT
lParam As Long
End Type
Const ABM_GETTASKBARPOS = &H5
Code in timer:
Code:
Private Sub Timer1_Timer()
Dim ABD As APPBARDATA, rec As RECT, ret&, trayHwnd&
trayHwnd = FindWindow("Shell_traywnd", vbNullString)
ret = GetWindowRect(trayHwnd, rec)
If ret = 0 Then Exit Sub
ABD.cbSize = Len(ABD)
SHAppBarMessage ABM_GETTASKBARPOS, ABD
Select Case ABD.uEdge
Case 0 '0-left
If rec.Left < 0 Then Debug.Print "Taskbar Hidden" Else Debug.Print "Taskbar Showing"
Case 1 '1-top
If rec.Top < 0 Then Debug.Print "Taskbar Hidden" Else Debug.Print "Taskbar Showing"
Case 2 '2-right
If rec.Right > Screen.Width / Screen.TwipsPerPixelX Then Debug.Print "Taskbar Hidden" Else Debug.Print "Taskbar Showing"
Case 3 '3-bottom
If rec.Bottom > Screen.Height / Screen.TwipsPerPixelY Then Debug.Print "Taskbar Hidden" Else Debug.Print "Taskbar Showing"
End Select
End Sub
Updated to be windows vista and seven aware (checks all potential taskbar locations left,top,right,bottom) as per LaVolpe's comments.
To remove the timer, would I need to do a global windows hook and look for the messages linked to the taskbar?
Btw the windows "Task and Start Menu Properties" dialog makes a mistake regarding this issue. In XP (home sp3) If I have the windows taskbar set to autohide, and then right click and select properties, the "Task and Start Menu Properties" pops up to the left side of the screen but is partially obscured by the windows taskbar, which for a commercial operating system is quite an eyesore. I can confirm the same problem exists on Win 7 too.
-
Re: Task Bar Metrics - when is the Taskbar Auto Hidden?
Yes & no. The taskbar may not be at the bottom; could be top, right or left sides. But the idea works.
-
Re: Task Bar Metrics - when is the Taskbar Auto Hidden?
just chiming in here, and you may already know this, but it seems to me you are doing more than necessary. I am not sure what you mean by "docks" though. If you mean you are overlaying on the front the way the media player toolbar does, then you only have to set your toolbar to be a child window of the taskbar with setparent.
lavolpe: yes and no, 0 is the default for the desktop. It has its own window handle, but zero is a built-in shortcut.
-
Re: Task Bar Metrics - when is the Taskbar Auto Hidden?
ok, it is now updated, XP home only allows the windows taskbar to be at the bottom although seven allows 4 positions, so it is now windows seven aware. Next thing - is there anyway to get rid of the timer (end of post 12)?
-
Re: Task Bar Metrics - when is the Taskbar Auto Hidden?
Witis, I don't have XP home - have XP Pro. The bar can be docked to any of the 4 sides. If your taskbar's Locked property is set to true, that'll prevent your from moving it.
-
Re: Task Bar Metrics - when is the Taskbar Auto Hidden?
that's not right. Even windows 95 will let you move the taskbar. Evidently you have the taskbar locked. If it's locked you can't move it. I have xp on two different computers and i can assure you the taskbar moves.
-
Re: Task Bar Metrics - when is the Taskbar Auto Hidden?
So you are both saying that there is a manual adjustment which is not displayed in the task and start bar menu properties like on seven -> Trying it, yep it worked when I right clicked the task bar and unchecked the "lock the taskbar" setting and then manually left clicked while dragging only from any uncovered section of the taskbar - not sure how many users will know how to get this functional in XP as it is not really very intuitive to get it working. This example reminds me though, I have noticed when using XP the windows file explorer displays the location in the windows caption (eg c:\program files) although in in Seven it is displayed in a drop down list box which allows me to copy and paste the location - is there a way to force XP to display the full path so that I can copy it?
-
Re: Task Bar Metrics - when is the Taskbar Auto Hidden?
Since Windows 95 I have seen many users (perhaps as much as 5%) put the taskbar in places other than the bottom of the screen.
To force XP (or 2000, etc) to show the full path in the address bar in Explorer, go to Tools -> Options, and tick the relevant box (I can't remember what it is called unfortunately, but it should be fairly obvious).
-
Re: Task Bar Metrics - when is the Taskbar Auto Hidden?
Hey thanks si_the_geek, after changing the Tools -> Options section, I had to right click the toolbar section, check "address bar", uncheck "lock the toolbars", then repositioned it, and it works good now :).
Once other thing, I was just trying to tidy up my code which lead me to this -> can I shortcut a loop to next somehow? like so:
Code:
Private Sub Command1_Click()
Dim i&, j&
For i = 1 To 10
If j > 100 Then Next i ' shortcut to next i?
' do some other code
Next i
End Sub
or do I have to do it via nesting ie:
Code:
Private Sub Command1_Click()
Dim i&, j&
For i = 1 To 10
If j < 101 Then
' do some other code
End If
Next i
End Sub
-
Re: Task Bar Metrics - when is the Taskbar Auto Hidden?
You need to use nesting I'm afraid.
-
Re: Task Bar Metrics - when is the Taskbar Auto Hidden?
ok, I'll leave the code as is.
-
Re: Task Bar Metrics - when is the Taskbar Auto Hidden?
Quote:
Originally Posted by
Witis
Hey thanks si_the_geek, after changing the Tools -> Options section, I had to right click the toolbar section, check "address bar", uncheck "lock the toolbars", then repositioned it, and it works good now :).
Once other thing, I was just trying to tidy up my code which lead me to this -> can I shortcut a loop to next somehow? like so:
Code:
Private Sub Command1_Click()
Dim i&, j&
For i = 1 To 10
If j > 100 Then Next i ' shortcut to next i?
' do some other code
Next i
End Sub
or do I have to do it via nesting ie:
Code:
Private Sub Command1_Click()
Dim i&, j&
For i = 1 To 10
If j < 101 Then
' do some other code
End If
Next i
End Sub
there's a command called "end for" or perhaps "exit for" i can't remember which, but it exits a for loop early. You could also set i to 10.
-
Re: [RESOLVED] Task Bar Metrics - when is the Taskbar Auto Hidden?
Hi Lord Orwell
I am trying not to exit the loop, instead I would like to skip to the next couter within the loop ie goto next i or just next, although I don't think it is possible as per si_the_geek, it seems only possible to exit the entire loop using exit for or i = max counter.
-
Re: Task Bar Metrics - when is the Taskbar Auto Hidden?
Quote:
Originally Posted by
Witis
is there a way to force XP to display the full path so that I can copy it?
it's a setting in folder options. You can even turn on an address bar for every folder, if you try hard enough.
Quote:
I am trying not to exit the loop, instead I would like to skip to the next couter within the loop ie goto next i or just next, although I don't think it is possible as per si_the_geek, it seems only possible to exit the entire loop using exit for or i = max counter.
i don't think i understand what you are asking. Your code above already does that because if the "if" statement is false, it's jumping straight to the "next".
are you perhaps against putting a huge block of code in there for readability? If so, put it in a function and call it.
-
Re: [RESOLVED] Task Bar Metrics - when is the Taskbar Auto Hidden?
Lord Orwell
Yes thanks I eventually got XP to display the full path in the address bar - although it wasn't very intuitive to get it running.
Regarding my code tidying:
The problem is that the code I posted before doesn't work, ie this line fails:
If j > 100 Then Next i ' shortcut to next i?
I also tried next with no luck, I think there is no way to do it.
-
Re: [RESOLVED] Task Bar Metrics - when is the Taskbar Auto Hidden?
Quote:
Originally Posted by
Witis
Lord Orwell
Yes thanks I eventually got XP to display the full path in the address bar - although it wasn't very intuitive to get it running.
Regarding my code tidying:
The problem is that the code I posted before doesn't work, ie this line fails:
If j > 100 Then Next i ' shortcut to next i?
I also tried next with no luck, I think there is no way to do it.
not what i was saying. Your WORKING code already does that. You don't run the if block at all if the value is under 101 so next is called as the next code execution. I don't see why you have an issue with it done that way. It is proper code.
-
Re: [RESOLVED] Task Bar Metrics - when is the Taskbar Auto Hidden?
Witis is aware that the If j < 101 version works, he just wanted to know if there is a way to jump directly to the Next rather than having to use nested If's - which can increase the indenting significantly if there are several places in the loop where you want to do it.