重画系列:双色Button

效果如下图:

比较简单,下面是源代码,新建一类库工程,拷贝以下源代码覆盖Class1,生成Dll文件就可以使用了

Public Class XpStyleButton
Inherits System.Windows.Forms.UserControl

#Region " Windows 窗体设计器生成的代码 "

Public Sub New()
MyBase.New()

'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()

'在 InitializeComponent() 调用之后添加任何初始化
setstyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.DoubleBuffer Or ControlStyles.UserPaint, True)
End Sub

'UserControl1 重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer

'注意:以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。

  1<system.diagnostics.debuggerstepthrough()> Private Sub InitializeComponent()   
  2'   
  3'XpStyleButton   
  4'   
  5Me.Name = "XpStyleButton"   
  6Me.Size = New System.Drawing.Size(264, 88) 
  7
  8End Sub 
  9
 10#End Region   
 11Private IsMouseDown As Boolean = False   
 12Private xMagin As Integer = 2   
 13Private yMagin As Integer = 2   
 14Private m_BackColor As Color = Color.BurlyWood   
 15Private m_ForeColor As Color = Color.White   
 16Private m_Style As Style = Style.RectStyle   
 17'button's Styles   
 18Public Enum Style   
 19RectStyle = 0   
 20EllipseStyle = 1   
 21CircleStyle = 2   
 22End Enum 
 23
 24  
 25'--------------------对外属性--------------   
 26'亮色   
 27Public Property LightColor() As Color   
 28Get   
 29Return m_ForeColor   
 30End Get   
 31Set(ByVal Value As Color)   
 32m_ForeColor = Value   
 33End Set   
 34End Property   
 35'暗色   
 36Public Property GrayColor() As Color   
 37Get   
 38Return Me.m_BackColor   
 39End Get   
 40Set(ByVal Value As Color)   
 41Me.m_BackColor = Value   
 42End Set   
 43End Property   
 44Public Property ButtonStyle() As Style   
 45Get   
 46Return Me.m_Style   
 47End Get   
 48Set(ByVal Value As Style)   
 49If Value = Style.CircleStyle Then   
 50Me.Height = Me.Width   
 51End If   
 52Me.m_Style = Value   
 53End Set   
 54End Property   
 55'在这里实现重画双色Button   
 56Private Sub XpStyleButton_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint   
 57Me.BackColor = Me.Parent.BackColor   
 58Dim brush As Drawing.Drawing2D.LinearGradientBrush   
 59Dim brush1 As Drawing.Drawing2D.LinearGradientBrush   
 60brush = New Drawing.Drawing2D.LinearGradientBrush(New PointF(0, 0), New PointF(0, Me.Height), Me.m_ForeColor, Me.m_BackColor)   
 61'对鼠标单击响应   
 62If Me.IsMouseDown Then   
 63brush1 = New Drawing.Drawing2D.LinearGradientBrush(New PointF(xMagin, yMagin), New PointF(xMagin, Me.Height), Color.FromArgb(0, 255, 255, 255), Color.FromArgb(150, 255, 255, 255))   
 64Else   
 65brush1 = New Drawing.Drawing2D.LinearGradientBrush(New PointF(xMagin, yMagin), New PointF(xMagin, Me.Height / 2), Color.FromArgb(150, 255, 255, 255), Color.FromArgb(0, 255, 255, 255))   
 66End If   
 67brush.WrapMode = Drawing.Drawing2D.WrapMode.TileFlipX   
 68brush1.WrapMode = Drawing.Drawing2D.WrapMode.TileFlipX 
 69
 70Dim rect As Rectangle = New Rectangle(0, 0, Me.Width - xMagin, Me.Height - yMagin)   
 71Dim rect1 As Rectangle = New Rectangle(xMagin, yMagin, Me.Width - 2 * xMagin, Me.Height / 2)   
 72Select Case Me.m_Style   
 73Case Style.RectStyle   
 74Me.DrawRectStyle(e.Graphics, rect, rect1, brush, brush1)   
 75Case Style.EllipseStyle   
 76Me.DrawEllipseStyle(e.Graphics, rect, rect1, brush, brush1)   
 77Case Style.CircleStyle   
 78Me.DrawEllipseStyle(e.Graphics, rect, rect1, brush, brush1)   
 79Case Else   
 80Me.DrawRectStyle(e.Graphics, rect, rect1, brush, brush1)   
 81End Select 
 82
 83End Sub   
 84'画椭圆形按钮(圆型按钮也是由它重画)   
 85Private Sub DrawEllipseStyle(ByRef g As Graphics, ByVal rect As Rectangle, ByVal rect1 As Rectangle, ByVal bbrush As Brush, ByVal fbrush As Brush)   
 86g.SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias   
 87g.FillEllipse(bbrush, rect)   
 88g.FillEllipse(fbrush, rect)   
 89g.DrawEllipse(New Pen(m_BackColor), rect)   
 90End Sub   
 91'画矩形按钮   
 92Private Sub DrawRectStyle(ByRef g As Graphics, ByVal rect As Rectangle, ByVal rect1 As Rectangle, ByVal bbrush As Brush, ByVal fbrush As Brush)   
 93g.SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias   
 94g.FillRectangle(bbrush, rect)   
 95g.FillRectangle(fbrush, rect)   
 96g.DrawRectangle(New Pen(m_BackColor), rect)   
 97End Sub   
 98Private Sub XpStyleButton_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown   
 99IsMouseDown = True   
