[分享][VB.Net]可以随意拖动的窗体类

[分享]可以随意拖动的窗体类
'此类封装完整,使用的时候只需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
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值