窗体上放一个TextBox,Name叫Text1,参看如下代码:Option ExplicitPrivate mouse_old_x As Single, mouse_old_y As Single Private flag_on_drag As BooleanPrivate Sub Form_Load() flag_on_drag = False
End SubPrivate Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) If (vbLeftButton = Button) Then mouse_old_x = X mouse_old_y = Y
flag_on_drag = True End If
End SubPrivate Sub Text1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) If True = flag_on_drag Then Text1.Left = Text1.Left + (X - mouse_old_x) Text1.Top = Text1.Top + (Y - mouse_old_y) End If
End SubPrivate Sub Text1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) If (vbLeftButton = Button) Then flag_on_drag = False End If
End Sub
使用SendMessage函数来发送移动消息是效果最佳的,没有停顿现象Option ExplicitPrivate Const HWND_TOPMOST = -1 Private Const SWP_NOSIZE = &H1 Private Const WM_SYSCOMMAND = &H112& Private Const SC_MOVE = &HF012& Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long 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 Any) As LongPrivate Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = vbLeftButton Then 'Ϊµ±Ç°µÄÓ¦ÓóÌÐòÊÍ·ÅÊó±ê²¶»ñ ReleaseCapture 'Òƶ¯´°Ìå SendMessage Text1.hwnd, WM_SYSCOMMAND, SC_MOVE, 0 End If End Sub
2楼的在我的环境下不用按按钮只要鼠标移动TextBox也跟着跑了 我的环境如下: WIn7 Ultimate 64-bit VB 6.0 with SP6
Private flag_on_drag As BooleanPrivate Sub Form_Load()
flag_on_drag = False
End SubPrivate Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If (vbLeftButton = Button) Then
mouse_old_x = X
mouse_old_y = Y
flag_on_drag = True
End If
End SubPrivate Sub Text1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If True = flag_on_drag Then
Text1.Left = Text1.Left + (X - mouse_old_x)
Text1.Top = Text1.Top + (Y - mouse_old_y)
End If
End SubPrivate Sub Text1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If (vbLeftButton = Button) Then
flag_on_drag = False
End If
End Sub
Private Const SWP_NOSIZE = &H1
Private Const WM_SYSCOMMAND = &H112&
Private Const SC_MOVE = &HF012&
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
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 Any) As LongPrivate Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbLeftButton Then
'Ϊµ±Ç°µÄÓ¦ÓóÌÐòÊÍ·ÅÊó±ê²¶»ñ
ReleaseCapture
'Òƶ¯´°Ìå
SendMessage Text1.hwnd, WM_SYSCOMMAND, SC_MOVE, 0
End If
End Sub
我的环境如下:
WIn7 Ultimate 64-bit
VB 6.0 with SP6
你那个也不错,API很正统,标准做法!