很早以前重写的XP_button类
怎么使用:
在窗体中将System.Windows.Forms.Button改为XPCtrl.XPButton即可
Friend WithEvents Button10 As XPCtrl.XPButton
...
Me.Button10 = New XPCtrl.XPButton
-----------------------------------------------------------------
Imports System
Imports System.Windows
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Namespace XPCtrl
Public Class XPButton
Inherits System.Windows.Forms.Button
Private mouseover As Boolean = False
Public Sub New()
End Sub
Protected Overloads Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim c5 As Color = Color.FromArgb(255, 255, 255)
Dim c2 As Color = Color.FromArgb(192, 192, 192)
If mouseover Then
c5 = Color.FromArgb(245, 245, 245)
c2 = Color.FromArgb(180, 175, 190)
End If
Dim b As Brush = New System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, c5, c2, LinearGradientMode.Vertical)
Dim offsetwidth As Integer = Me.Width / 50
Dim points(8) As Point
points(0).X = offsetwidth
points(0).Y = 0
points(1).X = Me.Width - offsetwidth
points(1).Y = 0
points(2).X = Me.Width
points(2).Y = offsetwidth
points(3).X = Me.Width
points(3).Y = Me.Height - offsetwidth
points(4).X = Me.Width - offsetwidth
points(4).Y = Me.Height
points(5).X = offsetwidth
points(5).Y = Me.Height
points(6).X = 0
points(6).Y = Me.Height - offsetwidth
points(7).X = 0
points(7).Y = offsetwidth
e.Graphics.FillPolygon(b, points, FillMode.Winding)
If Me.Focused Then
Dim offsetwidth1 As Integer = (Me.Width - 5) / 50 + 2
Dim points1(8) As Point
points1(0).X = offsetwidth1
points1(0).Y = 2
points1(1).X = Me.Width - offsetwidth1
points1(1).Y = 2
points1(2).X = Me.Width - 1
points1(2).Y = offsetwidth1
points1(3).X = Me.Width - 1
points1(3).Y = Me.Height - offsetwidth1
points1(4).X = Me.Width - offsetwidth1
points1(4).Y = Me.Height - 1
points1(5).X = 1
points1(5).Y = Me.Height - 1
points1(6).X = 2
points1(6).Y = Me.Height - offsetwidth1
points1(7).X = 2
points1(7).Y = offsetwidth1
Dim p As Pen = New Pen(Color.Orange, 2)
Dim p1 As Pen = New Pen(Color.Wheat, 2)
e.Graphics.DrawLine(p1, points1(0), points1(1))
e.Graphics.DrawLine(p, points1(1), points1(2))
e.Graphics.DrawLine(p, points1(2), points1(3))
e.Graphics.DrawLine(p, points1(3), points1(4))
e.Graphics.DrawLine(p, points1(4), points1(5))
e.Graphics.DrawLine(p, points1(5), points1(6))
e.Graphics.DrawLine(p1, points1(6), points1(7))
e.Graphics.DrawLine(p1, points1(7), points1(0))
End If
e.Graphics.DrawPolygon(New Pen(Color.DarkBlue, 2), points)
Dim drawFormat As StringFormat = New StringFormat
drawFormat.FormatFlags = StringFormatFlags.DisplayFormatControl
drawFormat.LineAlignment = StringAlignment.Center
drawFormat.Alignment = System.Drawing.StringAlignment.Center
Dim pf As PointF = New PointF(MyBase.Location.X, MyBase.Location.Y)
e.Graphics.DrawString(MyBase.Text, MyBase.Font, New LinearGradientBrush(Me.ClientRectangle, Color.Black, Color.Black, LinearGradientMode.Vertical), pf, drawFormat)
b.Dispose()
End Sub
Protected Overloads Overrides Sub OnMouseEnter(ByVal e As EventArgs)
mouseover = True
End Sub
Protected Overloads Overrides Sub OnNotifyMessage(ByVal m As System.Windows.Forms.Message)
MyBase.OnNotifyMessage(m)
End Sub
Protected Overloads Overrides Sub OnMouseLeave(ByVal e As EventArgs)
mouseover = False
End Sub
Protected Overloads Overrides Sub OnPaintBackground(ByVal pevent As System.Windows.Forms.PaintEventArgs)
pevent.Graphics.Clear(Color.Wheat)
End Sub
End Class
End Namespace
Public Class Tr_text
Inherits System.Windows.Forms.TextBox
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
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 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
'
'Tr_text
'
Me.Name = "Tr_text"
End Sub
#End Region
Protected Overrides Sub OnGotFocus(ByVal e As EventArgs)
Me.BackColor = System.Drawing.Color.FromArgb(CType(128, Byte), CType(255, Byte), CType(255, Byte))
MyBase.OnGotFocus(e)
End Sub
Protected Overrides Sub OnLostFocus(ByVal e As EventArgs)
Me.BackColor = Color.White
MyBase.OnLostFocus(e)
End Sub
End Class
Private StartPoint, EndPoint As New Point '保存鼠标移动的起始点和结束点
Private Sub Panel1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown '鼠标按下记录起始点
StartPoint.X = e.X
StartPoint.Y = e.Y
End Sub
Private Sub Panel1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseMove
If Not e.Button = MouseButtons.Left Then Exit Sub '鼠标移动,如果左键按下,则花一个临时的图形表示用户当前鼠标框选范围
Panel1.Refresh()
EndPoint.X = e.X
EndPoint.Y = e.Y
Dim x1, x2, y1, y2 As Int16
If EndPoint.X < StartPoint.X Then
x1 = EndPoint.X
x2 = StartPoint.X
Else
x2 = EndPoint.X
x1 = StartPoint.X
End If
If EndPoint.Y < StartPoint.Y Then
y1 = EndPoint.Y
y2 = StartPoint.Y
Else
y2 = EndPoint.Y
y1 = StartPoint.Y
End If
Dim g As Graphics
Dim pen As New Pen(Color.Black)
Dim w As Int16 = x2 - x1
Dim h As Int16 = y2 - y1
Dim rect As New Rectangle(x1, y1, w, h)
g = Panel1.CreateGraphics()
g.DrawRectangle(pen, rect)
End Sub
Private Sub Panel1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseUp '例子松开鼠标会花一个以短边为边长的正方形
Me.Refresh() '因为用的是panel,区域不大,可以用refresh刷新掉之前的图形再花新的
'如果区域大,可以参考区域更新
EndPoint.X = e.X '保存结束点
EndPoint.Y = e.Y
If EndPoint.X < StartPoint.X Then '计算大小,保证绘制矩形的长宽是正数
Dim t As Int16
t = StartPoint.X
StartPoint.X = EndPoint.X
EndPoint.X = t
End If
If EndPoint.Y < StartPoint.Y Then
Dim t As Int16
t = StartPoint.Y
StartPoint.Y = EndPoint.Y
EndPoint.Y = t
End If
Dim g As Graphics '规则的GDI+绘制矩形的步骤
Dim pen As New Pen(Color.Black)
Dim w As Int16 = EndPoint.X - StartPoint.X
Dim h As Int16 = EndPoint.Y - StartPoint.Y
Dim rect As New Rectangle(StartPoint.X, StartPoint.Y, w, h)
If w > h Then
rect = New Rectangle(StartPoint.X, StartPoint.Y, h, h)
Else
rect = New Rectangle(StartPoint.X, StartPoint.Y, w, w)
End If
g = Panel1.CreateGraphics()
g.DrawRectangle(pen, rect)
GR.Ephemeris(Panel1, g, True, rect)
End Sub