vb中怎样拖动控件

 我来答
mafan8
2012-11-01 · TA获得超过1.7万个赞
知道小有建树答主
回答量:927
采纳率:0%
帮助的人:768万
展开全部
‘拖动command1的例子
Option Explicit
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function ScreenToClient Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
Private Type POINTAPI
x1 As Long
y1 As Long
End Type
Dim cmdX1 As Integer
Dim cmdY1 As Integer
Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim P As POINTAPI
If Button = vbLeftButton Then
GetCursorPos P '获取鼠标在屏幕中的位置
ScreenToClient Me.hwnd, P '将现有坐标0,0转换为本窗体的坐标
cmdX1 = Command1.Left - P.x1 * Screen.TwipsPerPixelX
cmdY1 = Command1.Top - P.y1 * Screen.TwipsPerPixelY
End If
End Sub
Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim P As POINTAPI
Dim t As Boolean
If Button = vbLeftButton Then
GetCursorPos P '获取鼠标在屏幕中的位置
ScreenToClient Me.hwnd, P '将现有坐标0,0转换为本窗体的坐标
t = P.x1 >= 0 And P.y1 >= 0 And P.x1 < Me.Width / Screen.TwipsPerPixelX And P.y1 <= Me.Height / Screen.TwipsPerPixelY
If t Then
Command1.Left = P.x1 * Screen.TwipsPerPixelX + cmdX1
Command1.Top = P.y1 * Screen.TwipsPerPixelY + cmdY1
End If
End If
End Sub
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式