Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AndnixSH/cd04a66c59cd2a5687a7e7ef6b75f432 to your computer and use it in GitHub Desktop.
Save AndnixSH/cd04a66c59cd2a5687a7e7ef6b75f432 to your computer and use it in GitHub Desktop.
Flat TextBox control based on the System.Windows.Forms.TextBox that supports themes including custom border colors in normal and focused states
'Copyright (c) Smart PC Utilities, Ltd.
'All rights reserved.
#Region "References"
Imports System.Windows.Forms.Design
#End Region
Friend Class FlatTextBoxDesigner
Inherits ControlDesigner
#Region "Overridden Methods"
Protected Overrides Sub PreFilterProperties(properties As IDictionary)
MyBase.PreFilterProperties(properties)
properties.Remove("BorderStyle")
End Sub
#End Region
End Class
'Copyright (c) Smart PC Utilities, Ltd.
'All rights reserved.
Public Interface ITheme
#Region "Properties"
''' <summary>
''' Get or set the control style
''' </summary>
Property Theme As UITheme
#End Region
End Interface
'Copyright (c) Smart PC Utilities, Ltd.
'All rights reserved.
#Region "References"
Imports System.ComponentModel
Imports System.Drawing.Drawing2D
Imports FlatTextBox.NativeMethods
#End Region
<Designer(GetType(FlatTextBoxDesigner))> <ToolboxBitmap(GetType(TextBox))> Public Class MyFlatTextBox
Inherits TextBox
Implements ITheme
#Region "Private Members"
Private _theme As UITheme = UITheme.VS2019LightBlue
Private _drawBorder As Boolean = True
Private _borderNormalColor As Color = Color.FromArgb(204, 206, 219)
Private _borderActiveColor As Color = Color.FromArgb(0, 122, 204)
#End Region
#Region "Constants"
Private Const BORDER_WIDTH As Integer = 1
#End Region
#Region "Constructor"
Public Sub New()
BackColor = Color.White
ForeColor = Color.FromArgb(30, 30, 30)
BorderStyle = BorderStyle.None
DoubleBuffered = True
AutoSize = False
End Sub
#End Region
#Region "Public Properties"
<Localizable(True)> <Category("Appearance")> <Description("The backcolor of the Flat TextBox control.")> <DefaultValue(GetType(Color), "White")> Public Overrides Property BackColor As Color
Get
Return MyBase.BackColor
End Get
Set(value As Color)
MyBase.BackColor = value
End Set
End Property
<Localizable(True)> <Category("Appearance")> <Description("The forecolor of the Flat TextBox control.")> <DefaultValue(GetType(Color), "30, 30, 30")> Public Overrides Property ForeColor As Color
Get
Return MyBase.ForeColor
End Get
Set(value As Color)
MyBase.ForeColor = value
End Set
End Property
<Category("Appearance")> <Description("True to draw a border around the Flat TextBox control.")> <DefaultValue(True)> Public Property DrawBorder As Boolean
Get
Return _drawBorder
End Get
Set
If _drawBorder <> Value Then
_drawBorder = Value
UpdateStyles()
End If
End Set
End Property
<Localizable(True)> <Category("Appearance")> <Description("The border color of the Flat TextBox control in the normal state.")> <DefaultValue(GetType(Color), "204, 206, 219")> Public Property BorderColor As Color
Get
Return _borderNormalColor
End Get
Set(value As Color)
If _borderNormalColor <> value Then
_borderNormalColor = value
UpdateStyles()
End If
End Set
End Property
<Localizable(True)> <Category("Appearance")> <Description("The border color of the Flat TextBox control when the control has focus.")> <DefaultValue(GetType(Color), "0, 122, 204")> Public Property BorderActiveColor As Color
Get
Return _borderActiveColor
End Get
Set(value As Color)
If _borderActiveColor <> value Then
_borderActiveColor = value
UpdateStyles()
End If
End Set
End Property
<Category("Appearance")> <Description("The theme to apply to the Flat TextBox control.")> <DefaultValue(GetType(UITheme), "1")> Public Property Theme As UITheme Implements ITheme.Theme
Get
Return _theme
End Get
Set(value As UITheme)
_theme = value
If _theme = UITheme.VS2019DarkBlue Then
BackColor = Color.FromArgb(51, 51, 55)
ForeColor = Color.Gainsboro
BorderColor = Color.FromArgb(67, 67, 70)
BorderActiveColor = Color.FromArgb(0, 122, 204)
ElseIf _theme = UITheme.VS2019LightBlue Then
BackColor = Color.White
ForeColor = Color.FromArgb(30, 30, 30)
BorderColor = Color.FromArgb(204, 206, 219)
BorderActiveColor = Color.FromArgb(0, 122, 204)
End If
UpdateStyles()
End Set
End Property
#End Region
#Region "Overridden Properties"
Protected Overrides ReadOnly Property DefaultSize As Size
Get
Return New Size(100, 20)
End Get
End Property
Protected Overrides ReadOnly Property CreateParams As CreateParams
Get
Dim cp = MyBase.CreateParams
If _drawBorder Then cp.ExStyle = cp.ExStyle Or WindowStylesEx.WS_EX_CLIENTEDGE
Return cp
End Get
End Property
#End Region
#Region "Overridden Methods"
Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = WindowMessage.WM_NCPAINT AndAlso _drawBorder AndAlso Not DesignMode Then 'Draw the control border
Dim w As Integer
Dim h As Integer
Dim clip As Rectangle
Dim hdc As IntPtr
Dim clientRect As RECT = Nothing
GetClientRect(Handle, clientRect)
Dim windowRect As RECT = Nothing
GetWindowRect(Handle, windowRect)
w = windowRect.Right - windowRect.Left
h = windowRect.Bottom - windowRect.Top
clip = New Rectangle(CInt((w - clientRect.Right) / 2), CInt((h - clientRect.Bottom) / 2), clientRect.Right, clientRect.Bottom)
hdc = GetWindowDC(Handle)
Using g As Graphics = Graphics.FromHdc(hdc)
g.SetClip(clip, CombineMode.Exclude)
Using sb = New SolidBrush(BackColor)
g.FillRectangle(sb, 0, 0, w, h)
End Using
Using p = New Pen(If(Focused, _borderActiveColor, _borderNormalColor), BORDER_WIDTH)
g.DrawRectangle(p, 0, 0, w - 1, h - 1)
End Using
End Using
ReleaseDC(Handle, hdc)
Return
End If
MyBase.WndProc(m)
End Sub
Protected Overrides Sub OnBorderStyleChanged(e As EventArgs)
MyBase.OnBorderStyleChanged(e)
If BorderStyle <> BorderStyle.None Then
BorderStyle = BorderStyle.None
End If
End Sub
Protected Overrides Sub OnGotFocus(e As EventArgs)
MyBase.OnGotFocus(e)
UpdateStyles()
End Sub
Protected Overrides Sub OnLostFocus(e As EventArgs)
MyBase.OnLostFocus(e)
UpdateStyles()
End Sub
#End Region
End Class
'Copyright (c) Smart PC Utilities, Ltd.
'All rights reserved.
#Region "References"
Imports System.Runtime.InteropServices
#End Region
Friend Class NativeMethods
#Region "Win32 Methods"
''' <summary>
''' Retrieves the device context (DC) for the entire window, including title bar, menus, and scroll bars.
''' </summary>
''' <param name="hwnd">A handle to the window with a device context that is to be retrieved. If this value is NULL, GetWindowDC retrieves the device context for the entire screen.</param>
''' <returns>If the function succeeds, the return value is a handle to a device context for the specified window. If the function fails, the return value is NULL, indicating an error or an invalid hWnd parameter.</returns>
<DllImport("user32.dll")> Friend Shared Function GetWindowDC(hwnd As IntPtr) As IntPtr
End Function
''' <summary>
''' Releases a device context (DC), freeing it for use by other applications.
''' </summary>
''' <param name="hWnd">A handle to the window whose DC is to be released</param>
''' <param name="hDC">A handle to the DC to be released</param>
''' <returns>The return value indicates whether the DC was released. If the DC was released, the return value is 1. If the DC was not released, the return value is zero.</returns>
<DllImport("user32.dll")> Friend Shared Function ReleaseDC(hWnd As IntPtr, hDC As IntPtr) As Integer
End Function
<DllImport("user32.dll", SetLastError:=True)> Friend Shared Function GetClientRect(hWnd As IntPtr, <Out> ByRef lpRect As RECT) As Boolean
End Function
<DllImport("user32.dll", SetLastError:=True)> Friend Shared Function GetWindowRect(hWnd As IntPtr, <Out> ByRef lpRect As RECT) As Boolean
End Function
#End Region
#Region "Structures"
<StructLayout(LayoutKind.Sequential)>
Friend Structure RECT
Public Left, Top, Right, Bottom As Integer
Public Sub New(left As Integer, top As Integer, right As Integer, bottom As Integer)
Me.Left = left
Me.Top = top
Me.Right = right
Me.Bottom = bottom
End Sub
End Structure
#End Region
#Region "Enumerations"
Friend Enum WindowMessage As Integer
WM_CREATE = &H1
WM_NCPAINT = &H85
WM_NCLBUTTONDOWN = &HA1
WM_PAINT = &HF
WM_MOUSEWHEEL = &H20A
WM_NCCALCSIZE = &H83
WM_ERASEBKGND = &H14
WM_SIZE = &H5
WM_PRINTCLIENT = &H318
WM_HSCROLL = &H114
WM_VSCROLL = &H115
WM_KEYDOWN = &H100
WM_LBUTTONUP = &H202
WM_SHOWWINDOW = 24
End Enum
#End Region
End Class
'Copyright (c) Smart PC Utilities, Ltd.
'All rights reserved.
Public Enum UITheme As Integer
''' <summary>
''' User defined style
''' </summary>
Custom = -1
''' <summary>
''' Visual Studio 2019 Dark Blue style
''' </summary>
VS2019DarkBlue = 0
''' <summary>
''' Visual Studio 2019 Light Blue style
''' </summary>
VS2019LightBlue = 1
End Enum
'Copyright (c) Smart PC Utilities, Ltd.
'All rights reserved.
<Flags()> Friend Enum WindowStylesEx As Integer
''' <summary>Specifies a window that accepts drag-drop files.</summary>
WS_EX_ACCEPTFILES = &H10
''' <summary>Forces a top-level window onto the taskbar when the window is visible.</summary>
WS_EX_APPWINDOW = &H40000
''' <summary>Specifies a window that has a border with a sunken edge.</summary>
WS_EX_CLIENTEDGE = &H200
''' <summary>
''' Specifies a window that paints all descendants in bottom-to-top painting order using double-buffering.
''' This cannot be used if the window has a class style of either CS_OWNDC or CS_CLASSDC. This style is not supported in Windows 2000.
''' </summary>
''' <remarks>
''' With WS_EX_COMPOSITED set, all descendants of a window get bottom-to-top painting order using double-buffering.
''' Bottom-to-top painting order allows a descendant window to have translucency (alpha) and transparency (color-key) effects,
''' but only if the descendant window also has the WS_EX_TRANSPARENT bit set.
''' Double-buffering allows the window and its descendants to be painted without flicker.
''' </remarks>
WS_EX_COMPOSITED = &H2000000
''' <summary>
''' Specifies a window that includes a question mark in the title bar. When the user clicks the question mark,
''' the cursor changes to a question mark with a pointer. If the user then clicks a child window, the child receives a WM_HELP message.
''' The child window should pass the message to the parent window procedure, which should call the WinHelp function using the HELP_WM_HELP command.
''' The Help application displays a pop-up window that typically contains help for the child window.
''' WS_EX_CONTEXTHELP cannot be used with the WS_MAXIMIZEBOX or WS_MINIMIZEBOX styles.
''' </summary>
WS_EX_CONTEXTHELP = &H400
''' <summary>
''' Specifies a window which contains child windows that should take part in dialog box navigation.
''' If this style is specified, the dialog manager recurses into children of this window when performing navigation operations
''' such as handling the TAB key, an arrow key, or a keyboard mnemonic.
''' </summary>
WS_EX_CONTROLPARENT = &H10000
''' <summary>Specifies a window that has a double border.</summary>
WS_EX_DLGMODALFRAME = &H1
''' <summary>
''' Specifies a window that is a layered window.
''' This cannot be used for child windows or if the window has a class style of either CS_OWNDC or CS_CLASSDC.
''' </summary>
WS_EX_LAYERED = &H80000
''' <summary>
''' Specifies a window with the horizontal origin on the right edge. Increasing horizontal values advance to the left.
''' The shell language must support reading-order alignment for this to take effect.
''' </summary>
WS_EX_LAYOUTRTL = &H400000
''' <summary>Specifies a window that has generic left-aligned properties. This is the default.</summary>
WS_EX_LEFT = &H0
''' <summary>
''' Specifies a window with the vertical scroll bar (if present) to the left of the client area.
''' The shell language must support reading-order alignment for this to take effect.
''' </summary>
WS_EX_LEFTSCROLLBAR = &H4000
''' <summary>
''' Specifies a window that displays text using left-to-right reading-order properties. This is the default.
''' </summary>
WS_EX_LTRREADING = &H0
''' <summary>
''' Specifies a multiple-document interface (MDI) child window.
''' </summary>
WS_EX_MDICHILD = &H40
''' <summary>
''' Specifies a top-level window created with this style does not become the foreground window when the user clicks it.
''' The system does not bring this window to the foreground when the user minimizes or closes the foreground window.
''' The window does not appear on the taskbar by default. To force the window to appear on the taskbar, use the WS_EX_APPWINDOW style.
''' To activate the window, use the SetActiveWindow or SetForegroundWindow function.
''' </summary>
WS_EX_NOACTIVATE = &H8000000
''' <summary>
''' Specifies a window which does not pass its window layout to its child windows.
''' </summary>
WS_EX_NOINHERITLAYOUT = &H100000
''' <summary>
''' Specifies that a child window created with this style does not send the WM_PARENTNOTIFY message to its parent window when it is created or destroyed.
''' </summary>
WS_EX_NOPARENTNOTIFY = &H4
''' <summary>
''' The window does not render to a redirection surface.
''' This is for windows that do not have visible content or that use mechanisms other than surfaces to provide their visual.
''' </summary>
WS_EX_NOREDIRECTIONBITMAP = &H200000
''' <summary>Specifies an overlapped window.</summary>
WS_EX_OVERLAPPEDWINDOW = WS_EX_WINDOWEDGE Or WS_EX_CLIENTEDGE
''' <summary>Specifies a palette window, which is a mode-less dialog box that presents an array of commands.</summary>
WS_EX_PALETTEWINDOW = WS_EX_WINDOWEDGE Or WS_EX_TOOLWINDOW Or WS_EX_TOPMOST
''' <summary>
''' Specifies a window that has generic "right-aligned" properties. This depends on the window class.
''' The shell language must support reading-order alignment for this to take effect.
''' Using the WS_EX_RIGHT style has the same effect as using the SS_RIGHT (static), ES_RIGHT (edit), and BS_RIGHT/BS_RIGHTBUTTON (button) control styles.
''' </summary>
WS_EX_RIGHT = &H1000
''' <summary>Specifies a window with the vertical scroll bar (if present) to the right of the client area. This is the default.</summary>
WS_EX_RIGHTSCROLLBAR = &H0
''' <summary>
''' Specifies a window that displays text using right-to-left reading-order properties.
''' The shell language must support reading-order alignment for this to take effect.
''' </summary>
WS_EX_RTLREADING = &H2000
''' <summary>Specifies a window with a three-dimensional border style intended to be used for items that do not accept user input.</summary>
WS_EX_STATICEDGE = &H20000
''' <summary>
''' Specifies a window that is intended to be used as a floating toolbar.
''' A tool window has a title bar that is shorter than a normal title bar, and the window title is drawn using a smaller font.
''' A tool window does not appear in the taskbar or in the dialog that appears when the user presses ALT+TAB.
''' If a tool window has a system menu, its icon is not displayed on the title bar.
''' However, you can display the system menu by right-clicking or by typing ALT+SPACE.
''' </summary>
WS_EX_TOOLWINDOW = &H80
''' <summary>
''' Specifies a window that should be placed above all non-topmost windows and should stay above them, even when the window is deactivated.
''' To add or remove this style, use the SetWindowPos function.
''' </summary>
WS_EX_TOPMOST = &H8
''' <summary>
''' Specifies a window that should not be painted until siblings beneath the window (that were created by the same thread) have been painted.
''' The window appears transparent because the bits of underlying sibling windows have already been painted.
''' To achieve transparency without these restrictions, use the SetWindowRgn function.
''' </summary>
WS_EX_TRANSPARENT = &H20
''' <summary>Specifies a window that has a border with a raised edge.</summary>
WS_EX_WINDOWEDGE = &H100
End Enum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment