Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

SHAppBarMessage (shell32)
 
.
Summary
The SHAppBarMessage API. Uses the APPBARDATA structure.

C# Signature:

<summary>
     Sends an appbar message to the system.
     <para>
     Go to https://msdn.microsoft.com/en-us/library/windows/desktop/bb762108%28v=vs.85%29.aspx for more
     information.
     </para>

</summary>

/// <param name="dwMessage">
///     C++ ( dwMessage [in] Type: DWORD )<br />Appbar message value to send. This parameter can be one of the following
///     values.<br />
///     <list type="table">
///     <listheader>
///         <term>Possible AppBar Values</term>
///         <description>Any possible app bar value used int he function</description>
///     </listheader>
///     <item>
///         <term>ABM_NEW (0x00000000)</term>
///         <description>
///         Registers a new appbar and specifies the message identifier that the system should use to
///         send notification messages to the appbar.
///         </description>
///     </item>
///     <item>
///         <term>ABM_REMOVE (0x00000001)</term>
///         <description>Unregisters an appbar, removing the bar from the system's internal list.</description>
///     </item>
///     <item>
///         <term>ABM_QUERYPOS (0x00000002)</term>
///         <description>Requests a size and screen position for an appbar.</description>
///     </item>
///     <item>
///         <term>ABM_SETPOS (0x00000003)</term>
///         <description>Sets the size and screen position of an appbar.</description>
///     </item>
///     <item>
///         <term>ABM_GETSTATE (0x00000004)</term>
///         <description>Retrieves the autohide and always-on-top states of the Windows taskbar.</description>
///     </item>
///     <item>
///         <term>ABM_GETTASKBARPOS (0x00000005)</term>
///         <description>
///         Retrieves the bounding rectangle of the Windows taskbar. Note that this applies only to the
///         system taskbar. Other objects, particularly toolbars supplied with third-party software, also can be
///         present. As a result, some of the screen area not covered by the Windows taskbar might not be visible
///         to the user. To retrieve the area of the screen not covered by both the taskbar and other app bars—the
///         working area available to your application—, use the GetMonitorInfo function.
///         </description>
///     </item>
///     <item>
///         <term>ABM_ACTIVATE (0x00000006)</term>
///         <description>
///         Notifies the system to activate or deactivate an appbar. The lParam member of the APPBARDATA
///         pointed to by pData is set to TRUE to activate or FALSE to deactivate.
///         </description>
///     </item>
///     <item>
///         <term>ABM_GETAUTOHIDEBAR (0x00000007)</term>
///         <description>Retrieves the handle to the autohide appbar associated with a particular edge of the screen.</description>
///     </item>
///     <item>
///         <term>ABM_SETAUTOHIDEBAR (0x00000008)</term>
///         <description>Registers or unregisters an autohide appbar for an edge of the screen.</description>
///     </item>
///     <item>
///         <term>ABM_WINDOWPOSCHANGED (0x00000009)</term>
///         <description>Notifies the system when an appbar's position has changed.</description>
///     </item>
///     <item>
///         <term>ABM_SETSTATE (0x0000000A)</term>
///         <description>Windows XP and later: Sets the state of the appbar's autohide and always-on-top attributes.</description>
///     </item>
///     </list>
/// </param>
/// <param name="pData">
///     C++ ( pData [in, out] Type: PAPPBARDATA )<br /> A pointer to an APPBARDATA structure. The content
///     of the structure on entry and on exit depends on the value set in the dwMessage parameter. See the individual
///     message pages for specifics.
/// </param>
/// <returns>
///     C++ ( Type: UINT_PTR )<br />This function returns a message-dependent value. For more information, see the
///     Windows SDK documentation for thespecific appbar message sent.
/// </returns>
[DllImport("shell32.dll")]
static extern IntPtr SHAppBarMessage(uint dwMessage,
   [In] ref APPBARDATA pData);

VB Signature:

<DllImport("SHELL32", CallingConvention:=CallingConvention.StdCall)> _
  Shared Function SHAppBarMessage(ByVal dwMessage As Integer, ByRef pData As APPBARDATA) As Integer
  End Function

User-Defined Types:

None.

Notes:

None.

Tips & Tricks:

