devxlogo

Creating owner drawn menu items to simulate a color picker

Creating owner drawn menu items to simulate a color picker

' This class does the owner drawing of a MenuItem to draw a color box.' The class inherits from MenuItem, thus the ColorMenuItem objects' can be added to a parent MenuItem or a context menu as you ' normally do, and can also be mixed with other MenuItem items ' under the same parent item.Imports System.DrawingImports System.Windows.FormsPublic Class ColorMenuItem : Inherits MenuItem    Public m_Color As Color    Sub New(ByVal color As Color)        ' create the base menu        MyBase.New("")        ' save the color, and specify that the item will be owner drawn        Me.OwnerDraw = True        m_Color = color    End Sub    Sub New(ByVal color As Color, ByVal eventHandler As EventHandler)        ' create the base menu        MyBase.New("", eventHandler)        ' save the color, and specify that the item will be owner drawn        Me.OwnerDraw = True        m_Color = color    End Sub    Public Property Color() As Color        Get            Return m_Color        End Get        Set(ByVal Value As Color)            m_Color = Value        End Set    End Property    Protected Overrides Sub OnMeasureItem(ByVal e As MeasureItemEventArgs)        MyBase.OnMeasureItem(e)        e.ItemHeight = SystemInformation.MenuHeight - 4        e.ItemWidth = 40    End Sub    Protected Overrides Sub OnDrawItem(ByVal e As DrawItemEventArgs)        MyBase.OnDrawItem(e)        ' draw the background, using the normal/selected color        ' according to the menu item state        Dim brBack As Brush        If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then            brBack = SystemBrushes.Highlight        Else            brBack = SystemBrushes.Menu        End If        e.Graphics.FillRectangle(brBack, e.Bounds)        ' draw the color box        Dim brColorBox As Brush = New SolidBrush(m_Color)        Dim rect As New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, _            e.Bounds.Height)        ' draw the box's black border        rect.Inflate(-5, -3)        e.Graphics.FillRectangle(brColorBox, rect)        e.Graphics.DrawRectangle(New Pen(Color.Black), New Rectangle(rect.X, _            rect.Y, rect.Width, rect.Height))    End SubEnd Class' SAMPLE USAGE: create a color picker' In this example we create a menu bar for the form, with a top level ' menu called "Colors", and add to this item some ColorMenuItem items ' to simulate a color picker menu. We also handle the click on the' color items, and change the form's background color accordingly.' You may change the code to add the color items to a context menu, ' associated to a toolbar button or any other control.Private Sub Form1_Load(ByVal sender As System.Object, _    ByVal e As System.EventArgs) Handles MyBase.Load    ' create a new menu bar and add it to the form    Me.Menu = New MainMenu()    ' create and add the Colors top menu item    Dim mnuColors As MenuItem = New MenuItem("Colors")    Me.Menu.MenuItems.Add(mnuColors)    ' add some color menu items     mnuColors.MenuItems.Add(New ColorMenuItem(Color.Green, _        New EventHandler(AddressOf mnuColor_Click)))    mnuColors.MenuItems.Add(New ColorMenuItem(Color.Navy, _        New EventHandler(AddressOf mnuColor_Click)))    mnuColors.MenuItems.Add(New ColorMenuItem(Color.Purple, _        New EventHandler(AddressOf mnuColor_Click)))    mnuColors.MenuItems.Add(New ColorMenuItem(Color.Red, _        New EventHandler(AddressOf mnuColor_Click)))    mnuColors.MenuItems.Add(New ColorMenuItem(Color.Lime, _        New EventHandler(AddressOf mnuColor_Click)))    mnuColors.MenuItems.Add(New ColorMenuItem(Color.Yellow, _        New EventHandler(AddressOf mnuColor_Click)))    ' create a normal menu item    mnuColors.MenuItems.Add(New MenuItem("Other color...", _        New EventHandler(AddressOf mnuColor_Click)))End SubPrivate Sub mnuColor_Click(ByVal sender As System.Object, _    ByVal e As System.EventArgs)    Dim bgColor As Color    ' if the clicked item is a ColorMenuItem, extract its color    If TypeOf sender Is ColorMenuItem Then        bgColor = CType(sender, ColorMenuItem).Color    Else        ' otherwise open a Windows's Color dialog box        ' ...add your code here...    End If    ' set the form's background color to the selected text    Me.BackColor = bgColorEnd Sub

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist