自定义控件--xp风格按钮(可设置文字颜色)

Imports System.Drawing

Imports System.ComponentModel

Public Class winxpbutton

Inherits System.Windows.Forms.Button

Private my_mouseDown As Boolean = False ' 鼠标按下

Private my_mouseHover As Boolean = False ' 鼠标移到上面

Private m_textcolor As Color = System.Drawing.Color.Black ' 字体颜色

  1<description(" ")="" 字体颜色。=""> _ 
  2
  3Public  Property  textcolor()  As  Color 
  4
  5Get 
  6
  7Return  m_textcolor 
  8
  9End  Get 
 10
 11Set  (  ByVal  Value  As  Color) 
 12
 13m_textcolor = Value 
 14
 15Me  .Invalidate() 
 16
 17End  Set 
 18
 19End  Property 
 20
 21Public  Sub  New  () 
 22
 23MyBase  .New() 
 24
 25'  该调用是  Windows  窗体设计器所必需的。 
 26
 27InitializeComponent() 
 28
 29'  在  InitializeComponent()  调用之后添加任何初始化  ,true  表示将指定的样式应用到控件 
 30
 31'  设置控件样式位能够充分地更改控件行为 
 32
 33Me  .SetStyle(ControlStyles.UserPaint,  True  ) 
 34
 35'  关联事件委托 
 36
 37AddHandler  Me  .MouseDown,  AddressOf  my_OnMouseDown 
 38
 39AddHandler  Me  .MouseUp,  AddressOf  my_OnMouseUp 
 40
 41AddHandler  Me  .MouseEnter,  AddressOf  my_OnMouseEnter 
 42
 43AddHandler  Me  .MouseLeave,  AddressOf  my_OnMouseLeave 
 44
 45Height = 23 
 46
 47Width = 75 
 48
 49End  Sub 
 50
 51Protected  Overrides  Sub  OnPaint(  ByVal  pevent  As  System.Windows.Forms.PaintEventArgs) 
 52
 53'pevent.ClipRectangle  指在其中绘制的矩形  ,  即使用父控件的背景色来画这个矩形按钮 
 54
 55pevent.Graphics.FillRectangle(  New  SolidBrush(  Me  .Parent.BackColor), pevent.ClipRectangle) 
 56
 57If  (Enabled =  False  )  Then 
 58
 59'  画不可用状态 
 60
 61DrawDisableButton(pevent.Graphics) 
 62
 63ElseIf  (my_mouseDown)  Then  '  画鼠标按下状态 
 64
 65DrawMouseDownButton(pevent.Graphics) 
 66
 67ElseIf  (my_mouseHover)  Then  '  画鼠标移动到其上状态 
 68
 69DrawMouseHoverButton(pevent.Graphics) 
 70
 71ElseIf  (Focused)  Then  '  有焦点,但鼠标未移动到其上 
 72
 73DrawContainFocusButton(pevent.Graphics) 
 74
 75Else  '  一般情况下 
 76
 77DrawNormalButton(pevent.Graphics) 
 78
 79End  If 
 80
 81'  写文本 
 82
 83WriteText(pevent.Graphics) 
 84
 85End  Sub 
 86
 87'  鼠标按下的状态处理 
 88
 89Private  Sub  my_OnMouseDown(  ByVal  sender  As  Object  ,  ByVal  e  As  MouseEventArgs) 
 90
 91my_mouseDown =  True  '  鼠标按下 
 92
 93End  Sub 
 94
 95'  鼠标松开状态的处理 
 96
 97Private  Sub  my_OnMouseUp(  ByVal  sender  As  Object  ,  ByVal  e  As  MouseEventArgs) 
 98
 99my_mouseDown =  False  '  鼠标松开 
