[分享]可以随意拖动的窗体类
| ||||
| ||||
'此类封装完整,使用的时候只需inherits shapedForm
Imports System.Drawing.Drawing2D Public Class ShapedForm Inherits System.Windows.Forms.Form ' 可以在子类窗体Load事件之前赋值 Public oFormPath As GraphicsPath Private oOriginalRegion As Region = Nothing ' 用于窗体移动 Private bFormDragging As Boolean = False Private oPointClicked As Point #Region " Windows 窗体设计器生成的代码 " Public Sub New() MyBase.New() '该调用是 Windows 窗体设计器所必需的。 InitializeComponent() '在 InitializeComponent() 调用之后添加任何初始化 End Sub '窗体重写 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() ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14) Me.ClientSize = New System.Drawing.Size(292, 266) Me.Name = "Form1" Me.Text = "Form1" End Sub #End Region Private Sub ShapedForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' 给子类提供一个设置窗体形状的机会 Me.SetInitialFormShape() If Not Me.oFormPath Is Nothing Then Me.AssignShapePath() End If End Sub Public Sub AssignShapePath() If Me.oOriginalRegion Is Nothing Then Me.oOriginalRegion = Me.Region End If Me.Region = New Region(Me.oFormPath) Me.Invalidate() End Sub Public Sub ResetShape() Me.Region = Me.oOriginalRegion Me.Invalidate() End Sub Public Overridable Sub SetInitialFormShape() ' 这个方法用来让子类覆盖 End Sub Private Sub ShapedForm_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _ Handles MyBase.MouseDown Me.bFormDragging = True Me.oPointClicked = New Point(e.X, e.Y) End Sub Private Sub ShapedForm_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _ Handles MyBase.MouseUp Me.bFormDragging = False End Sub Private Sub ShapedForm_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _ Handles MyBase.MouseMove If Me.bFormDragging Then Dim oMoveToPoint As Point ' 以当前鼠标位置为基础,找出目标位置 oMoveToPoint = Me.PointToScreen(New Point(e.X, e.Y)) ' 根据开始位置作出调整 oMoveToPoint.Offset(Me.oPointClicked.X * -1, (Me.oPointClicked.Y + SystemInformation.CaptionHeight + _ SystemInformation.BorderSize.Height) * -1) ' 移动窗体 Me.Location = oMoveToPoint End If End Sub End Class |
[分享][VB.Net]可以随意拖动的窗体类
最新推荐文章于 2021-10-30 18:46:28 发布