Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitlePosition a form under the mouse
Keywordsposition, form, mouse
CategoriesTips and Tricks, API
 
Use GetCursorPos and ScreenToClient to see where the mouse is in form coordinates. Then move the form to the appropriate position.
 
' Center the form at the mouse.
Private Sub PositionFormOverMouse(ByVal frm As Form, ByVal _
    target_x As Single, ByVal target_y As Single)
    ' Get the current mouse position.
    GetCursorPos pt

    ' Convert into form client area coordinates.
    ScreenToClient frm.hwnd, pt

    ' Convert to twips.
    mouse_x = frm.ScaleX(pt.x, vbPixels, vbTwips)
    mouse_y = frm.ScaleY(pt.y, vbPixels, vbTwips)

    ' Center the form at this position.
    frm.Left = frm.Left + mouse_x - target_x
    frm.Top = frm.Top + mouse_y - target_y
End Sub

Private Sub Form_Click()
    PositionFormOverMouse Me, ScaleWidth / 2, ScaleHeight / _
        2
End Sub
 
Formatted by Neil Crosby
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated