Enhancing Controls Through Owner Draw


There are a lot more drawing features included in .NET, but what we've seen here should be enough to whet your appetite. You can do a lot of fancy drawing with GDI+, but let's face it: You and I are programmers, not artists. If we were artists, we'd be raking in six figures using a floor mop to draw traditional abstract cubist Italian landscapes with Bauhausian accents.

Fortunately, there are practical semi-artistic things you can do with GDI+. One important drawing feature is owner draw, a sharing of drawing responsibilities between a control and you, the programmer. (You are the "owner.") The ComboBox control supports owner drawing of the individual items in the drop-down portion of the list. Let's create a ComboBox control that displays color names, including a small sample of the color to the left of the name. Create a new Windows Forms application, and add a ComboBox control named ComboBox1 to Form1. Make these changes to ComboBox1.

  • Change its DropDownStyle property to DropDownList.

  • Change its DrawMode property to OwnerDrawFixed.

  • Alter its Items property, adding multiple color names as distinct text lines in the String Collection Editor window. I added Red, Green, and Blue.

Now, add the following code to the source code area of Form1's class.

Private Sub ComboBox1_DrawItem(ByVal sender As Object, _       ByVal e As System.Windows.Forms.DrawItemEventArgs) _       Handles ComboBox1.DrawItem    ' ----- Ignore the unselelected state.    If (e.Index = -1) Then Return    ' ----- Create a brush for the display color, based on    '   the name of the item.    Dim colorBrush As New SolidBrush(Color.FromName( _       CStr(ComboBox1.Items(e.Index))))    ' ----- Create a text brush. The color varies based on    '       whether this item is selected or not.    Dim textBrush As Brush    If ((e.State And DrawItemState.Selected) = _          DrawItemState.Selected) Or _          ((e.State And DrawItemState.HotLight) = _          DrawItemState.HotLight) Then       textBrush = New SolidBrush(SystemColors.HighlightText)    Else       textBrush = New SolidBrush(SystemColors.ControlText)    End If    ' ----- Get the shape of the color display area.    Dim colorBox As New Rectangle(e.Bounds.Left + 4, _       e.Bounds.Top + 2, (e.Bounds.Height - 4) * 2, _       e.Bounds.Height - 4)    ' ----- Draw the selected or unselected background.    e.DrawBackground()    ' ----- Draw the custom color area.    e.Graphics.FillRectangle(colorBrush, colorBox)    e.Graphics.DrawRectangle(Pens.Black, colorBox)    ' ----- Draw the name of the color to the right of    '       the color.    e.Graphics.DrawString(CStr(ComboBox1.Items(e.Index)), _       ComboBox1.Font, textBrush, 8 + colorBox.Width, _       e.Bounds.Top + ((e.Bounds.Height - _       ComboBox1.Font.Height) / 2))    ' ----- Draw a selected rectangle around the item,    '       if needed.    e.DrawFocusRectangle()    ' ----- Clean up.    textBrush.Dispose()    colorBrush.Dispose() End Sub 


Run the code and play with the combo box, as shown in Figure 17-14.

Figure 17-14. Our custom color combo box





Start-to-Finish Visual Basic 2005. Learn Visual Basic 2005 as You Design and Develop a Complete Application
Start-to-Finish Visual Basic 2005: Learn Visual Basic 2005 as You Design and Develop a Complete Application
ISBN: 0321398009
EAN: 2147483647
Year: 2006
Pages: 247
Authors: Tim Patrick

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net