VB Sample Class

Imports System
Imports System.Text
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Timers
Imports System.Runtime.InteropServices

Public Class AppBar

#Region " Class Variablen"
    Private DockSize As Size
    Private MyDockForm As System.Windows.Forms.Form
    Private fBarRegistered As Boolean = False
    Private MyEdge As ABEdge
    Friend uCallBack As Integer
    Dim PosfixTimer As New System.Timers.Timer
#End Region

#Region " Structure und Enum"

    <StructLayout(LayoutKind.Sequential)> _
    Public Structure RECT
    Public left As Integer
    Public top As Integer
    Public right As Integer
    Public bottom As Integer
    End Structure

    <StructLayout(LayoutKind.Sequential)> _
    Structure APPBARDATA
    Public cbSize As Integer
    Public hWnd As IntPtr
    Public uCallbackMessage As Integer
    Public uEdge As Integer
    Public rc As RECT
    Public lParam As Boolean
    End Structure

    <StructLayout(LayoutKind.Sequential)> _
    Public Structure TaskbarState
    Public T_EDGE As ABEdge
    Public T_STATE As ABState
    Public T_SIZE As RECT
    End Structure

    Public Enum ABMsg
    ABM_NEW = 0
    ABM_REMOVE = 1
    ABM_QUERYPOS = 2
    ABM_SETPOS = 3
    ABM_GETSTATE = 4
    ABM_GETTASKBARPOS = 5
    ABM_ACTIVATE = 6
    ABM_GETAUTOHIDEBAR = 7
    ABM_SETAUTOHIDEBAR = 8
    ABM_WINDOWPOSCHANGED = 9
    ABM_SETSTATE = 10
    End Enum

    Public Enum ABState
    ABS_MANUAL = &H0
    ABS_AUTOHIDE = &H1
    ABS_ALWAYSONTOP = &H2
    ABS_AUTOHIDEANDONTOP = &H3
    End Enum

    Public Enum ABNotify
    ABN_STATECHANGE = 0
    ABN_POSCHANGED
    ABN_FULLSCREENAPP
    ABN_WINDOWARRANGE
    End Enum

    Enum ABEdge
    ABE_LEFT = 0
    ABE_TOP = 1
    ABE_RIGHT = 2
    ABE_BOTTOM = 3
    End Enum

    Public Enum AppBarDockStyle As Integer
    None
    ScreenLeft
    ScreenTop
    ScreenRight
    ScreenBottom
    End Enum
#End Region

