Solved

Absolute mouse position

Posted on 2007-11-15
4
1,142 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
  • 2
  • 2
4 Comments
 

Author Comment

by:altiplano
Comment Utility
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 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
You're welcome.

Rick
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now