Programmatically resize Windows taskbar

Posted on 2006-04-18
Last Modified: 2012-06-27
Does anyone know how to programmatically resize the Windows taskbar?

I'm trying to "minimize" the taskbar to allow for a IE browser based application to take up the entire screen.  I've been able to hide the taskbar using the following code:
ShowWindow(FindWindow("Shell_TrayWnd", vbnullString), SW_HIDE)

But that does not allow for other windows to be resized where the toolbar used to be.  But if I manually resize the taskbar to be minimized, then I can make my app fit the entire screen.  

Any suggestions?
Question by:CaitlinRoberts
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    If you're using IE have you considered putting it into full screen mode?  Press F11 in IE to see what I mean...

    Author Comment

    Yes, that's a great suggestion.  However, as part of amy requirements I need to disable the minimize, maximize, close, and IE toolbar functions.  And it seems like the only way to do this without opening a new window is by resizing the window and moving it so that the toolbar is out of sight.  Although that's not the best of solutions, it's all I have to work with right now.  

    So right now, I can move the browser to hide the IE toolbar.  And I can hide the toolbar using the ShowWindow(FindWindow("shell_trayWnd",null), SW_HIDE) command, but if I use that command, I cannot resize the window to cover the portion of the screen where the taskbar used to be.
    LVL 96

    Accepted Solution

    You need to use the SHAppBarMessage API call when changing the state of the AppBar, or the other windows won't know about the change.  

    Here is a class that I use (somewhat untested):

    ' Interesting reading:
    ' The Old New Thing (Raymond Chen) -- How to Query Properties of the TaskBar
    '  <Quote>
    '    You can also use the ABM_SETSTATE message to change these states.
    '    Note that doing so from a program is discouraged; these state bits
    '    belong to the user's preferences. A program shouldn't be messing
    '    with the user's preferences. (Well, unless the whole point of the
    '    program is to change the user's preferences, of course. But the
    '    frequency with which I see this question makes me wonder whether
    '    there really are that many settings-tweaking programs out there.
    '    I suspect people are using this power for evil, not for good.)
    '    And to stave off follow-up questions: ABS_ALWAYSONTOP and ABS_AUTOHIDE
    '    are the only two properties of the taskbar that are programmable.
    '  </Quote>

    Imports System.Runtime.InteropServices

    Public Class AppBar

      Public Enum AppBarPosition
      End Enum   'AppBarPosition

      Private Const ABM_NEW As Int32 = &H0
      Private Const ABM_REMOVE As Int32 = &H1
      Private Const ABM_QUERYPOS As Int32 = &H2
      Private Const ABM_SETPOS As Int32 = &H3S
      Private Const ABM_GETSTATE As Int32 = &H4
      Private Const ABM_GETTASKBARPOS As Int32 = &H5
      Private Const ABM_ACTIVATE As Int32 = &H6
      Private Const ABM_GETAUTOHIDEBAR As Int32 = &H7
      Private Const ABM_SETAUTOHIDEBAR As Int32 = &H8
      Private Const ABM_WINDOWPOSCHANGED As Int32 = &H9
      Private Const ABM_SETSTATE As Int32 = &HA
      Private Const ABN_STATECHANGE As Int32 = &H0
      Private Const ABN_POSCHANGED As Int32 = &H1
      Private Const ABN_FULLSCREENAPP As Int32 = &H2
      Private Const ABN_WINDOWARRANGE As Int32 = &H3

      Private Const ABS_AUTOHIDE As Int32 = &H1
      Private Const ABS_ALWAYSONTOP As Int32 = &H2

      Private Const ABE_LEFT As Int32 = &H0
      Private Const ABE_TOP As Int32 = &H1
      Private Const ABE_RIGHT As Int32 = &H2
      Private Const ABE_BOTTOM As Int32 = &H3

      Private Declare Function GetAppBarMessage Lib "shell32" Alias "SHAppBarMessage" _
         (ByVal dwMessage As Integer, ByRef pData As APPBARDATA) As Integer

      Private Declare Function SetAppBarMessage Lib "shell32" Alias "SHAppBarMessage" _
       (ByVal dwMessage As Integer, ByVal pData As APPBARDATA) As Integer

      Private Structure APPBARDATA
        Dim cbSize As Integer
        Dim hwnd As Integer
        Dim uCallbackMessage As Integer
        Dim uEdge As Integer
        Dim rc As Rectangle
        Dim lParam As Integer     'message specific
      End Structure

      Private m_autoHide As Boolean
      Private m_alwaysOnTop As Boolean
      Private m_dockPosition As AppBarPosition = AppBarPosition.Unknown
      Private m_bounds As Rectangle

      Public Sub New()
      End Sub

      Private Sub GetState()

        Dim rc As New Rectangle
        Dim data As New APPBARDATA

        ' Initialize the APPBARDATA structure
        data.cbSize = Marshal.SizeOf(data)

        ' Get the appbar state  
        Dim state As Int32 = GetAppBarMessage(ABM_GETSTATE, data)

        Select Case state

          Case 0
            m_autoHide = False
            m_alwaysOnTop = False

          Case ABS_ALWAYSONTOP
            m_autoHide = False
            m_alwaysOnTop = True

          Case Else
            m_autoHide = True

        End Select     'state

        ' See which edge has a taskbar
        For position As Int32 = ABE_LEFT To ABE_BOTTOM

          data.uEdge = position
          Dim hWndAppBar As Int32 = GetAppBarMessage(ABM_GETAUTOHIDEBAR, data)

          If hWndAppBar > 0 Then

            Select Case position

              Case ABE_TOP
                m_dockPosition = AppBarPosition.Top

              Case ABE_LEFT
                m_dockPosition = AppBarPosition.Left

              Case ABE_RIGHT
                m_dockPosition = AppBarPosition.Right

              Case ABE_BOTTOM
                m_dockPosition = AppBarPosition.Bottom

            End Select          'position

          End If

        Next position

        Dim pos As Int32 = GetAppBarMessage(ABM_GETTASKBARPOS, data)

        If pos > 0 Then
          m_bounds = data.rc
        End If

      End Sub   'GetState

      Private Sub SetState()

        Dim data As New APPBARDATA
        data.cbSize = Marshal.SizeOf(data)

        If Me.AutoHide Then
          data.lParam = ABS_AUTOHIDE
        End If

        If Me.AlwaysOnTop Then
          data.lParam = data.lParam Or ABS_ALWAYSONTOP
        End If

        SetAppBarMessage(ABM_SETSTATE, data)

      End Sub  'SetState

      Public Property AutoHide() As Boolean
          Return m_autoHide
        End Get
        Set(ByVal Value As Boolean)
        End Set
      End Property   'AutoHide

      Public Property AlwaysOnTop() As Boolean
          Return m_alwaysOnTop
        End Get
        Set(ByVal Value As Boolean)
          m_alwaysOnTop = Value
        End Set
      End Property   'AlwaysOnTop

      Public ReadOnly Property DockPosition() As AppBarPosition
          Return m_dockPosition
        End Get
      End Property   'DockPosition

      Public ReadOnly Property Bounds() As Rectangle
          Return m_bounds
        End Get
      End Property   'Bounds

      Public ReadOnly Property Left() As Integer
          Return m_bounds.Left
        End Get
      End Property  'Left

      Public ReadOnly Property Top() As Integer
          Return m_bounds.Top
        End Get
      End Property  'Top

      Public ReadOnly Property Location() As Point
          Return m_bounds.Location
        End Get
      End Property  'Location

      Public ReadOnly Property Size() As Size
          Return m_bounds.Size
        End Get
      End Property  'Size

    End Class


    Author Comment

    Thanks for post!  Looks exactly like what I needed.  I'm just having a little trouble get it to work.  
    I get a "Object reference not set to an instance of an object" error when it's trying to call the SetAppBarMessage method.  Any suggestions?


    Author Comment

    I've got it!!  It looks like it didn't like the ByVal in:
      Private Declare Function SetAppBarMessage Lib "shell32" Alias "SHAppBarMessage" _
       (ByVal dwMessage As Integer, ByVal pData As APPBARDATA) As Integer
      ...for the pData.

    It seems like it need to be passed in by reference.  Thanks so much for your help, you have no idea how long I've been working on this problem!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Article by: jpaulino
    XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String ( Literal, only instead of starting and ending with w…
    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now