#Region " DLLImports"

    <DllImport("SHELL32", CallingConvention:=CallingConvention.StdCall)> _
    Shared Function SHAppBarMessage(ByVal dwMessage As Integer, ByRef pData As APPBARDATA) As Integer
    End Function

    <DllImport("user32.dll")> _
    Shared Function GetSystemMetrics(ByVal Index As Integer) As Integer
    End Function

    <DllImport("user32.dll", ExactSpelling:=True)> _
    Function LockWorkStation() As Boolean
    End Function

    <DllImport("User32.dll", ExactSpelling:=True, CharSet:=System.Runtime.InteropServices.CharSet.Auto)> _
    Private Shared Function MoveWindow(ByVal hWnd As IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal cx As Integer, ByVal cy As Integer,
                       ByVal repaint As Boolean) As Boolean
    End Function

    <DllImport("User32.dll", CharSet:=CharSet.Auto)> _
    Private Shared Function RegisterWindowMessage(ByVal msg As String) As Integer
    End Function

    Private Declare Auto Function FindWindow Lib "user32" ( ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr

#End Region

#Region " Propertys"

    Public ReadOnly Property IsBarRegistered() As Boolean
    Get
        Return fBarRegistered
    End Get
    End Property

    Public Property AppBarDockSize() As Size
    Get
        Return DockSize
    End Get
    Set(ByVal Value As Size)
        DockSize = Value
    End Set
    End Property

    Private Property AppBarEdge() As ABEdge
    Get
        Return MyEdge
    End Get
    Set(ByVal Value As ABEdge)
        MyEdge = Value
    End Set
    End Property

#End Region

    'Constructor
    Friend Sub New(ByRef DockForm As System.Windows.Forms.Form, ByVal dockStyle As AppBarDockStyle, ByVal AppBarSize As Size)

    'Form Übergeben
    MyDockForm = DockForm

    'Dockstyles setzen
    UpdateDockedAppBarPosition(dockStyle)
    AppBarDockSize = AppBarSize

    'Eventhandler Initialisieren
    AddHandler MyDockForm.Load, AddressOf Me.OnLoad
    AddHandler MyDockForm.Closing, AddressOf Me.OnClosing

    End Sub

    'Pos Form
    Public Sub UpdateDockedAppBarPosition(ByVal dockStyle As AppBarDockStyle)
    Select Case dockStyle
        Case AppBarDockStyle.None : Return
        Case AppBarDockStyle.ScreenLeft : AppBarEdge = ABEdge.ABE_LEFT
        Case AppBarDockStyle.ScreenTop : AppBarEdge = ABEdge.ABE_TOP
        Case AppBarDockStyle.ScreenRight : AppBarEdge = ABEdge.ABE_RIGHT
        Case AppBarDockStyle.ScreenBottom : AppBarEdge = ABEdge.ABE_BOTTOM
        Case Else : AppBarEdge = ABEdge.ABE_TOP
    End Select
    End Sub

    'Register (ABSetPos)
    Friend Sub RegisterBar(ByVal hWnd As IntPtr, ByRef uCallbackMessage As Int32, ByVal idealSize As Size, ByVal DockEdge As ABEdge)
    Dim abd As APPBARDATA = New APPBARDATA

    abd.cbSize = Marshal.SizeOf(abd)
    abd.hWnd = hWnd
    AppBarEdge = DockEdge

    uCallbackMessage = RegisterWindowMessage("AppBarMessage")
    abd.uCallbackMessage = uCallbackMessage
    Dim ret As Long = SHAppBarMessage(CType(ABMsg.ABM_NEW, Integer), abd)
    fBarRegistered = True
    ABSetPos(hWnd, idealSize, DockEdge)
    End Sub

    'UnregAppBar
    Friend Sub UnregisterAppBar(ByVal hWnd As IntPtr)
    Dim abd As APPBARDATA = New APPBARDATA
    abd.cbSize = Marshal.SizeOf(abd)
    abd.hWnd = hWnd
    SHAppBarMessage(ABMsg.ABM_REMOVE, abd)
    End Sub

    'Reg Form as Appbar
    Friend Sub ABSetPos(ByVal hWnd As IntPtr, ByVal idealSize As Size, ByVal DockEdge As ABEdge)
    Dim abd As APPBARDATA = New APPBARDATA

    abd.cbSize = Marshal.SizeOf(abd)
    abd.hWnd = hWnd
    abd.uEdge = CType(ABEdge.ABE_TOP, Integer)
    abd.uEdge = CType(DockEdge, Integer)
    If abd.uEdge = CType(ABEdge.ABE_LEFT, Integer) OrElse abd.uEdge = CType(ABEdge.ABE_RIGHT, Integer) Then
        abd.rc.top = 0
        abd.rc.bottom = SystemInformation.PrimaryMonitorSize.Height
        If abd.uEdge = CType(ABEdge.ABE_LEFT, Integer) Then
        abd.rc.left = 0
        abd.rc.right = idealSize.Width
        Else
        abd.rc.right = SystemInformation.PrimaryMonitorSize.Width
        abd.rc.left = abd.rc.right - idealSize.Width
        End If
    Else
        abd.rc.left = 0
        abd.rc.right = SystemInformation.PrimaryMonitorSize.Width
        If abd.uEdge = CType(ABEdge.ABE_TOP, Integer) Then
        abd.rc.top = 0
        abd.rc.bottom = idealSize.Height
        Else
        abd.rc.bottom = SystemInformation.PrimaryMonitorSize.Height
        abd.rc.top = abd.rc.bottom - idealSize.Height
        End If
    End If
    SHAppBarMessage(CType(ABMsg.ABM_QUERYPOS, Integer), abd)
    Select Case abd.uEdge
        Case CType(ABEdge.ABE_LEFT, Integer)
        abd.rc.right = abd.rc.left + idealSize.Width
        ' break
        Case CType(ABEdge.ABE_RIGHT, Integer)
        abd.rc.left = abd.rc.right - idealSize.Width
        ' break
        Case CType(ABEdge.ABE_TOP, Integer)
        abd.rc.bottom = abd.rc.top + idealSize.Height
        ' break
        Case CType(ABEdge.ABE_BOTTOM, Integer)
        abd.rc.top = abd.rc.bottom - idealSize.Height
        ' break
    End Select

    SHAppBarMessage(CType(ABMsg.ABM_SETPOS, Integer), abd)
    MoveWindow(abd.hWnd, abd.rc.left, abd.rc.top, abd.rc.right - abd.rc.left, abd.rc.bottom - abd.rc.top, True)
    End Sub

    Public Sub SetAutoHide()
    Dim lRet As Long
    Dim abd As APPBARDATA = New APPBARDATA
    abd.cbSize = Marshal.SizeOf(abd)
    abd.uEdge = ABEdge.ABE_RIGHT
    'abd.hWnd = FindWindow("Shell_TrayWnd", "") 'Tasbar Handle
    abd.hWnd = MyDockForm.Handle
    abd.lParam = True

    lRet = SHAppBarMessage(ABMsg.ABM_SETAUTOHIDEBAR, abd)
    End Sub

    Friend Function GetWinTaskbarState(ByRef WinTaskBar As TaskbarState) As Boolean
    Dim hWndAppBar As Long
    Dim ABD As New APPBARDATA
    Dim state As Long, tsize As Long
    Dim msg As New StringBuilder
    Dim position As Integer
    Dim retState As TaskbarState

    Try
        ABD.cbSize = Marshal.SizeOf(ABD)
        ABD.uEdge = 0
        ABD.hWnd = FindWindow("Shell_TrayWnd", "")
        ABD.lParam = True

        'get the appbar state  
        state = SHAppBarMessage(CType(ABMsg.ABM_GETSTATE, Integer), ABD)

        Select Case state
        Case ABState.ABS_MANUAL
            retState.T_STATE = ABState.ABS_MANUAL
        Case ABState.ABS_ALWAYSONTOP
            retState.T_STATE = ABState.ABS_ALWAYSONTOP
        Case ABState.ABS_AUTOHIDE
            retState.T_STATE = ABState.ABS_AUTOHIDE
        Case ABState.ABS_AUTOHIDEANDONTOP
            retState.T_STATE = ABState.ABS_AUTOHIDEANDONTOP
        End Select

        'see which edge has a taskbar
        For position = ABEdge.ABE_LEFT To ABEdge.ABE_BOTTOM
        ABD.uEdge = position
        hWndAppBar = SHAppBarMessage(CType(ABMsg.ABM_GETAUTOHIDEBAR, Integer), ABD)

        If hWndAppBar > 0 Then
            Select Case position
            Case ABEdge.ABE_LEFT : retState.T_EDGE = ABEdge.ABE_LEFT
            Case ABEdge.ABE_TOP : retState.T_EDGE = ABEdge.ABE_TOP
            Case ABEdge.ABE_RIGHT : retState.T_EDGE = ABEdge.ABE_RIGHT
            Case ABEdge.ABE_BOTTOM : retState.T_EDGE = ABEdge.ABE_BOTTOM
            End Select
        End If
        Next

        tsize = SHAppBarMessage(CType(ABMsg.ABM_GETTASKBARPOS, Integer), ABD)
        retState.T_SIZE = ABD.rc

        'Rückgabe ob sich der TaskbarStatus verändert hat
        WinTaskBar = retState
        Return True

    Catch err As Exception
        Trace.Assert(False)
        Return False
    End Try
    End Function

    'Eventhandler Form Load
    Private Overloads Sub OnLoad(ByVal sender As Object, ByVal e As System.EventArgs)
    'UpdateDockedAppBarPosition(RegisterAppBar.AppBarDockStyle.ScreenRight)
    End Sub

    'Eventhandler Form Close
    Private Overloads Sub OnClosing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
    UnregisterAppBar(MyDockForm.Handle)
    End Sub
End Class

Sample Code:

Alternative Managed API:

Do you know one? Please contribute it!

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions