Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

How to make XP Style Context menu with Image Property

Author: DevASP
Download Source Code :

In this Article I will tell You the way in which we can make XP Style Context Menu with Image Property. which makes your application user friendly.

VB.Net has its own  Context menu but its Context menu is simple most. In professional application usually Clients wants images with the file menu.  This looks nice and makes file Context menu user friendly.


To Use this Type of Context menu simply Create a Context menu Structure in Form Designer as you would usually create a Context menu. Once you are happy with the layout of your Menu just follow the Simple Steps as Outlined Below.


1  Save All.

2  Expand [#Region " Windows Form Designer generated code "].

3  Press Ctrl+A.

4  Press Ctrl+H.

5  Type [System.Windows.Forms] in the [Find what:] Combobox.

6  Type [MDAUDI] in the [Replace with:] Combobox.

7  Click Replace All, then Close.

8  Save All.

9  Rebuild Solution.


You will now have two new properties for each Menu Item, which can be accessed through the Properties Window. These new properties have

been assigned to the Appearance Category. If you add any New MenuItems you will need to re-run the above steps,

unless you want to draw them in a different style (such as the sidebar in the attached project).


Write this region in your control to initialize the basic things.


#Region " Initialisation "


        Private m_Image As Image

        Private m_font As Font

        Private w As Integer

        Private h As Integer


        Public Sub New()

            MyClass.New("", Nothing, Nothing, System.Windows.Forms.Shortcut.None)

        End Sub


        Public Sub New(ByVal text As String, ByVal image As Image, ByVal onClick As EventHandler, ByVal shortcut As Shortcut)


            MyBase.New(text, onClick, shortcut)

            OwnerDraw = True

            m_Image = image

            m_font = SystemInformation.MenuFont

        End Sub


#End Region

Without the Default Value attribute here we would not be able to reset the image to None once we have set an image. So for this property writes this region it removes this property.

#Region " Custom Properties "



        DefaultValue(GetType(Image), "None"), _

  Description("The Image to be displayed in MenuItems Sidebar.")> _

        Public Property Image() As Image


                Image = m_Image

            End Get

            Set(ByVal New_Image As Image)

                m_Image = New_Image

            End Set

        End Property



        Description("The Font that displays in this MenuItem.")> _

        Public Property Font() As Font


                Font = m_font

            End Get

            Set(ByVal New_Font As Font)

                m_font = New_Font

            End Set

        End Property


        Private ReadOnly Property WindowMenu() As Boolean


                Return CBool(TypeOf (Me.Parent) Is MainMenu)

            End Get

        End Property


        Public ReadOnly Property Width() As Integer


                Return w

            End Get

        End Property


        Public ReadOnly Property Height() As Integer


                Return h

            End Get

        End Property

#End Region


Now write that Owner draw region no need Understand it If you are beginner in And have not Knowledge of owner draw Properties . For beginner just remember that whenever We want to make some thing different From the Environment in which we are working we have to use Owner draw Properties.The routines below also Doing the same thing.


#Region " Owner Draw Subs "


        Protected Overrides Sub OnMeasureItem(ByVal e As System.Windows.Forms.MeasureItemEventArgs)




            Dim ItemText As String

            Dim sf As New StringFormat

            sf.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.Hide


            'replace Chr(2) with two spaces to get overall text length.

            ItemText = JoinItemText().Replace(Chr(2), "  ")


            Select Case ItemText

                Case "-" 'Seperator

                    e.ItemHeight = 3

                Case Else

                    Dim szF As SizeF

                    e.ItemHeight = CInt(e.Graphics.MeasureString(ItemText, m_font, szF, sf).Height)

                    If e.ItemHeight < 22 Then e.ItemHeight = 22

                    e.ItemWidth = CInt(e.Graphics.MeasureString(ItemText, m_font, szF, sf).Width) + 2

                    'If not TopLevel MenuItem then

                    If Not WindowMenu Then

                        e.ItemWidth += 44

                    End If

            End Select


            h = e.ItemHeight

            w = e.ItemWidth




        End Sub


Private Sub ClearBackGround(ByVal e As System. Windows. Forms. DrawItemEventArgs)


Dim BackBrush As Brush = SystemBrushes.Menu

Dim BackRect As New Rectangle(e.Bounds.X + 24, e.Bounds.Y, e.Bounds. Width - 24, e.Bounds.Height)


     If WindowMenu Then

           BackBrush = SystemBrushes.Control

           BackRect.X -= 24

           BackRect.Width += 24

      End If


      e.Graphics.FillRectangle(BackBrush, BackRect)

End Sub


Private Sub DrawSideBar(ByVal e As System.Windows. Forms. DrawItemEventArgs)


      Dim SideBrush As Brush = SystemBrushes.Control

e.Graphics.FillRectangle(SideBrush, New Rectangle(e.Bounds.X, e.Bounds.Y, 24, e.Bounds.Height))


 End Sub



Private Sub DrawText(ByVal e As System.Windows.Forms.DrawItemEventArgs)


Dim TextRectF As New RectangleF(e.Bounds.X + 32, e.Bounds.Top, e.Bounds.Width - 48, e.Bounds.Height)

   Dim TextBrush As Brush = SystemBrushes.ControlText

   Dim sItemText() As String = Split(JoinItemText(), Chr(2))

   Dim sf As New System.Drawing.StringFormat(StringFormatFlags.NoWrap)


            'Vertical Centre Align for Text.

            sf.LineAlignment = StringAlignment.Center

            'Show Hot-Key UnderScore in Text.

            sf.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.Show


   if WindowMenu Then

TextRectF = New RectangleF(e.Bounds.X + 8, e.Bounds.Y, e.Bounds. Width - 8, e.Bounds.Height)

   End If


   If CBool(e.State And DrawItemState.Selected) _

        Or CBool(e.State And DrawItemState.HotLight) Then

        TextBrush = SystemBrushes.WindowText

   End If


            If CBool(e.State And DrawItemState.Disabled) Then

                TextBrush = SystemBrushes.FromSystemColor(SystemColors.GrayText)

            End If


            e.Graphics.DrawString(sItemText(0), m_font, TextBrush, TextRectF, sf)

            'If ControlKey present then...

            If UBound(sItemText) = 1 Then

                'Right Align for Control Key Text.

                sf.Alignment = StringAlignment.Far

                e.Graphics.DrawString(sItemText(1), m_font, TextBrush, TextRectF, sf)

            End If


            'Finished with StringFormat and sItemText.


            Erase sItemText


        End Sub


#End Region



Source code is given with this article. if you have any problem in doing it just add the User control as Add existing item and Build your project now no need to write any thing control form.

Add Article Comment:
Name :
Email Address :
Comments :
<< How to make Custom Tool Bar using VB.Net

Disclaimer - Privacy
© 2002-2018