当前位置:编程学习 > VB >>

vb中怎么使form的style改为0还能移动

追问:第一个我试了不行啊!说“实时错误424,要求对象”

答案:Public MouseDown As Boolean, Tmpx As Integer, Tmpy As Integer
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
MouseDown = True
Tmpx = X: Tmpy = Y
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If MouseDown Then
  frmsecond.Top = frmsecond.Top + (Y - Tmpy)
  frmsecond.Left = frmsecond.Left + (X - Tmpx)
End If
End Sub

Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
MouseDown = False
End Sub

还有Api方式的

Option Explicit
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function ReleaseCapture Lib "user32" () As Long
 
 Const WM_NCLBUTTONDOWN = &HA1
 Const HTCAPTION = 2
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbLeftButton Then
ReleaseCapture
SendMessage Me.hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&
End If
End Sub

 

一楼的可行,但无需专门用个变量来记录鼠标键是否按下,MouseMove过程本身有个Button参数记录了键的状态,所以可以简化为:

Dim Tmpx As Single, Tmpy As Single

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Tmpx = X: Tmpy = 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 - Tmpx), Me.Top + (Y - Tmpy)
End Sub

 

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 Long
Private Const WM_SYSCOMMAND = &H112
Private Const SC_MOVE = &HF010&
Private Const HTCAPTION = 2
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
ReleaseCapture
SendMessage Me.hWnd, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0
End If
End Sub

上一个:怎样用vb程序访问Access数据库并使其连接?
下一个:vb登录程序,请高手帮忙分析,谢谢!

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,