Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Creating owner drawn menu items to simulate a color picker

Total Hit ( 3626)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


Click here to copy the following block
' 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.Drawing
Imports System.Windows.Forms

Public 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 Sub

End 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 Sub

Private 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 = bgColor
End Sub


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.