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

Creating an XP Style File Menu (Part 1)

Author: DevASP
Download Source Code : 410_WindowsApplication1.zip

In this Article I tell You the way in which we can make XP style File Menu with Image Property.

 

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

 

To Use this Type of menu simply Create a Menu Structure in Form Designer as you would usually create a 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

            Get

                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

            Get

                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

            Get

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

            End Get

        End Property

 

        Public ReadOnly Property Width() As Integer

            Get

                Return w

            End Get

        End Property

 

        Public ReadOnly Property Height() As Integer

            Get

                Return h

            End Get

        End Property

#End Region

Now write that Owner draw region no need Understand it If you are beginner in vb.net 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)

 

            MyBase.OnMeasureItem(e)

 

            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

 

            sf.Dispose()

 

        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.

            sf.Dispose()

            Erase sItemText

 

        End Sub

   

#End Region

 

 

Source code is given with this article. If you have any proplem 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 Extract Information of Your Hard Disk using Win32 API in VB.Net (Part 4)

Disclaimer - Privacy
© 2002-2017 DevASP.net