在一个空白窗体上点击鼠标 在点击位置弹出另外一个窗体 

解决方案 »

  1.   


    Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Form2.Top = Y + Me.Top
        Form2.Left = X + Me.Left
        Form2.Show
    End Sub
      

  2.   


    Option ExplicitPrivate Type POINTAPI
            x As Long
            y As Long
    End Type
    Private Type RECT
            Left As Long
            Top As Long
            Right As Long
            Bottom As Long
    End Type
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    Private Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long
    Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As LongPrivate Sub Form_Click()
        Dim ptCursor As POINTAPI
        Dim rcWindow As RECT
        
        GetCursorPos ptCursor
        Load Form2
        Form2.ScaleMode = vbPixels
        GetWindowRect Form2.hwnd, rcWindow
        MoveWindow Form2.hwnd, ptCursor.x, ptCursor.y, rcWindow.Right - rcWindow.Left, rcWindow.Bottom - rcWindow.Top, 1
        Form2.Show , Me
    End Sub
      

  3.   

    Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Form2.Top = Y + Me.Top
        Form2.Left = X + Me.Left
        Form2.Show
    End Sub
      

  4.   

    由于VB本身获得的宽度和高度不包括窗口边框和标题栏,所以使用了API。
      

  5.   

    Ding lyserver不过我觉得没必要用 MoveWindow() 这个 API ,直接调用 Form2.Move 就可以了。
    Private Sub Form_Click()
        Dim ptCursor As POINTAPI
        Dim rcWindow As RECT
        
        GetCursorPos ptCursor
        Load Form2
        Form2.ScaleMode = vbPixels
        GetWindowRect Form2.hwnd, rcWindow
        'MoveWindow Form2.hwnd, ptCursor.x, ptCursor.y, rcWindow.Right - rcWindow.Left, rcWindow.Bottom - rcWindow.Top, 1
        Form2.Move ptCursor.X * 15, ptCursor.Y * 15
        Form2.Show , Me
    End Sub
      

  6.   

    不好意思~~~~~
    用这样的代码就行了, API函数: MoveWindow()、GetWindowRect() 都不必用。Private Sub Form_Click()
        Dim ptCursor As POINTAPI
        Dim rcWindow As RECT
        
        GetCursorPos ptCursor
        Load Form2
        Form2.Move ptCursor.X * 15, ptCursor.Y * 15
        Form2.Show , Me
    End Sub