Voici comment insérer les couleurs dans un combobox.
Vous pouvez changer de couleur du combobox dans bordercolor et buttoncolor.voici ma classe :
Imports System.Reflection
Imports System.Runtime.InteropServices
Imports System.ComponentModel
Public Class ComboboxColor : Inherits ComboBox
Public Sub New()
Me.Font = New Font("Times New Roman", 10, FontStyle.Regular)
Me.Size = New Size(180, 23)
Me.DropDownStyle = ComboBoxStyle.DropDownList
Me.DrawMode = DrawMode.OwnerDrawFixed
Me.MaxDropDownItems = 10
Me.IntegralHeight = False
ComboboxModeColor(Me)
AddHandler Me.DrawItem, AddressOf _DrawItem
End Sub
Private Sub _DrawItem(sender As Object, e As DrawItemEventArgs)
e.DrawBackground()
If e.Index >= 0 Then
Dim colBrush As Brush = New SolidBrush(Color.FromName(CType(Me.Items(e.Index), String)))
Dim r1 = New Rectangle(e.Bounds.Left + 1, e.Bounds.Top + 1, 2 * (e.Bounds.Height - 2), e.Bounds.Height - 2)
Dim r2 = Rectangle.FromLTRB(r1.Right + 2, e.Bounds.Top, e.Bounds.Right, e.Bounds.Bottom)
e.Graphics.FillRectangle(colBrush, r1)
e.Graphics.DrawRectangle(Pens.Black, r1)
TextRenderer.DrawText(e.Graphics, Me.GetItemText(Me.Items(e.Index)), Me.Font, r2, Me.ForeColor, TextFormatFlags.Left Or TextFormatFlags.VerticalCenter)
End If
e.DrawFocusRectangle()
End Sub
Private Sub ComboboxModeColor(cmb As ComboBox)
Me.Items.Clear()
For Each bcolor As KnownColor In [Enum].GetValues(GetType(KnownColor))
Dim loadcolor As Color = Color.FromKnownColor(bcolor)
If loadcolor.IsSystemColor = False Then
If loadcolor.Name <> "Transparent" Then
cmb.Items.Add(loadcolor.Name)
End If
End If
Next
If cmb.Items.Count > 0 Then cmb.SelectedIndex = 0
End Sub
Private borderColorField As Color = Color.Gray
<DefaultValue(GetType(Color), "Gray")>
Public Property BorderColor As Color
Get
Return borderColorField
End Get
Set(ByVal value As Color)
If borderColorField <> value Then
borderColorField = value
Invalidate()
End If
End Set
End Property
Private buttonColorField As Color = Color.LightGray
<DefaultValue(GetType(Color), "LightGray")>
Public Property ButtonColor As Color
Get
Return buttonColorField
End Get
Set(ByVal value As Color)
If buttonColorField <> value Then
buttonColorField = value
Invalidate()
End If
End Set
End Property
Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = WM_PAINT AndAlso DropDownStyle = ComboBoxStyle.DropDownList Then
Dim clientRect = ClientRectangle
Dim dropDownButtonWidth = SystemInformation.HorizontalScrollBarArrowWidth
Dim outerBorder = New Rectangle(clientRect.Location, New Size(clientRect.Width - 1, clientRect.Height - 1))
Dim innerBorder = New Rectangle(outerBorder.X + 1, outerBorder.Y + 1, outerBorder.Width - dropDownButtonWidth - 2, outerBorder.Height - 2)
Dim innerInnerBorder = New Rectangle(innerBorder.X + 1, innerBorder.Y + 1, innerBorder.Width - 2, innerBorder.Height - 2)
Dim dropDownRect = New Rectangle(innerBorder.Right + 1, innerBorder.Y, dropDownButtonWidth, innerBorder.Height + 1)
If RightToLeft = RightToLeft.Yes Then
innerBorder.X = clientRect.Width - innerBorder.Right
innerInnerBorder.X = clientRect.Width - innerInnerBorder.Right
dropDownRect.X = clientRect.Width - dropDownRect.Right
dropDownRect.Width += 1
End If
Dim innerBorderColor = If(Enabled, BackColor, SystemColors.Control)
Dim outerBorderColor = If(Enabled, BorderColor, SystemColors.ControlDark)
Dim buttonColor = If(Enabled, Me.ButtonColor, SystemColors.Control)
Dim middle = New Point(dropDownRect.Left + dropDownRect.Width / 2, dropDownRect.Top + dropDownRect.Height / 2)
Dim arrow = New Point() {New Point(middle.X - 3, middle.Y - 2), New Point(middle.X + 4, middle.Y - 2), New Point(middle.X, middle.Y + 2)}
Dim ps = New PAINTSTRUCT()
Dim shoulEndPaint As Boolean = False
Dim dc As IntPtr
If m.WParam = IntPtr.Zero Then
dc = BeginPaint(Handle, ps)
m.WParam = dc
shoulEndPaint = True
Else
dc = m.WParam
End If
Dim rgn = CreateRectRgn(innerInnerBorder.Left, innerInnerBorder.Top, innerInnerBorder.Right, innerInnerBorder.Bottom)
SelectClipRgn(dc, rgn)
DefWndProc(m)
DeleteObject(rgn)
rgn = CreateRectRgn(clientRect.Left, clientRect.Top, clientRect.Right, clientRect.Bottom)
SelectClipRgn(dc, rgn)
Using g = Graphics.FromHdc(dc)
Using b = New SolidBrush(buttonColor)
g.FillRectangle(b, dropDownRect)
End Using
Using b = New SolidBrush(outerBorderColor)
g.FillPolygon(b, arrow)
End Using
Using p = New Pen(innerBorderColor)
g.DrawRectangle(p, innerBorder)
g.DrawRectangle(p, innerInnerBorder)
End Using
Using p = New Pen(outerBorderColor)
g.DrawRectangle(p, outerBorder)
End Using
End Using
If shoulEndPaint Then EndPaint(Handle, ps)
DeleteObject(rgn)
Else
MyBase.WndProc(m)
End If
End Sub
Private Const WM_PAINT As Integer = &HF
<StructLayout(LayoutKind.Sequential)>
Public Structure RECT
Public L, T, R, B As Integer
End Structure
<StructLayout(LayoutKind.Sequential)>
Public Structure PAINTSTRUCT
Public hdc As IntPtr
Public fErase As Boolean
Public rcPaint_left As Integer
Public rcPaint_top As Integer
Public rcPaint_right As Integer
Public rcPaint_bottom As Integer
Public fRestore As Boolean
Public fIncUpdate As Boolean
Public reserved1 As Integer
Public reserved2 As Integer
Public reserved3 As Integer
Public reserved4 As Integer
Public reserved5 As Integer
Public reserved6 As Integer
Public reserved7 As Integer
Public reserved8 As Integer
End Structure
<DllImport("user32.dll")>
Private Shared Function BeginPaint(ByVal hWnd As IntPtr,
<[In](), Out()> ByRef lpPaint As PAINTSTRUCT) As IntPtr
End Function
<DllImport("user32.dll")>
Private Shared Function EndPaint(ByVal hWnd As IntPtr, ByRef lpPaint As PAINTSTRUCT) As Boolean
End Function
<DllImport("gdi32.dll")>
Public Shared Function SelectClipRgn(ByVal hDC As IntPtr, ByVal hRgn As IntPtr) As Integer
End Function
<DllImport("user32.dll")>
Public Shared Function GetUpdateRgn(ByVal hwnd As IntPtr, ByVal hrgn As IntPtr, ByVal fErase As Boolean) As Integer
End Function
Public Enum RegionFlags
[ERROR] = 0
NULLREGION = 1
SIMPLEREGION = 2
COMPLEXREGION = 3
End Enum
<DllImport("gdi32.dll")>
Friend Shared Function DeleteObject(ByVal hObject As IntPtr) As Boolean
End Function
<DllImport("gdi32.dll")>
Private Shared Function CreateRectRgn(ByVal x1 As Integer, ByVal y1 As Integer, ByVal x2 As Integer, ByVal y2 As Integer) As IntPtr
End Function
End Class
Aucun commentaire:
Enregistrer un commentaire