Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AppBar VB.Net

Status
Not open for further replies.

bigtimmin

MIS
Apr 12, 2005
125
US
Has anyone seen a VB.Net example of an Appbar. I've seen tons of C# and VB.6. I've tried converting the VB.6 but constantly run in to problems. Help!

Maybe this world is another planet’s Hell.
Aldous Huxley

eyes_015.gif___1129027102664
eyes_015.gif___1129027102664
 
Converting from C# might actually be easier. If you need help on parts, or if the code isn't very large I can help you with it.

Regards, Ruffnekk
---
Do not underestimate the power of simplicity!
 
Well I guess that is a No! Converted C# to VB.Net 2003 based on the link provide by jebenson still not working.
Code:
Imports System.Runtime.InteropServices
    Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        RegisterBar()
    End Sub
    Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        RegisterBar()
    End Sub

#Region " AppBar "
    <StructLayout(LayoutKind.Sequential)> 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 IntPtr
    End Structure
    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
    Enum ABNotify
        ABN_STATECHANGE = 0
        ABN_POSCHANGED
        ABN_FULLSCREENAPP
        ABN_WINDOWARRANGE
    End Enum
    Enum ABEdge
        ABE_LEFT = 0
        ABE_TOP
        ABE_RIGHT
        ABE_BOTTOM
    End Enum
    Private fBarRegistered As Boolean = False
    <DllImport("SHELL32", CallingConvention:=CallingConvention.StdCall)> _
    Public Shared Function SHAppBarMessage(ByVal dwMessage As Integer, ByRef BarrData As APPBARDATA) As Integer
    End Function
    <DllImport("USER32")> _
    Public Shared Function GetSystemmetric(ByVal Index As Integer) As Integer
    End Function
    <DllImport("User32.dll", ExactSpelling:=True, CharSet:=System.Runtime.InteropServices.CharSet.Auto)> _
    Public 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)> _
    Public Shared Function RegisterWindowMessage(ByVal msg As String) As Integer
    End Function
    Private uCallBack As Integer

    Private Sub RegisterBar()
        Dim abd As New APPBARDATA
        Dim ret As Integer
        abd.cbSize = Marshal.SizeOf(abd)
        abd.hWnd = Me.Handle
        If fBarRegistered Then
            uCallBack = RegisterWindowMessage("AppBarMessage")
            abd.uCallbackMessage = uCallBack

            ret = SHAppBarMessage(ABMsg.ABM_NEW, abd)
            fBarRegistered = True

            ABSetPos()
        Else
            ret = SHAppBarMessage(ABMsg.ABM_REMOVE, abd)
            fBarRegistered = False
        End If
    End Sub

    Private Sub ABSetPos()
        Dim abd As New APPBARDATA
        abd.cbSize = Marshal.SizeOf(abd)
        abd.hWnd = Me.Handle
        abd.uEdge = ABEdge.ABE_TOP

        If abd.uEdge = ABEdge.ABE_LEFT OrElse abd.uEdge = ABEdge.ABE_RIGHT Then
            abd.rc.top = 0
            abd.rc.bottom = SystemInformation.PrimaryMonitorSize.Height
            If abd.uEdge = ABEdge.ABE_LEFT Then
                abd.rc.left = 0
                abd.rc.right = Size.Width
            Else
                abd.rc.right = SystemInformation.PrimaryMonitorSize.Width
                abd.rc.left = abd.rc.right - Size.Width
            End If
        Else
            abd.rc.left = 0
            abd.rc.right = SystemInformation.PrimaryMonitorSize.Width
            If abd.uEdge = ABEdge.ABE_TOP Then
                abd.rc.top = 0
                abd.rc.bottom = Size.Height
            Else
                abd.rc.bottom = SystemInformation.PrimaryMonitorSize.Height
                abd.rc.top = abd.rc.bottom - Size.Height
            End If
        End If

        'Query the system for an approved size and position. 
        SHAppBarMessage(ABMsg.ABM_QUERYPOS, abd)

        'Adjust the rectangle, depending on the edge to which the appbar is anchored. 
        Select Case abd.uEdge
            Case ABEdge.ABE_LEFT
                abd.rc.right = abd.rc.left + Size.Width
            Case ABEdge.ABE_RIGHT
                abd.rc.left = abd.rc.right - Size.Width
            Case ABEdge.ABE_TOP
                abd.rc.bottom = abd.rc.top + Size.Height
            Case ABEdge.ABE_BOTTOM
                abd.rc.top = abd.rc.bottom - Size.Height
        End Select

        'Pass the final bounding rectangle to the system. 
        SHAppBarMessage(ABMsg.ABM_SETPOS, abd)

        'Move and size the appbar so that it conforms to the  bounding rectangle passed to the system. 
        MoveWindow(abd.hWnd, abd.rc.left, abd.rc.top, abd.rc.right - abd.rc.left, abd.rc.bottom - abd.rc.top, True)
    End Sub

#End Region

Maybe this world is another planet’s Hell.
Aldous Huxley

eyes_015.gif___1129027102664
eyes_015.gif___1129027102664
 
I was hoping someone could help me with my code.

Maybe this world is another planet’s Hell.
Aldous Huxley

eyes_015.gif___1129027102664
eyes_015.gif___1129027102664
 
Quickly viewing your code, shouldn't the If ... Then statement in RegisterBar() read:

Code:
If [b]Not[/b] fBarRegistered Then
  ..
Else
  ..
End If

fBarRegistered is defined and given the initial value of False. When the application first calls RegisterBar() on Form Load, the bar should be created when it is not registered. When the Form Close event is calling RegisterBar(), the bar should be removed if fRegisterBar is True.

So I think you have that mixed up, and now each time you call the function on startup, it tries to remove the non-existing appbar.

Regards, Ruffnekk
---
Do not underestimate the power of simplicity!
 
Thanks Rffnekk, when I converted it from C# I missed that. It is now registering a space on the desktop, however, it does not place my app in it. If change formborderstyle=FixedToolWindow it works? If it's set to none (which is how it should be set) it doesn't.

Maybe this world is another planet’s Hell.
Aldous Huxley

eyes_015.gif___1129027102664
eyes_015.gif___1129027102664
 
Thanks for the great post! I used it to create a appbar of my own but I noticed that I was unable to drag it to other edges of the desktop. The space initally created remains, regaurdless were I move the appbar, until I close it. I am a new VB user, and new to appbars, if you have a sample project that would be great!!

Thanks
Dillon01
 
Change the RegisterBar Sub routine to except 2 parms.
Code:
[COLOR=blue]Public Sub[/color] RegisterBar([COLOR=blue]ByVal[/color] frm [COLOR=blue]As[/color] Form, [COLOR=blue]Optional ByVal[/color] dockEdge [COLOR=blue]As[/color] ABEdge = [i]myDefault position[/i])
Once the Drag starts unregister the old position, RegisterBar(Me). After the Drop register the new position, RegisterBar(Me, myNew Position)

Maybe this world is another planet’s Hell.
Aldous Huxley

eyes_015.gif___1129027102664
eyes_015.gif___1129027102664
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top