[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Absolute mouse position

Posted on 2007-11-15
4
Medium Priority
?
1,167 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

650 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