自定义控件--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 "字体颜色 <Description("字体颜色。")> _ Public Property textcolor() As Color Get Return m_textcolor End Get Set(ByVal Value As Color) m_textcolor = Value Me.Invalidate() End Set End Property Public Sub New() MyBase.New()
"该调用是 Windows 窗体设计器所必需的。 InitializeComponent()
"在 InitializeComponent() 调用之后添加任何初始化,true表示将指定的样式应用到控件
"设置控件样式位能够充分地更改控件行为 Me.SetStyle(ControlStyles.UserPaint, True) "关联事件委托 AddHandler Me.MouseDown, AddressOf my_OnMouseDown AddHandler Me.MouseUp, AddressOf my_OnMouseUp AddHandler Me.MouseEnter, AddressOf my_OnMouseEnter
AddHandler Me.MouseLeave, AddressOf my_OnMouseLeave Height = 23 Width = 75 End Sub
Protected Overrides Sub OnPaint(ByVal pevent As System.Windows.Forms.PaintEventArgs) "pevent.ClipRectangle指在其中绘制的矩形,即使用父控件的背景色来画这个矩形按钮 pevent.Graphics.FillRectangle(New SolidBrush(Me.Parent.BackColor), pevent.ClipRectangle) If (Enabled = False) Then "画不可用状态 DrawDisableButton(pevent.Graphics) ElseIf (my_mouseDown) Then "画鼠标按下状态 DrawMouseDownButton(pevent.Graphics) ElseIf (my_mouseHover) Then "画鼠标移动到其上状态 DrawMouseHoverButton(pevent.Graphics) ElseIf (Focused) Then "有焦点,但鼠标未移动到其上 DrawContainFocusButton(pevent.Graphics) Else "一般情况下 DrawNormalButton(pevent.Graphics) End If "写文本 WriteText(pevent.Graphics) End Sub "鼠标按下的状态处理 Private Sub my_OnMouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) my_mouseDown = True "鼠标按下 End Sub "鼠标松开状态的处理 Private Sub my_OnMouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) my_mouseDown = False "鼠标松开 |
