to Wuxyingshu(无影石)
其实是我错了,我以为他写的代码跟我一样。后来我修改了那个错误就行了。
但是他的方法比较罗嗦,我给出一个较简洁的方法:
Option Explicit
Dim bgnX As Single, bgnY As Single
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbLeftButton Then
bgnX = X
bgnY = Y
End If
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbLeftButton Then
Me.Move Me.Left + X - bgnX, Me.Top + Y - bgnY
End If
End Sub
比如窗体名字为frmMain,你允许用户拖动picTitleBar来拖动窗体
'定义变量
Dim MoveFrm As Boolean
Dim oldleft As Integer
Dim oldtop As Integer
Dim oldx As Integer
Dim oldy As Integer
'鼠标按下时
Private Sub picTitlebar_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
MoveFrm = True
oldleft = frmMain.Left
oldtop = frmMain.Top
oldx = X + frmMain.Left
oldy = Y + frmMain.Top
End If
End Sub
'鼠标移动时
Private Sub picTitlebar_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 And MoveFrm = True Then
Dim thisx As Integer, thisy As Integer
thisx = X + frmMain.Left
thisy = Y + frmMain.Top
frmMain.Left = oldleft + thisx - oldx
frmMain.Top = oldtop + thisy - oldy
End If
End Sub
'鼠标松开时
Private Sub picTitlebar_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
MoveFrm = False
End If
End Sub