Public Class Sidebar
#Region "Sidebar Declarations"
    Private Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" (ByVal hwnd As IntPtr, ByVal hWndInsertAfter As Integer, ByVal x As Integer, ByVal y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer
    Public Declare Auto Function MoveWindow Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal X As Int32, ByVal Y As Int32, ByVal nWidth As Int32, ByVal nHeight As Int32, ByVal bRepaint As Boolean) As Boolean
    Declare Function SHAppBarMessage Lib "shell32.dll" Alias "SHAppBarMessage" (ByVal dwMessage As Integer, ByRef pData As APPBARDATA) As Integer
    Declare Function SetWindowPos Lib "user32.dll" (ByVal hwnd As Integer, ByVal hWndInsertAfter As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal cX As Integer, ByVal cY As Integer, ByVal wFlags As Integer) As Integer

    Structure APPBARDATA
        Dim cbSize As Integer
        Dim hwnd As Integer
        Dim uCallbackMessage As [Delegate]
        Dim uEdge As Integer
        Dim rc As RECT
        Dim lParam As Integer ' message specific
    End Structure

    Structure RECT
        Dim Left As Integer
        Dim Top As Integer
        Dim Right As Integer
        Dim Bottom As Integer
    End Structure

    Const ABE_LEFT As Integer = 0
    Const ABE_TOP As Integer = &H1
    Const ABE_RIGHT As Integer = 2
    Const ABE_BOTTOM As Integer = 3

    Const ABM_NEW As Integer = 0
    Const ABM_REMOVE As Integer = 1
    Const ABM_QUERYPOS As Integer = 2
    Const ABM_SETPOS As Integer = &H3
    Const ABM_GETSTATE As Integer = 4
    Const ABM_GETTASKBARPOS As Integer = 5
    Const ABM_ACTIVATE As Integer = 6
    Const ABM_GETAUTOHIDEBAR As Integer = 7
    Const ABM_SETAUTOHIDEBAR As Integer = 8
    Const ABM_WINDOWPOSCHANGED As Integer = 9

    Const ABS_AUTOHIDE As Integer = 1
    Const ABS_ALWAYSONTOP As Integer = 2

    Const HWND_NOTTOPMOST As Integer = -2
    Const HWND_TOPMOST As Integer = -1
    Const HWND_TOP As Integer = 0
    Const SHOWNORMAL As Integer = 5

    Const SWP_NOSIZE As Integer = &H1
    Const SWP_NOMOVE As Short = &H2
    Const SWP_NOZORDER As Integer = 4
    Const SWP_NOACTIVATE As Integer = &H10
    Const SWP_DRAWFRAME As Integer = &H20
    Const SWP_SHOWWINDOW As Integer = &H40
#End Region

    Private Sidebar As APPBARDATA
    Private Handle As IntPtr

    Public Sub New(pHandle As IntPtr)
        Handle = pHandle
    End Sub

    Public Sub RegisterSidebar(pScreenName As String)
        Sidebar.hwnd = Handle.ToInt32
        Sidebar.cbSize = Len(Sidebar)

        Dim oSelectedScreen = System.Windows.Forms.Screen.PrimaryScreen

        ' TODO: Make Sidebar Screen configurable
        'If pScreenName <> "" Then
        '    Dim oScreens = System.Windows.Forms.Screen.AllScreens
        '    For Each oScreen In oScreens
        '        If oScreen.DeviceName = pScreenName Then
        '            oSelectedScreen = oScreen
        '        End If
        '    Next
        'End If

        With Sidebar
            .uEdge = ABE_RIGHT
            .rc.Top = oSelectedScreen.WorkingArea.Top  '0
            .rc.Right = oSelectedScreen.WorkingArea.Right ' right
            .rc.Left = oSelectedScreen.WorkingArea.Right - 200 ' width of our appbar
            .rc.Bottom = oSelectedScreen.WorkingArea.Height ' bottom
            SHAppBarMessage(ABM_NEW, Sidebar)
            SetWindowPos(Sidebar.hwnd, HWND_TOP, .rc.Left, .rc.Top, .rc.Right - .rc.Left, .rc.Bottom, SWP_SHOWWINDOW Or SWP_NOACTIVATE)
            SHAppBarMessage(ABM_SETPOS, Sidebar)
        End With
    End Sub

    Public Sub UnregisterSidebar()
        SHAppBarMessage(ABM_REMOVE, Sidebar)
    End Sub
End Class
