分享
 
 
 

控件的拖动和缩放技术全解

王朝c#·作者佚名  2006-12-17
窄屏简体版  字體: |||超大  

一、光标的指示

当光标移动到要拖动或缩放的控件上时,应显示相应的动作箭头。定义一个枚举的变量来标识对应的状态。

Enum EnumMousePointPosition

MouseSizeNone = 0 '无

MouseSizeRight = 1 '拉伸右边框

MouseSizeLeft = 2 '拉伸左边框

MouseSizeBottom = 3 '拉伸下边框

MouseSizeTop = 4 '拉伸上边框

MouseSizeTopLeft = 5 '拉伸左上角

MouseSizeTopRight = 6 '拉伸右上角

MouseSizeBottomLeft = 7 '拉伸左下角

MouseSizeBottomRight = 8 '拉伸右下角

MouseDrag = 9 '鼠标拖动

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 - p1.Y)

p1 = New Point(e.X, e.Y) '记录光标拖动的当前点

End If

这里和拖动有些区别,就是高度的变化会导致光标位置数值的变化,因此需重新用p1定位光标起始点的位置。所有涉及控件底边和右边的缩放都要这样处理,而左边和顶边则不用。

其它所有各方向的拖动都可以类推。

四、委派

所有的操作,一共总结为三个函数,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 Form1

Inherits System.Windows.Forms.Form

Enum EnumMousePointPosition

MouseSizeNone = 0 '无

MouseSizeRight = 1 '拉伸右边框

MouseSizeLeft = 2 '拉伸左边框

MouseSizeBottom = 3 '拉伸下边框

MouseSizeTop = 4 '拉伸上边框

MouseSizeTopLeft = 5 '拉伸左上角

MouseSizeTopRight = 6 '拉伸右上角

MouseSizeBottomLeft = 7 '拉伸左下角

MouseSizeBottomRight = 8 '拉伸右下角

MouseDrag = 9 '鼠标拖动

End Enum

Dim m_MousePointPosition As EnumMousePointPosition

Dim p, p1 As Point

Private Sub MyMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)

p = New Point(e.X, e.Y) '记录光标开始拖动点

p1 = 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 - p1.Y)

p1 = New Point(e.X, e.Y) '记录光标拖动的当前点

Case EnumMousePointPosition.MouseSizeBottomRight

sender.Size = New Size(sender.Width + e.X - p1.X, sender.Height + e.Y - p1.Y)

p1 = New Point(e.X, e.Y)

Case EnumMousePointPosition.MouseSizeRight

sender.Size = New Size(sender.Width + e.X - p1.X, sender.Height)

p1 = 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 - p1.Y)

p1 = 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 - p1.X), sender.Height - (e.Y - p.Y))

p1 = 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 '改变光标

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

End If

End Sub

Private Function MousePointPosition(ByVal Size As Size, ByVal e As System.Windows.Forms.MouseEventArgs) As EnumMousePointPosition

Const Band = 10

If e.X >= -1 * Band And e.X <= Size.Width And e.Y >= -1 * Band And e.Y <= Size.Height Then

If e.X < Band Then

If e.Y < Band Then

Return EnumMousePointPosition.MouseSizeTopLeft

ElseIf e.Y > -1 * Band + Size.Height Then

Return EnumMousePointPosition.MouseSizeBottomLeft

Else

Return EnumMousePointPosition.MouseSizeLeft

End If

ElseIf e.X > -1 * Band + Size.Width Then

If e.Y < Band Then

Return EnumMousePointPosition.MouseSizeTopRight

ElseIf e.Y > -1 * Band + Size.Height Then

Return EnumMousePointPosition.MouseSizeBottomRight

Else

Return EnumMousePointPosition.MouseSizeRight

End If

Else

If e.Y < Band Then

Return EnumMousePointPosition.MouseSizeTop

ElseIf e.Y > -1 * Band + Size.Height Then

Return EnumMousePointPosition.MouseSizeBottom

Else

Return EnumMousePointPosition.MouseDrag

End If

End If

Else

Return EnumMousePointPosition.MouseSizeNone

End If

End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim Button As New Button

Controls.Add(Button)

AddHandler Button.MouseDown, AddressOf MyMouseDown

AddHandler Button.MouseMove, AddressOf MyMouseMove

AddHandler Button.MouseLeave, AddressOf MyMouseLeave

End Sub

End Class

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有