.NET OwnerDraw ComboBox: Vista/7 themed DropDownList
Posted
by Stijn
on Stack Overflow
See other posts from Stack Overflow
or by Stijn
Published on 2010-05-23T13:55:28Z
Indexed on
2010/05/23
14:00 UTC
Read the original article
Hit count: 527
I'm working on a custom ComboBox for a project, and have the basic painting working. One of the remaining things to do is make it look like the default ComboBox in Vista and 7, when the DropDownStyle is set to DropDownList (looks like a button instead of a regular ComboBox).
I read much about VisualStyleRenderer, read ComboBox.cs and ComboBoxRenderer.cs, and tried to get it working. It works in the sense of, it does something. It just doesn't do what it's supposed to do.
I also found an article about Vista themed Owner-Drawn Buttons, but it is in C++ (which would be a challenge on its own for me) and it seems like an awful lot of code.
If anyone could supply either VB or C# code, that would be great. Below is my current code.
Public Sub New()
DrawMode = Windows.Forms.DrawMode.OwnerDrawFixed
ItemHeight *= 2
DropDownStyle = ComboBoxStyle.DropDownList
End Sub
Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)
If e.Index = -1 Then
Return
End If
If Not (e.State = DrawItemState.Selected) Then
e.DrawBackground()
End If
Dim brsh As Brush
If (e.State And DrawItemState.Focus) = DrawItemState.Focus And (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
brsh = SystemBrushes.HighlightText
Else
brsh = SystemBrushes.ControlText
End If
e.DrawFocusRectangle()
e.Graphics.DrawString(Items(e.Index), e.Font, brsh, e.Bounds)
End Sub
© Stack Overflow or respective owner