Tested On: VS Version: 2002 and 2003 .NET Frakework Version 1.1 and 1.0
Problem: A splitter control that is not too thick that it takes up too much valuable space, and not too thin that it is difficult for a user to locate.
Solution: A custom splitter with a handle and 3D-look. This solutions is very simple and does not require the complexities of using XP themes, or resource intensive operations of using Win32 native DLLs. [Click Image] 1) Add a class or User Control to you Solution 2) Inherit it from System.Windows.Forms.Splitter
Example: Imports System.Drawing.Text Imports System.Drawing Public Class CoolSplitter Inherits System.Windows.Forms.Splitter
Public Sub New() MyBase.New()
MyBase.SetStyle(ControlStyles.DoubleBuffer, True) MyBase.SetStyle(ControlStyles.ResizeRedraw, True) MyBase.SetStyle(ControlStyles.AllPaintingInWmPaint, True) MyBase.SetStyle(ControlStyles.SupportsTransparentBackColor, True) MyBase.UpdateStyles() Me.BackColor = System.Drawing.Color.Transparent
End Sub End Class
3)Notice, The new splitter control CoolSplitter is initialized with certain characteristics. The most important ones are : - ControlStyles.ResizeRedraw - ControlStyles.DoubleBuffer They allow for a flicker-free painting of the control.
4) Next, we need to decide on the new look n' feel for the splitter control. Like mentioned earlier, we need a 3D-lookin splitter control that has a handle drawn in the center. In order to accomplish this task we need to be somewhat familiar with GDI+ classes, and namespaces: System.Drawing and System.Drawing.Text
In order to draw anything on the surface of a control we need to get the controls Graphics object. The Graphics object represents all elements of a control that are visual and is responsible for drawing color,shades,shapes, etc. Do not create a new instance of a Graphics object, instead get the current instance, and modify that. The best place to change a controls look is right before it is painted (OnPaint method): Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs) Dim display As Graphics = e.Graphics 'Code to draw goes here... End Sub
There are two objects we need to draw. 1) 3D-surface 2) A hadle
3D-Surface To draw a 3D surface, we use the Graphics object's FillRectangle method and supply it with a special kind of brush, Drawing.Drawing2D.LinearGradientBrush. This brush paints two colors fading into each other. Example: Dim mybrush As Drawing.Drawing2D.LinearGradientBrush mybrush = New Drawing.Drawing2D.LinearGradientBrush(New PointF(0, 0), New PointF(0, Me.Height - 1), System.Drawing.SystemColors.ControlLight, Drawing.SystemColors.ControlDark) display.FillRectangle(mybrush, New Drawing.Rectangle(0, 0, Me.Width, Me.Height))
The two color used are System.Drawing.SystemColors.ControlLigh (begin color) and System.Drawing.SystemColors.ControlDark (ending color).
A Handle
We use the same technique here, but use a different style of Brush object -- a HatchBrush Example: Dim myrect As RectangleF = New RectangleF(Me.Width / 2, 0, 20, 2) display.FillRectangle(New Drawing.Drawing2D.HatchBrush(Drawing2D.HatchStyle.NarrowVertical, Me.ForeColor), myrect)
5) Finally, make sure you also handle the Resize event of your CoolSplitter Protected Overrides Sub OnResize(ByVal e As System.EventArgs) MyBase.Invalidate() 'repaint control upon resize End Sub ===================================================
Customizing the CoolSplitter (making it cooler)
=============================================
You can design the control in such a way that it allows users of your CoolSplitter to customize it a bit using the Property sheet. For instance, you might want fade colors and handle width to be customizable. The example below customizes the splitter handle background color
Public Property HandleColor() As System.Drawing.Color Get Return MyBase.ForeColor End Get Set(ByVal Value As System.Drawing.Color) MyBase.ForeColor = Value End Set End Property I hope this was a useful article. If you find any inconsistencies, or ambiguities, please contact me. If you have better solutions and would like to share with me, I will be delighted to learn about it.
Enjoy! > |