100Me.Refresh()   
101End Sub 
102
103Private Sub XpStyleButton_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp   
104IsMouseDown = False   
105Me.Refresh()   
106End Sub 
107
108  
109End Class   
110以下是测试的源代码,需要添加上面生成的Dll的引用: 
111
112Public Class Form1   
113Inherits System.Windows.Forms.Form 
114
115#Region " Windows 窗体设计器生成的代码 " 
116
117Public Sub New()   
118MyBase.New() 
119
120'该调用是 Windows 窗体设计器所必需的。   
121InitializeComponent() 
122
123'在 InitializeComponent() 调用之后添加任何初始化 
124
125End Sub 
126
127'窗体重写处置以清理组件列表。   
128Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)   
129If disposing Then   
130If Not (components Is Nothing) Then   
131components.Dispose()   
132End If   
133End If   
134MyBase.Dispose(disposing)   
135End Sub   
136Private button1 As XPStyleButton.XpStyleButton   
137'Windows 窗体设计器所必需的   
138Private components As System.ComponentModel.IContainer 
139
140'注意:以下过程是 Windows 窗体设计器所必需的   
141'可以使用 Windows 窗体设计器修改此过程。   
142'不要使用代码编辑器修改它。   
143Friend WithEvents XpStyleButton1 As XPStyleButton.XpStyleButton   
144Friend WithEvents XpStyleButton4 As XPStyleButton.XpStyleButton   
145<system.diagnostics.debuggerstepthrough()> Private Sub InitializeComponent()   
146Me.button1 = New XPStyleButton.XpStyleButton()   
147Me.XpStyleButton1 = New XPStyleButton.XpStyleButton()   
148Me.XpStyleButton4 = New XPStyleButton.XpStyleButton()   
149Me.SuspendLayout()   
150'   
151'button1   
152'   
153Me.button1.BackColor = System.Drawing.SystemColors.Control   
154Me.button1.ButtonStyle = XPStyleButton.XpStyleButton.Style.RectStyle   
155Me.button1.ForeColor = System.Drawing.Color.White   
156Me.button1.GrayColor = System.Drawing.Color.FromArgb(CType(0, Byte), CType(192, Byte), CType(0, Byte))   
157Me.button1.LightColor = System.Drawing.SystemColors.HighlightText   
158Me.button1.Location = New System.Drawing.Point(56, 32)   
159Me.button1.Name = "button1"   
160Me.button1.Size = New System.Drawing.Size(88, 25)   
161Me.button1.TabIndex = 0   
162'   
163'XpStyleButton1   
164'   
165Me.XpStyleButton1.BackColor = System.Drawing.SystemColors.Control   
166Me.XpStyleButton1.ButtonStyle = XPStyleButton.XpStyleButton.Style.CircleStyle   
167Me.XpStyleButton1.GrayColor = System.Drawing.Color.Blue   
168Me.XpStyleButton1.LightColor = System.Drawing.Color.White   
169Me.XpStyleButton1.Location = New System.Drawing.Point(280, 24)   
170Me.XpStyleButton1.Name = "XpStyleButton1"   
171Me.XpStyleButton1.Size = New System.Drawing.Size(104, 64)   
172Me.XpStyleButton1.TabIndex = 1   
173'   
174'XpStyleButton4   
175'   
176Me.XpStyleButton4.BackColor = System.Drawing.SystemColors.Control   
177Me.XpStyleButton4.ButtonStyle = XPStyleButton.XpStyleButton.Style.CircleStyle   
178Me.XpStyleButton4.GrayColor = System.Drawing.Color.BurlyWood   
179Me.XpStyleButton4.LightColor = System.Drawing.Color.White   
180Me.XpStyleButton4.Location = New System.Drawing.Point(168, 16)   
181Me.XpStyleButton4.Name = "XpStyleButton4"   
182Me.XpStyleButton4.Size = New System.Drawing.Size(96, 96)   
183Me.XpStyleButton4.TabIndex = 2   
184'   
185'Form1   
186'   
187Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)   
188Me.ClientSize = New System.Drawing.Size(520, 273)   
189Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.button1, Me.XpStyleButton4, Me.XpStyleButton1})   
190Me.Name = "Form1"   
191Me.Text = "Form1"   
192Me.ResumeLayout(False) 
193
194End Sub 
195
196#End Region 
197
198End Class   
199谢谢阅读,希望有什么意见能和我联系, [email protected] qq: 44460100</system.diagnostics.debuggerstepthrough()></system.diagnostics.debuggerstepthrough()>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus