如何做到拖动窗体窗体就跟着鼠标移动

 我来答
cwa9958
2008-02-01 · TA获得超过1885个赞
知道大有可为答主
回答量:2504
采纳率:0%
帮助的人:2018万
展开全部
简单,不要用api吧

Dim x0 As Long, y0 As Long '记录按下鼠标的位置

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
x0 = X '记录位置
y0 = Y
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then '按下鼠标左键
Me.Left = Me.Left + X - x0
Me.Top = Me.Top + Y - y0
End If
End Sub
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
fanhappy123
2008-02-01 · TA获得超过514个赞
知道小有建树答主
回答量:380
采纳率:0%
帮助的人:581万
展开全部
'把以下代码加入就可以了

Const HTCAPTION = 2
Const WM_NCLBUTTONDOWN = &HA1

Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, Y As Single)
If Button = 1 Then
Dim ReturnVal As Long
x = ReleaseCapture()
ReturnVal = SendMessage(hWnd, WM_NCLBUTTONDOWN, HTCAPTION, 0)
End If
End Sub
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
lyglay
2008-02-01 · TA获得超过1137个赞
知道大有可为答主
回答量:975
采纳率:0%
帮助的人:0
展开全部
你也可以不用API来实现

Dim PrevX As Long, PrevY As Long

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
PrevX = X: PrevY = Y
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then Me.Move Me.Left + X - PrevX, Me.Top + Y - PrevY
End Sub
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式