100
101'  重新绘制控件时发生  Paint  事件。  PaintEventArgs  指定绘制控件所用的  Graphics 
102
103'  以及绘制控件所在的  ClipRectangle  。 
104
105Dim  pe  As  PaintEventArgs =  New  PaintEventArgs(CreateGraphics(), ClientRectangle) 
106
107OnPaint(pe) 
108
109End  Sub 
110
111'  鼠标进入 
112
113Private  Sub  my_OnMouseEnter(  ByVal  sender  As  Object  ,  ByVal  e  As  EventArgs) 
114
115my_mouseHover =  True  '  鼠标移动到其上 
116
117' 
118
119Dim  pe  As  PaintEventArgs =  New  PaintEventArgs(CreateGraphics(), ClientRectangle) 
120
121OnPaint(pe) 
122
123End  Sub 
124
125'  鼠标移动开 
126
127Private  Sub  my_OnMouseLeave(  ByVal  sender  As  Object  ,  ByVal  e  As  EventArgs) 
128
129my_mouseHover =  False  '  鼠标移动开 
130
131' 
132
133Dim  pe  As  PaintEventArgs =  New  PaintEventArgs(CreateGraphics(), ClientRectangle) 
134
135OnPaint(pe) 
136
137End  Sub 
138
139Private  Sub  DrawBorder(  ByVal  g  As  Graphics,  ByVal  state  As  Integer  ) 
140
141If  (state = 1)  Then  '  绘制一般边框 
142
143'  绘制一个画笔,高光点,宽度  2 
144
145Dim  p  As  Pen =  New  Pen(SystemColors.ControlLightLight, 2) 
146
147'g.DrawLine  画线,  p  是画笔,后面是第一个点的坐标,第二个点的坐标 
148
149g.DrawLine(p, 1, 1, 1, Height - 2)  '  绘制左侧竖线 
150
151g.DrawLine(p, 1, 1, Width - 2, 1)  '  绘制上面横线 
152
153g.DrawLine(p, Width - 1, 2, Width - 1, Height - 2)  '  绘制右侧竖线,由于已经在上面绘制了横线(纵坐标为  1  ),所以从  2  开始 
154
155g.DrawLine(p, 2, Height - 1, Width - 2, Height - 1)  '  绘制下面横线 
156
157ElseIf  (state = 2)  Then  '  绘制移动到其上的边框 
158
159'  与一般边框用高光区别的是显示黄色 
160
161Dim  p  As  Pen =  New  Pen(Color.Yellow, 2) 
162
163g.DrawLine(p, 1, 1, 1, Height - 2) 
164
165g.DrawLine(p, 1, 1, Width - 2, 1) 
166
167g.DrawLine(p, Width - 1, 2, Width - 1, Height - 2) 
168
169g.DrawLine(p, 2, Height - 1, Width - 2, Height - 1) 
170
171ElseIf  (state = 3)  Then  '  绘制按下的显示边框 
172
173'  与一般边框用高光区别的是显示暗褐色 
174
175Dim  p  As  Pen =  New  Pen(SystemColors.ControlDark, 2) 
176
177g.DrawLine(p, 1, 1, 1, Height - 2) 
178
179g.DrawLine(p, 1, 1, Width - 2, 1) 
180
181g.DrawLine(p, Width - 1, 2, Width - 1, Height - 2) 
182
183g.DrawLine(p, 2, Height - 1, Width - 2, Height - 1) 
184
185ElseIf  (state = 4)  Then  '  绘制不可用状态边框 
186
187'  与一般边框用高光区别的是显示亮色 
188
189Dim  p  As  Pen =  New  Pen(SystemColors.ControlLight, 2) 
190
191g.DrawLine(p, 1, 1, 1, Height - 2) 
192
193g.DrawLine(p, 1, 1, Width - 2, 1) 
194
195g.DrawLine(p, Width - 1, 2, Width - 1, Height - 2) 
196
197g.DrawLine(p, 2, Height - 1, Width - 2, Height - 1) 
198
199ElseIf  (state = 5)  Then  '  绘制有焦点但鼠标不在其上的状态 
200
201'  与一般边框用高光区别的是显示兰色 
202
203Dim  p  As  Pen =  New  Pen(Color.SkyBlue, 2) 
204
205g.DrawLine(p, 1, 1, 1, Height - 2) 
206
207g.DrawLine(p, 1, 1, Width - 2, 1) 
208
209g.DrawLine(p, Width - 1, 2, Width - 1, Height - 2) 
210
211g.DrawLine(p, 2, Height - 1, Width - 2, Height - 1) 
212
213End  If 
214
215'//  做完如上的处理后再对可用和不可用做圆化边缘处理(也就是把按钮的  4  个角进行圆化处理) 
216
217If  (state = 4)  Then  '  不可用时 
218
219'  &lt;span style="FONT-SIZE: 9pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: "Times New Roman&amp;qu</description(">
Published At
Categories with Web编程
Tagged with
comments powered by Disqus