SCÉNARIO
J’utilise un thème visuel Windows tiers.
Quand je vois ma candidature, cela ressemble à ceci:
Mais lorsque j’utilise le thème Aero normal, il affiche partout des frontières blanches horribles:
QUESTION
Je sais que le jeu de couleurs utilisé dans l’application dépend du style visuel, mais:
Je peux hériter des TextBox
, ComboBox
et TabControl
pour changer la couleur de la bordure dessinée en utilisant une couleur plus sombre? Comment?
METTRE À JOUR
Mes boîtes de texte utilisent une valeur de propriété Fixed3D
de Fixed3D
Mes ComboBox utilisent une propriété FlatStyle
avec la valeur Flat
et sont définies en tant que DropDownList
Vous pouvez intercepter le WM_PAINT
ou WM_ERASEBKGND
et dessiner la bordure manuellement:
[DllImport("user32.dll")] static extern IntPtr GetWindowDC(IntPtr hWnd); [DllImport("user32.dll")] static extern int ReleaseDC(IntPtr hwnd, IntPtr hDC); protected override void WndProc(ref Message m) { IntPtr hdc; if (m.Msg == 0x14) //WM_ERASEBKGND { hdc = GetWindowDC(m.HWnd); if (hdc != IntPtr.Zero) { using (Graphics g = Graphics.FromHdc(hdc)) { g.DrawRectangle(Pens.Red, 0, 0, this.Width-1, this.Height-1); } ReleaseDC(m.HWnd, hdc); } base.WndProc(ref m); }
Il y a cependant un problème lorsque la zone de texte perd son focus.
Solution VB.Net pour ComboBox avec bordure (non complète, elle doit être améliorée avec un 3dBorder)
#Region " Option Statements " Option Explicit On Option Ssortingct On Option Infer Off #End Region #Region " Imports " Imports System.ComponentModel #End Region ''' ''' A custom user control. ''' Public Class ElektroComboBox : Inherits ComboBox #Region " Properties " ''' ''' Gets or sets the border color. ''' ''' The border color. Public Property BorderColor() As Color Get Return borderColor1 End Get Set(ByVal value As Color) Me.borderColor1 = value Me.Invalidate() End Set End Property ''' ''' The border color. ''' Private borderColor1 As Color = SystemColors.ControlDark ''' ''' Gets or sets the border style. ''' ''' The border color. Public Property BorderStyle As ButtonBorderStyle Get Return borderStyle1 End Get Set(ByVal value As ButtonBorderStyle) Me.borderStyle1 = value Me.Invalidate() End Set End Property ''' ''' The border style. ''' Private borderStyle1 As ButtonBorderStyle = ButtonBorderStyle.Solid #End Region #Region " Enumerations " ''' ''' Specifies a Windows Message. ''' Private Enum WindowsMessages WM_PAINT = &HF End Enum #End Region #Region " Windows Procedure " ''' ''' Processes Windows messages. ''' ''' The Windows to process. Protected Overrides Sub WndProc(ByRef m As Message) MyBase.WndProc(m) Select Case m.Msg Case WindowsMessages.WM_PAINT Me.DrawBorder() End Select End Sub #End Region #Region " Private Methods " ''' ''' Draws a border on the control surface. ''' Private Sub DrawBorder() Using g As Graphics = Graphics.FromHwnd(Me.Handle) ControlPaint.DrawBorder(Graphics.FromHwnd(Me.Handle), Me.ClientRectangle, Me.borderColor1, Me.borderStyle1) End Using End Sub #End Region End Class
Solution VB.Net pour GroupBox avec bordure (non complète, elle redessine les contrôles contenus de manière incorrecte en mode conception )
#Region " Option Statements " Option Explicit On Option Ssortingct On Option Infer Off #End Region #Region " Imports " Imports System.ComponentModel #End Region #Region " ElektroGroupBox " ''' ''' A custom user control. ''' Public Class ElektroGroupBox : Inherits GroupBox #Region " Properties " ''' ''' Gets or sets the border color. ''' ''' The border color. Public Property BorderColor As Color Get Return Me.borderColor1 End Get Set(ByVal value As Color) Me.borderColor1 = value Me.Invalidate() End Set End Property ''' ''' The border color. ''' Private borderColor1 As Color = SystemColors.ControlDark ''' ''' Gets or sets the border style. ''' ''' The border color. Public Property BorderStyle As ButtonBorderStyle Get Return borderStyle1 End Get Set(ByVal value As ButtonBorderStyle) Me.borderStyle1 = value Me.Invalidate() End Set End Property ''' ''' The border style. ''' Private borderStyle1 As ButtonBorderStyle = ButtonBorderStyle.Solid #End Region #Region " Events " ''' ''' Handles the event. ''' ''' A that contains the event data. Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) ' MyBase.OnPaint(e) Me.DrawBorder(e) End Sub #End Region #Region " Private Methods " ''' ''' Draws a border on the control surface. ''' Private Sub DrawBorder(ByVal e As PaintEventArgs) ' The groupbox header text size. Dim textSize As Size = TextRenderer.MeasureText(Me.Text, Me.Font) ' The width of the blankspace drawn at the right side of the text. Dim blankWidthSpace As Integer = 3 ' The thex horizontal offset. Dim textOffset As Integer = 7 ' The rectangle where to draw the border. Dim borderRect As Rectangle = e.ClipRectangle With borderRect .Y = .Y + (textSize.Height \ 2) .Height = .Height - (textSize.Height \ 2) End With ' The rectangle where to draw the header text. Dim textRect As Rectangle = e.ClipRectangle With textRect .X = .X + textOffset .Width = (textSize.Width - blankWidthSpace) .Height = textSize.Height End With ' Draw the border. ControlPaint.DrawBorder(e.Graphics, borderRect, Me.borderColor1, Me.borderStyle1) ' Fill the text rectangle. e.Graphics.FillRectangle(New SolidBrush(Me.BackColor), textRect) ' Draw the text on the text rectangle. textRect.Width = textSize.Width + (blankWidthSpace * 2) ' Fix the right side space. e.Graphics.DrawSsortingng(Me.Text, Me.Font, New SolidBrush(Me.ForeColor), textRect) End Sub #End Region End Class #End Region