MBColorPicker Control For Windows Applications

Introduction

What is the use of MBColorPicker? It provides a graphical interface to select a color from a set of various colors. It is simple to use, just drop it onto a form, adjust the design time properties and use it like normal controls.

Background

MBColorPicker is a color picker control that provides various functionalities so the user can select the required color from a set of colors. The language used is VB.NET.

Control properties

Here is the list of properties available in MBColorPicker:

  • PickedColor: This property gets and sets the color picked by MBColorPicker.
  • Arrow: This property sets the arrow on the MBColorPicker button.
  • BaseColor: This property sets the base color of the MBColorPicker button.
  • BaseStrokeColor: This property sets the base stroke color of the MBColorPicker button.
  • OnColor: This property sets the on color of the MBColorPicker button.
  • OnStrokeColor: This property sets the on stroke color of the MBColorPicker button.
  • PressColor: This property sets the press color of the MBColorPicker button.
  • PressStrokeColor: This property sets the press stroke color of the MBColorPicker button.
  • Radius: This property sets the corner radius of the MBColorPicker button.

Code

The concept for this MBColorPicker came from the Microsoft Office 2007 color picker. I organized my paint event into layers like this:

  1. Protected Overrides Sub OnPaint(ByVal pevent As System.Windows.Forms.PaintEventArgs)  
  2.     Dim g As Graphics = pevent.Graphics  
  3.     g.SmoothingMode = SmoothingMode.HighQuality  
  4.     g.InterpolationMode = InterpolationMode.High  
  5.     Dim r As Rectangle = New Rectangle(New Point(-1, -1), New Size(Me.Width + _radius, Me.Height + _radius))  
  6.     Dim path As GraphicsPath = New GraphicsPath  
  7.     Dim rp As Rectangle = New Rectangle(New Point(0, 0), New Size(Me.Width - 1, Me.Height - 1))  
  8.     DrawArc(rp, path)  
  9.     FillGradients(g, path)  
  10.     DrawImage(g)  
  11.     DrawText(g)  
  12.     DrawArrow(g)  
  13. End Sub 
The following are some methods that provide a transparent look to MBColorPicker. This method draws the background for MBColorPicker.
  1. ''' <summary>  
  2. ''' Draw the MBColorPicker Control.  
  3. ''' </summary>  
  4. Protected Overrides Sub OnCreateControl()  
  5.     MyBase.OnCreateControl()  
  6.     A0 = BaseColor.A  
  7.     R0 = BaseColor.R  
  8.     G0 = BaseColor.G  
  9.     B0 = BaseColor.B  
  10.     _colorStroke = _baseStroke  
  11.     Dim r As Rectangle = New Rectangle(New Point(-1, -1), _  
  12.              New Size(Me.Width + _radius, Me.Height + _radius))  
  13.     If Me.Size <> Nothing Then  
  14.         Dim pathregion As GraphicsPath = New GraphicsPath  
  15.         DrawArc(r, pathregion)  
  16.         Me.Region = New Region(pathregion)  
  17.     End If  
  18. End Sub 
This methods draws the color boxes for MBColorPicker:
  1. ''' <summary>  
  2. ''' Draw Color Boxes For MBColorPicker.  
  3. ''' </summary>  
  4. ''' <param name="graphics">Graphics As Graphics</param>  
  5. Private Sub DrawColorBoxes(ByVal graphics As Graphics)  
  6.     DrawColorBelt(graphics, 0, _TopBoxMargin1, True)  
  7.     For i As Int16 = 1 To _ColorMatrixY - 2  
  8.         DrawColorBelt(graphics, i, (i - 1) * _ColorBoxUnit + _TopBoxMargin2, False)  
  9.         Next  
  10.         For i As Int16 = 0 To _ColorMatrixX - 1  
  11.             graphics.DrawRectangle(New Pen(Color.Gray), i * _  
  12.               (_ColorBoxUnit + _ColorBoxMarginX) + _ColorBoxMarginX, _  
  13.               _TopBoxMargin2, _ColorBoxUnit, _ColorBoxUnit * 5)  
  14.         Next  
  15.         DrawColorBelt(graphics, _ColorMatrixY - 1, _TopBoxMargin3, True)  
  16. End Sub  
  17.   
  18. ''' <summary>  
  19. ''' Draw Color Belt For MBColorPicker.  
  20. ''' </summary>  
  21. ''' <param name="graphics">Graphics As Graphics</param>  
  22. ''' <param name="no">No As Int16</param>  
  23. ''' <param name="yOffSet">yOffSet As Int16</param>  
  24. ''' <param name="border">Border As Boolean</param>  
  25. ''' <remarks></remarks>  
  26. Private Sub DrawColorBelt(ByVal graphics As Graphics, ByVal no As Int16, _  
  27.             ByVal yOffSet As Int16, ByVal border As Boolean)  
  28.     For i As Int16 = 0 To _ColorMatrixX - 1  
  29.         DrawColorBox(graphics, _ColorMatrix(i, no), _  
  30.            New Point(i * (_ColorBoxUnit + _ColorBoxMarginX) + _ColorBoxMarginX, yOffSet), border)  
  31.     Next  
  32. End Sub 
This method handles the drop down color for MBColorPicker:
  1. Protected Overrides Sub OnMouseUp(ByVal mevent As System.Windows.Forms.MouseEventArgs)  
  2.     R0 = OnColor.R  
  3.     G0 = OnColor.G  
  4.     B0 = OnColor.B  
  5.     _colorStroke = OnStrokeColor  
  6.     _showbase = MB_ShowBase.Yes  
  7.     i_mode = 1  
  8.     mouse = True  
  9.     If _splitlocation = MB_SplitLocation.Right And xmouse > _  
  10.              Me.Width - _splitdistance And _splitbutton = MB_SplitButton.Yes Then  
  11.         If (_arrow = MB_Arrow.ToDown) Then  
  12.             Dim _MBColorPicker As MBColorPickerHelper = New MBColorPickerHelper(Me)  
  13.             _MBColorPicker.Show(Me.Parent, New Point(Me.Location.X, Me.Height + Me.Location.Y))  
  14.         ElseIf (_arrow = MB_Arrow.ToRight) Then  
  15.             Dim _MBColorPicker As MBColorPickerHelper = New MBColorPickerHelper(Me)  
  16.             _MBColorPicker.Show(Me.Parent, New Point(Me.Location.X + Me.Width, _  
  17.                    (Me.Location.Y + Me.Height / 2) - _MBColorPicker.Height / 2))  
  18.         End If  
  19.     Else  
  20.         MyBase.OnMouseUp(mevent)  
  21.         If (_Keeppressed) Then  
  22.             _Ispressed = True  
  23.             For Each _control As Control In Me.Parent.Controls  
  24.                 If _control.Name <> Me.Name And TypeOf (_control) Is MBColorPickerButton = True Then  
  25.                     CType(_control, MBColorPickerButton)._Ispressed = False  
  26.                     CType(_control, MBColorPickerButton).UpdateMouseLeave()  
  27.                 End If  
  28.             Next  
  29.         End If  
  30.     End If  
  31. End Sub 

Using the code

 
 
It is very easy to use the MBColorPicker in your application. Just add the reference of the provided DLL to your application and just drag and drop.
 
 

History

  • MBColorPicker version 1.0. 

Up Next
    Ebook Download
    View all
    Learn
    View all