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

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes