移动窗体和改变大小
Imports System.Runtime.InteropServices
Imports System.Drawing
Imports System
Imports System.Windows.Forms
Public Class Form1
Dim isMouseDown As Boolean = False
'表示鼠标当前是否处于按下状态,初始值为否
Dim direction As MouseDirection = MouseDirection.None
'表示拖动的方向,起始为None,表示不拖动
Dim mouseOff As Point
'定义一个枚举,表示拖动方向
Public Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Declare Function ReleaseCapture Lib "user32" Alias "ReleaseCapture" () As Boolean
Public Const WM_SYSCOMMAND = &H112
Public Const SC_MOVE = &HF010
Public Const HTCAPTION = &H2
Public Enum MouseDirection
Herizontal
'水平方向拖动,只改变窗体的宽度
Vertical
'垂直方向拖动,只改变窗体的高度
Declining
'倾斜方向,同时改变窗体的宽度和高度
None
End Enum
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Me.MouseDown
mouseOff = New Point(-e.X, -e.Y)
'记录鼠标位置
'当鼠标的位置处于边缘时,允许进行改变大小。
If e.Location.X >= Me.Width - 5 AndAlso e.Location.Y > Me.Height - 5 Then
isMouseDown = True
ElseIf e.Location.X >= Me.Width - 5 Then
isMouseDown = True
ElseIf e.Location.Y >= Me.Height - 5 Then
isMouseDown = True
Else
Me.Cursor = Cursors.Arrow
'改变鼠标样式为原样
isMouseDown = False
'鼠标移动事件
ReleaseCapture()
SendMessage(Me.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0)
End If
End Sub
Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Me.MouseUp
Console.WriteLine("松开鼠标")
isMouseDown = False
direction = MouseDirection.None
If isMouseDown Then
isMouseDown = False
End If
End Sub
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Me.MouseMove
'鼠标移动到边缘,改变鼠标的图标
If e.Location.X >= Me.Width - 5 AndAlso e.Location.Y > Me.Height - 5 Then
Me.Cursor = Cursors.SizeNWSE
direction = MouseDirection.Declining
ElseIf e.Location.X >= Me.Width - 5 Then
Me.Cursor = Cursors.SizeWE
direction = MouseDirection.Herizontal
ElseIf e.Location.Y >= Me.Height - 5 Then
Me.Cursor = Cursors.SizeNS
direction = MouseDirection.Vertical
Else
'否则,以外的窗体区域,鼠标星座均为单向箭头(默认)
Me.Cursor = Cursors.Arrow
End If
If e.Location.X >= (Me.Width + Me.Left + 10) OrElse (e.Location.Y > Me.Height + Me.Top + 10) Then
isMouseDown = False
End If
'设定好方向后,调用下面方法,改变窗体大小
ResizeWindow()
End Sub
Private Sub ResizeWindow()
If Not isMouseDown Then
Return
End If
If direction = MouseDirection.Declining Then
'Me.Cursor = Cursors.SizeNWSE
'改变宽度
Me.Width = MousePosition.X - Me.Left + 5
Me.Height = MousePosition.Y - Me.Top + 5
ElseIf direction = MouseDirection.Herizontal Then
'Me.Cursor = Cursors.SizeWE
'改变宽度
Me.Width = MousePosition.X - Me.Left + 5
ElseIf direction = MouseDirection.Vertical Then
'Me.Cursor = Cursors.SizeNS
'改变高度
Me.Height = MousePosition.Y - Me.Top + 5
Else
'鼠标不在窗口右和下边缘,把鼠标打回原型
Me.Cursor = Cursors.Arrow
isMouseDown = False
End If
End Sub
End Class
'移动窗体
Public Class Form1
'******************************************
Private oOriginalRegion As Region = Nothing
' 用于窗体移动
Private bFormDragging As Boolean = False
Private oPointClicked As Point
'******************************************
Private Sub Form1_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 Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
Me.bFormDragging = False
End Sub
'******************************************
Private Sub Form1_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
[本日志由 tiancao1001 于 2019-12-17 08:48 PM 编辑]
暂时没有评论