一光標的指示 當光標移動到要拖動或縮放的控件上時
應顯示相應的動作箭頭
定義一個枚舉的變量來標識對應的狀態
Enum EnumMousePointPosition
MouseSizeNone =
無
MouseSizeRight =
拉伸右邊框
MouseSizeLeft =
拉伸左邊框
MouseSizeBottom =
拉伸下邊框
MouseSizeTop =
拉伸上邊框
MouseSizeTopLeft =
拉伸左上角
MouseSizeTopRight =
拉伸右上角
MouseSizeBottomLeft =
拉伸左下角
MouseSizeBottomRight =
拉伸右下角
MouseDrag =
鼠標拖動
End Enum
Dim m_MousePointPosition As EnumMousePointPosition
在MouseMove事件中讀取進入控件的光標位置
用下面函數判斷光標的狀態
m_MousePointPosition = MousePointPosition(sender
Size
e)
按對應的狀態顯示光標形狀
Select Case m_MousePointPosition
Case EnumMousePointPosition
MouseSizeNone
Me
Cursor = Cursors
Arrow
箭頭
Case EnumMousePointPosition
MouseDrag
Me
Cursor = Cursors
SizeAll
四方向
Case EnumMousePointPosition
MouseSizeBottom
Me
Cursor = Cursors
SizeNS
南北
Case EnumMousePointPosition
MouseSizeTop
Me
Cursor = Cursors
SizeNS
南北
Case EnumMousePointPosition
MouseSizeLeft
Me
Cursor = Cursors
SizeWE
東西
Case EnumMousePointPosition
MouseSizeRight
Me
Cursor = Cursors
SizeWE
東西
Case EnumMousePointPosition
MouseSizeBottomLeft
Me
Cursor = Cursors
SizeNESW
東北到南西
Case EnumMousePointPosition
MouseSizeBottomRight
Me
Cursor = Cursors
SizeNWSE
東南到西北
Case EnumMousePointPosition
MouseSizeTopLeft
Me
Cursor = Cursors
SizeNWSE
東南到西北
Case EnumMousePointPosition
MouseSizeTopRight
Me
Cursor = Cursors
SizeNESW
東北到南西
End Select
如果光標離開控件
MouseMove事件將不響應
因此用需用MouseLeave事件來接力完成光標狀態的標識和顯示
Private Sub MyMouseLeave(ByVal sender As Object
ByVal e As System
EventArgs)
m_MousePointPosition = EnumMousePointPosition
MouseSizeNone
Me
Cursor = Cursors
Arrow
End Sub
二拖動 拖動的原理是移動控件的Location到當前光標的位置
並保持控件的高度和寬度不變
如果記錄起始光標點為p
當前光標點為e
那麼x方向的移動距離是e
X
p
X
x方向的移動距離是e
Y
p
Y
在鼠標按下時
即是拖動的開始
在MouseDown事件中
記錄光標開始拖動點
p = New Point(e
X
e
Y)
在MouseMove事件
檢測是否持續按著鼠標左鍵
如果是則更新控件的起點位置
If e
Button = MouseButtons
Left Then
sender
Location = New Point(sender
Left + e
X
p
X
sender
Top + e
Y
p
Y)
End If
三縮放 縮放的原理和拖動類似
如果縮放底邊
就是根據光標的移動改變控件的高度
而不改變控件的起點位置
同樣
如果縮放底邊
在鼠標按下時
即是縮放的開始
在MouseDown事件中
記錄光標開始拖動點
p = New Point(e
X
e
Y)
在MouseMove事件
檢測是否持續按著鼠標左鍵
如果是則更新控件的高度
If e
Button = MouseButtons
Left Then
sender
Size = New Size(sender
Width
sender
Height + e
Y
p
Y)
p
= New Point(e
X
e
Y)
記錄光標拖動的當前點
End If
這裡和拖動有些區別
就是高度的變化會導致光標位置數值的變化
因此需重新用p
定位光標起始點的位置
所有涉及控件底邊和右邊的縮放都要這樣處理
而左邊和頂邊則不用
其它所有各方向的拖動都可以類推
四委派 所有的操作
一共總結為三個函數
MyMouseDown
MyMouseMove
MyMouseLeave
動態創建控件後
需委派相應的事件到這三個函數
如果創建一個按鈕
Dim Button As New Button
Controls
Add(Button)
AddHandler Button
MouseDown
AddressOf MyMouseDown
AddHandler Button
MouseMove
AddressOf MyMouseMove
AddHandler Button
MouseLeave
AddressOf MyMouseLeave
五源碼 建立一個窗體
放置一個按鈕
點擊該按鈕創建動態建立一個可以拖動和縮放的按鈕
全部源碼如下
Public Class Form
Inherits System
Windows
Forms
Form
Enum EnumMousePointPosition
MouseSizeNone =
無
MouseSizeRight =
拉伸右邊框
MouseSizeLeft =
拉伸左邊框
MouseSizeBottom =
拉伸下邊框
MouseSizeTop =
拉伸上邊框
MouseSizeTopLeft =
拉伸左上角
MouseSizeTopRight =
拉伸右上角
MouseSizeBottomLeft =
拉伸左下角
MouseSizeBottomRight =
拉伸右下角
MouseDrag =
鼠標拖動
End Enum
Dim m_MousePointPosition As EnumMousePointPosition
Dim p
p
As Point
Private Sub MyMouseDown(ByVal sender As Object
ByVal e As System
Windows
Forms
MouseEventArgs)
p = New Point(e
X
e
Y)
記錄光標開始拖動點
p
= New Point(e
X
e
Y)
End Sub
Private Sub MyMouseLeave(ByVal sender As Object
ByVal e As System
EventArgs)
m_MousePointPosition = EnumMousePointPosition
MouseSizeNone
Me
Cursor = Cursors
Arrow
End Sub
Private Sub MyMouseMove(ByVal sender As Object
ByVal e As System
Windows
Forms
MouseEventArgs)
If e
Button = MouseButtons
Left Then
Select Case m_MousePointPosition
Case EnumMousePointPosition
MouseDrag
sender
Location = New Point(sender
Left + e
X
p
X
sender
Top + e
Y
p
Y)
Case EnumMousePointPosition
MouseSizeBottom
sender
Size = New Size(sender
Width
sender
Height + e
Y
p
Y)
p
= New Point(e
X
e
Y)
記錄光標拖動的當前點
Case EnumMousePointPosition
MouseSizeBottomRight
sender
Size = New Size(sender
Width + e
X
p
X
sender
Height + e
Y
p
Y)
p
= New Point(e
X
e
Y)
Case EnumMousePointPosition
MouseSizeRight
sender
Size = New Size(sender
Width + e
X
p
X
sender
Height)
p
= New Point(e
X
e
Y)
Case EnumMousePointPosition
MouseSizeTop
sender
Location = New Point(sender
Left
sender
Top + (e
Y
p
Y))
sender
Size = New Size(sender
Width
sender
Height
(e
Y
p
Y))
Case EnumMousePointPosition
MouseSizeLeft
sender
Location = New Point(sender
Left + e
X
p
X
sender
Top)
sender
Size = New Size(sender
Width
(e
X
p
X)
sender
Height)
Case EnumMousePointPosition
MouseSizeBottomLeft
sender
Location = New Point(sender
Left + e
X
p
X
sender
Top)
sender
Size = New Size(sender
Width
(e
X
p
X)
sender
Height + e
Y
p
Y)
p
= New Point(e
X
e
Y)
Case EnumMousePointPosition
MouseSizeTopRight
sender
Location = New Point(sender
Left
sender
Top + (e
Y
p
Y))
sender
Size = New Size(sender
Width + (e
X
p
X)
sender
Height
(e
Y
p
Y))
p
= New Point(e
X
e
Y)
Case EnumMousePointPosition
MouseSizeTopLeft
sender
Location = New Point(sender
Left + e
X
p
X
sender
Top + (e
Y
p
Y))
sender
Size = New Size(sender
Width
(e
X
p
X)
sender
Height
(e
Y
p
Y))
End Select
Else
m_MousePointPosition = MousePointPosition(sender
Size
e)
判斷光標的位置狀態
Select Case m_MousePointPosition
改變光標
From:http://tw.wingwit.com/Article/program/net/201311/13842.html