Author Topic: Color Combobox (VB.NET)  (Read 4863 times)

0 Members and 1 Guest are viewing this topic.

mcarson

  • Guest
Color Combobox (VB.NET)
« on: July 02, 2008, 03:11:01 PM »
Having searched the internet for an example of creating a custom-drawn combo box that looks similar to the AutoCAD Select Color combo, I came to the conclusion that I'd have to create my own... :cry:
I have found that it wasn't as difficult as I thought...
This is my first revision; not using anything AutoCAD, however I am working on 'cloning' most of the functionality that the AutoCAD one provides...
I would be grateful for any comments

Code: [Select]
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Imaging

Public Class ColorCombo
Inherits ComboBox

Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
Me.imglColor = New System.Windows.Forms.ImageList(Me.components)
Me.SuspendLayout()

Me.imglColor.ColorDepth = System.Windows.Forms.ColorDepth.Depth32Bit
Me.imglColor.ImageSize = New System.Drawing.Size(24, 12)
Me.imglColor.TransparentColor = System.Drawing.Color.Transparent

Me.ResumeLayout(False)

End Sub

Private components As System.ComponentModel.IContainer
Friend WithEvents imglColor As New System.Windows.Forms.ImageList

Public Sub New()
Me.Items.Add("Red")
Me.Items.Add("Yellow")
Me.Items.Add("Green")
Me.Items.Add("Cyan")
Me.Items.Add("Blue")
Me.Items.Add("Magenta")
Me.Items.Add("White")
Me.Items.Add("Dark Gray")
Me.Items.Add("Light Gray")
'Me.Items.Add("Select Color... ")
FillImageList()
End Sub

Private Sub FillImageList()
'Red Image
Dim imgRed As Image = New Bitmap(24, 12, PixelFormat.Format32bppArgb)
Dim red As Graphics = Graphics.FromImage(imgRed)
red.FillRectangle(New SolidBrush(Color.Red), New Rectangle(0, 0, 24, 12))
imglColor.Images.Add("Red", imgRed)

'Yellow Image
Dim imgYellow As Image = New Bitmap(24, 12, PixelFormat.Format32bppArgb)
Dim yellow As Graphics = Graphics.FromImage(imgYellow)
yellow.FillRectangle(New SolidBrush(Color.Yellow), New Rectangle(0, 0, 24, 12))
imglColor.Images.Add("Yellow", imgYellow)

'Green Image
Dim imgGreen As Image = New Bitmap(24, 12, PixelFormat.Format32bppArgb)
Dim Green As Graphics = Graphics.FromImage(imgGreen)
Green.FillRectangle(New SolidBrush(Color.Green), New Rectangle(0, 0, 24, 12))
imglColor.Images.Add("Green", imgGreen)

'Cyan Image
Dim imgCyan As Image = New Bitmap(24, 12, PixelFormat.Format32bppArgb)
Dim Cyan As Graphics = Graphics.FromImage(imgCyan)
Cyan.FillRectangle(New SolidBrush(Color.Cyan), New Rectangle(0, 0, 24, 12))
imglColor.Images.Add("Cyan", imgCyan)

'Blue Image
Dim imgBlue As Image = New Bitmap(24, 12, PixelFormat.Format32bppArgb)
Dim Blue As Graphics = Graphics.FromImage(imgBlue)
Blue.FillRectangle(New SolidBrush(Color.Blue), New Rectangle(0, 0, 24, 12))
imglColor.Images.Add("Blue", imgBlue)

'Magenta Image
Dim imgMagenta As Image = New Bitmap(24, 12, PixelFormat.Format32bppArgb)
Dim Magenta As Graphics = Graphics.FromImage(imgMagenta)
Magenta.FillRectangle(New SolidBrush(Color.Magenta), New Rectangle(0, 0, 24, 12))
imglColor.Images.Add("Magenta", imgMagenta)

'White Image
Dim imgWhite As Image = New Bitmap(24, 12, PixelFormat.Format32bppArgb)
Dim White As Graphics = Graphics.FromImage(imgWhite)
White.FillRectangle(New SolidBrush(Color.White), New Rectangle(0, 0, 24, 12))
'White.DrawRectangle(New Pen(Color.Black, 1), 0, 1, 23, 11)
imglColor.Images.Add("White", imgWhite)

'DarkGray Image
Dim imgDarkGray As Image = New Bitmap(24, 12, PixelFormat.Format32bppArgb)
Dim DarkGray As Graphics = Graphics.FromImage(imgDarkGray)
DarkGray.FillRectangle(New SolidBrush(Color.DarkGray), New Rectangle(0, 0, 24, 12))
imglColor.Images.Add("Dark Gray", imgDarkGray)

'LightGray Image
Dim imgLightGray As Image = New Bitmap(24, 12, PixelFormat.Format32bppArgb)
Dim LightGray As Graphics = Graphics.FromImage(imgLightGray)
LightGray.FillRectangle(New SolidBrush(Color.LightGray), New Rectangle(0, 0, 24, 12))
imglColor.Images.Add("Light Gray", imgLightGray)

'Select Color Image
'Dim imgSelect As Image = New Bitmap(24, 12, PixelFormat.Format32bppPArgb)
'Dim _Select As Graphics = Graphics.FromImage(imgSelect)

'_Select.DrawLine(New Pen(Color.Black, 1), New PointF(6, 9), New PointF(6, 10))
'_Select.DrawLine(New Pen(Color.Black, 1), New PointF(7, 9), New PointF(7, 10))

'_Select.DrawLine(New Pen(Color.Black, 1), New PointF(12, 9), New PointF(12, 10))
'_Select.DrawLine(New Pen(Color.Black, 1), New PointF(13, 9), New PointF(13, 10))

'_Select.DrawLine(New Pen(Color.Black, 1), New PointF(18, 9), New PointF(18, 10))
'_Select.DrawLine(New Pen(Color.Black, 1), New PointF(19, 9), New PointF(19, 10))

'imglColor.Images.Add("Select Color... ", imgSelect)

End Sub


Private Sub MeasureComboItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles Me.MeasureItem
If e.Index < 0 Then Return

Dim s As String = sender.Items(e.Index)
Dim img As Image = imglColor.Images(e.Index)
Dim hgt = Math.Max(img.Height, e.Graphics.MeasureString(s, Me.Font).Height) * 1.1

If Me.DropDownStyle = ComboBoxStyle.DropDownList Then
Me.ItemHeight = hgt
Else
Me.ItemHeight = e.Graphics.MeasureString(s, Me.Font).Height * 1.1
End If

e.ItemWidth = Me.Width

End Sub

Private Sub DrawComboItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Me.DrawItem

If e.Index = -1 Then Return

Dim g As Graphics = e.Graphics
Me.DoubleBuffered = True
g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality

'fill the background of the item
g.FillRectangle(New SolidBrush(Color.White), e.Bounds)
'draw the image from the image list control, offset it by 5 pixels and makes sure it's centered vertically
Dim myImage As Image = imglColor.Images.Item(sender.Items(e.Index))
g.DrawImage(myImage, 5, e.Bounds.Top + (e.Bounds.Height - myImage.Height) \ 2)
'draw the text, position it so that it starts 5 pixels to the right of the image
g.DrawString(sender.Items(e.Index), Me.Font, Brushes.Black, e.Bounds.Left + myImage.Width + 5, e.Bounds.Top)

End Sub

End Class