?
Solved

Absolute mouse position

Posted on 2007-11-15
4
Medium Priority
?
1,160 Views
Last Modified: 2013-11-28
Hi experts,

How can I find/calculate the absolute mouse position?

I have a subform with data in "continuous forms". On each detail line there is a button that opens a small form with options, and I would like to open that form next to the current mouse position.

DoCmd.MoveSize allows me to position that form, but requires absolute co-ordinates (within the Access window).  The OnMouseDown event gives me only the relative mouse position (within the control). Given the fact that the control is in a continuos form, I don't even know the absolute position of the control, so I'm stuck.

Thanks,
Michiel
0
Comment
Question by:altiplano
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 

Author Comment

by:altiplano
ID: 20288653
OK, I found the answer myself: http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q152969
regards,
Michiel
0
 
LVL 16

Accepted Solution

by:
Rick_Rickards earned 750 total points
ID: 20291218
Yes, but a couple API calls are in order.  

To get your mouse position you’ll need to create a custom data type that will hold two long integers, one for the X coordinate, and one for the Y coordinate.  For example, create a module named basMouse and insert the following code…

Option Compare Database
Option Explicit

Type TypeCoordXY
    X As Long
    Y As Long
End Type

Declare Function GetCursorPos Lib "user32" (lpPoint As TypeCoordXY) As Long

So to get your Mouse X & Y Coordinates you would run code like this…

Sub MousePos()
    Dim typXY As TypeCoordXY
    GetCursorPos typXY
    Debug.Print typXY.x & "= X Possition"
    Debug.Print typXY.Y; " = Y Possition"
End Sub

One thing to be clear about is that the mouse position is returned in terms of pixels and when positioning forms you need to provide the location in terms of twips.  Thus, having the mouse position isn’t enough; you’ll need to be able to convert the pixels to twips.

This can be done by creating a module such as basPixelsAndTwips and insert the following code…
Private Declare Function apiGetDC Lib "User32" Alias "GetDC" (ByVal hwnd As Long) As Long
Private Declare Function apiGetDeviceCaps Lib "gdi32" Alias "GetDeviceCaps" (ByVal hdc As Long, ByVal nIndex As Long) As Long
Private Declare Function apiReleaseDC Lib "User32" Alias "ReleaseDC" (ByVal hwnd As Long, ByVal hdc As Long) As Long

Private Const TwipsPerInch = 1440

Public Sub ConvertPixelsToTwips(X As Long, Y As Long)
    '**********************************************************************************************
    '** PURPOSE: Converts the two pixel measurements passed as arguments to twips.               **
    '** ARGUMENTS: X and Y Measurement variables in pixels.                                      **
    '** These will be converted to twips and returned through the same variables "by reference." **
    '**********************************************************************************************

    Dim hdc As Long
    Dim hwnd As Long
    Dim RetVal As Long
   
    Dim XPixelsPerInch As Variant
    Dim YPixelsPerInch As Variant
   
    Const LogPixelsX = 88
    Const LogPixelsY = 90

    ' Retrieve the current number of pixels per inch, which is resolution-dependent.
   hdc = apiGetDC(0)
   XPixelsPerInch = apiGetDeviceCaps(hdc, LogPixelsX)
   YPixelsPerInch = apiGetDeviceCaps(hdc, LogPixelsY)
   RetVal = apiReleaseDC(0, hdc)

   ' Compute and return the measurements in twips.
   X = (X / XPixelsPerInch) * TwipsPerInch
   Y = (Y / YPixelsPerInch) * TwipsPerInch
End Sub

‘************************************
Once done you could then get your mouse position and then convert it to twips as follows.

Sub MousePos()
    Dim typXY As TypeCoordXY
    GetCursorPos typXY
    Debug.Print typXY.X & "= X Possition in terms of Pixels"
    Debug.Print typXY.Y; " = Y Possition in terms of Pixels"
    ConvertPixelsToTwips typXY.X, typXY.Y
    Debug.Print typXY.X & "= X Possition in terms of Twips"
    Debug.Print typXY.Y; " = Y Possition in terms of Twips"
End Sub
0
 

Author Comment

by:altiplano
ID: 20304157
Hi Rick,
Thanks for adding to this item. I actually figured that out myself as well, but it's good to have the full answer written out for future reference.
regards,
Michiel
0
 
LVL 16

Expert Comment

by:Rick_Rickards
ID: 20304193
You're welcome.

Rick
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question