Flat style Combobox

Zveřejněno 24 srpna 05 02:49 odp.

Standartní Combobox v .NET nemá vlastnost Flat, tak jsem si udělal vlastní Combobox, který se tváří stylem Flat :-) . Třída také přetěžuje funkci OnDrawItem a vykresluje pozadí položkek sama s použitím LinearGradientBrush.

 

 

Imports System.ComponentModel

Imports System.Windows.Forms

Imports System.Drawing

<Description("ComboBox"), _

DefaultProperty("Props"), _

System.Drawing.ToolboxBitmap(GetType(System.Windows.Forms.ComboBox))> _

Public Class eComboBox

Inherits System.Windows.Forms.ComboBox

Private Const WM_PAINT As Integer = &HF

Private Const WM_NCPAINT As Integer = &H85

Private m_bolMouseDown As Boolean = False

Private m_RectButton As Rectangle

'private variables

Private _props As eComboBoxProps

'Constructor

Public Sub New()

_props = New eComboBoxProps(Me)

Me.SetStyle(Windows.Forms.ControlStyles.DoubleBuffer, True)

DrawMode = DrawMode.OwnerDrawFixed

End Sub

'Properties

<DesignerSerializationVisibility(DesignerSerializationVisibility.Content), _

TypeConverter(GetType(ExpandableObjectConverter))> _

Public ReadOnly Property Props() As eComboBoxProps

Get

Return _props

End Get

End Property

Public Class eComboBoxProps

'Private variables

Private ctrl As eComboBox

'Public variables

Public Enum enClearAction

SelectFirstItem

ClearComboBox

End Enum

'Constructor

Public Sub New(ByVal ctrl_ As eComboBox)

ctrl = ctrl_

End Sub

 

'Properties

Private m_DrawItem_SelectedItemStartColor As System.Drawing.Color = Color.DarkBlue

Private m_DrawItem_SelectedItemEndColor As System.Drawing.Color = Color.Blue

Private m_DrawItem_ItemStartColor As System.Drawing.Color = Color.LightBlue

Private m_DrawItem_ItemEndColor As System.Drawing.Color = Color.WhiteSmoke

 

<DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), _

DefaultValue(GetType(System.Drawing.Color), "LightBlue")> _

Public Property DrawItem_ItemStartColor() As System.Drawing.Color

Get

Return m_DrawItem_ItemStartColor

End Get

Set(ByVal Value As System.Drawing.Color)

m_DrawItem_ItemStartColor = Value

End Set

End Property

<DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), _

DefaultValue(GetType(System.Drawing.Color), "WhiteSmoke")> _

Public Property DrawItem_ItemEndColor() As System.Drawing.Color

Get

Return m_DrawItem_ItemEndColor

End Get

Set(ByVal Value As System.Drawing.Color)

m_DrawItem_ItemEndColor = Value

End Set

End Property

<DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), _

DefaultValue(GetType(System.Drawing.Color), "DarkBlue")> _

Public Property DrawItem_SelectedItemStartColor() As System.Drawing.Color

Get

Return m_DrawItem_SelectedItemStartColor

End Get

Set(ByVal Value As System.Drawing.Color)

m_DrawItem_SelectedItemStartColor = Value

End Set

End Property

<DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), _

DefaultValue(GetType(System.Drawing.Color), "Blue")> _

Public Property DrawItem_SelectedItemEndColor() As System.Drawing.Color

Get

Return m_DrawItem_SelectedItemEndColor

End Get

Set(ByVal Value As System.Drawing.Color)

m_DrawItem_SelectedItemEndColor = Value

End Set

End Property

End Class

Protected Overrides Sub OnMouseUp(ByVal e As MouseEventArgs)

MyBase.OnMouseUp(e)

m_bolMouseDown = False

Invalidate()

End Sub

Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)

MyBase.OnMouseDown(e)

m_RectButton = New Rectangle(Width - 18, 0, 17, Height)

If e.X >= m_RectButton.Left Then

m_bolMouseDown = True

Invalidate()

End If

End Sub

Protected Overrides Sub OnResize(ByVal e As EventArgs)

MyBase.OnResize(e)

Invalidate()

End Sub

Protected Overrides Sub WndProc(ByRef m As Message)

Dim g As Graphics = Graphics.FromHwnd(Handle)

Select Case m.Msg

Case WM_NCPAINT

g.Clear(BackColor)

Me.Invalidate()

Case WM_PAINT

MyBase.WndProc(m)

g.DrawRectangle(New Pen(BackColor, 2), _

New Rectangle(2, 2, Width - 4, Height - 4))

If Me.Focused = True Then

Dim rect As New Rectangle(0, 0, Me.Width - 18, Me.Height)

ControlPaint.DrawBorder(g, rect, Color.Gray, ButtonBorderStyle.Inset)

End If

m_RectButton = New Rectangle(Width - 18, 0, 17, Height)

If m_bolMouseDown Then

ControlPaint.DrawComboButton(g, m_RectButton, ButtonState.Pushed)

Else

ControlPaint.DrawComboButton(g, m_RectButton, ButtonState.Flat)

End If

End Select

MyBase.WndProc(m)

g.Dispose()

End Sub

Protected Overrides Sub OnDrawItem(ByVal e As DrawItemEventArgs)

If e.Index = -1 Then Return

Dim item As ListItem = Me.Items(e.Index)

Dim strText As String

If item Is Nothing Then

strText = ""

Else

strText = item.TEXT 'CStr(Items.Item(e.Index))

End If

Dim brBrush As Brush

 

If CBool(e.State And DrawItemState.Selected) Then

brBrush = New System.Drawing.Drawing2D.LinearGradientBrush(e.Bounds, Props.DrawItem_SelectedItemStartColor, Props.DrawItem_SelectedItemEndColor, 0)

Else

If Me.DroppedDown = False Then

brBrush = New System.Drawing.Drawing2D.LinearGradientBrush(e.Bounds, Color.White, Color.White, 0)

Else

If e.Index Mod 2 = 0 Then

brBrush = New System.Drawing.Drawing2D.LinearGradientBrush(e.Bounds, Props.DrawItem_ItemStartColor, Props.DrawItem_ItemEndColor, 0)

Else

brBrush = New System.Drawing.Drawing2D.LinearGradientBrush(e.Bounds, Color.White, Color.WhiteSmoke, 0)

End If

End If

End If

e.Graphics.FillRectangle(brBrush, e.Bounds)

brBrush = New SolidBrush(e.ForeColor)

e.Graphics.DrawString(strText, e.Font, brBrush, e.Bounds.Left, e.Bounds.Top)

End Sub

Private Sub eComboBox_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Leave

Me.Invalidate()

End Sub

Private Sub eComboBox_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.GotFocus

Me.Invalidate()

End Sub

End Class

by opis

Komentář

Žádné komentáře
Neregistrovaní uživatele nemužou přidávat komentáře.
Vyvojar.cz na prodej!