+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 21

Thread: toggle taskbar

  1. #1
    Valued Forum Contributor
    Join Date
    06-27-2006
    Posts
    230

    toggle taskbar

    I found this code on the web for hiding the windows taskbar.

    Option 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
    It works as advertised

    What I would like to do is read a value so that I can toggle between show/hide.

    Any help is appreciated

  2. #2
    Forum Guru mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,984
    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 assisted or failed to assist you I welcome your Feedback.

  3. #3
    Valued Forum Contributor
    Join Date
    06-27-2006
    Posts
    230
    That is my question

    how do I test for whether the taskbar is visible ?

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979
    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.
    Private 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
    Macro to test if the Taskbar is Minimized
    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
    Sincerely,
    Leith Ross

  5. #5
    Valued Forum Contributor
    Join Date
    06-27-2006
    Posts
    230
    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.

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979
    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

  7. #7
    Valued Forum Contributor
    Join Date
    06-27-2006
    Posts
    230
    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.

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979
    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...
    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
    Sincerely,
    Leith Ross

  9. #9
    Valued Forum Contributor
    Join Date
    06-27-2006
    Posts
    230
    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.

  10. #10
    Valued Forum Contributor
    Join Date
    06-27-2006
    Posts
    230
    Leith after doing some searching I found this function
    Public Declare Function IsWindowVisible Lib "user32" ( ByVal hwnd As Long)As Long
    So I tried this :
    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
    It seems to be working.
    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.

  11. #11
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979
    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.

  12. #12
    Valued Forum Contributor
    Join Date
    06-27-2006
    Posts
    230
    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.

  13. #13
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979
    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

  14. #14
    Valued Forum Contributor
    Join Date
    06-27-2006
    Posts
    230
    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

  15. #15
    Valued Forum Contributor
    Join Date
    06-27-2006
    Posts
    230
    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

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0