GetCurrentPositionEx - Easy

Posted on 2000-02-17
Last Modified: 2008-02-26
Hello all,

Anybody had problems with this?

When I use

chk = UserControl.hdc
'or chk = UserControl.parent.hdc

lngResult = GetCurrentPositionEx(chk, CurrPossControl)

lngresult is 1 (ok) but CurrPossControl.x and y are always 0?

All ideas appreciated?



Question by:simonbennett
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
  • 5
  • 2

Expert Comment

ID: 2531067
      I assume you are declaring your function as follows:

Private Declare Function GetCurrentPositionEx Lib "gdi32" Alias "GetCurrentPositionEx" (ByVal hdc As Long, lpPoint As POINTAPI) As Long

Private Type POINTAPI
        x As Long
        y As Long
End Type

Return value--Nonzero on success, zero on failure.

LVL 15

Expert Comment

ID: 2531073
(0, 0) is default for CurrentPosition.
Are you sure you are using some method (MoveTo, LineTo, .Print) that will change CurrentX/CurrentY?

Author Comment

ID: 2531268
Hi guys -

I guessed it would return the current position of the window relative to 'everything'.

For more info - I am using GetCursorPos to find the position of the mouse - I need to know if the mouse is over or not a usercontrol...

Any ideas?


Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 15

Expert Comment

ID: 2531551
Use ClientToScreen api to convert coordinates:

' add 1 command button
Option Explicit
'Windows 32 API
Private Type POINTAPI
    X As Long
    Y As Long
End Type
Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long

Private Sub Form_Click()
    Dim pt1 As POINTAPI
    pt1.X = 0
    pt1.Y = 0
    'point pt1 is topleft point of the command1 control
    ' convert to screen coordinate
    Call ClientToScreen(Me.Command1.hwnd, pt1)
    Print "Command1: " & pt1.X & ", " & pt1.Y
    pt1.X = 0
    pt1.Y = 0
    'point pt1 is topleft point of the FORM client area
    ' convert to screen coordinate
    Call ClientToScreen(Me.hwnd, pt1)
    Print "Form: " & pt1.X & ", " & pt1.Y
End Sub
LVL 15

Expert Comment

ID: 2531726
Here is my sample for "MouseIsOver(ctl)". It also works for windowless controls!

Option Explicit
' add label control and timer
Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type
Private Type POINTAPI
    X As Long
    Y As Long
End Type
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function ClientToScreen Lib "user32" _
    (ByVal hWnd As Long, lpPoint As POINTAPI) As Long
Private Declare Function PtInRect Lib "user32" (lpRect As RECT, _
    ByVal ptx As Long, ByVal pty As Long) As Long

Private Sub Timer1_Timer()
    Dim bld As Boolean, clr As ColorConstants
    ' now check if cursor is over control
    If MouseIsOver(Label1) Then
        bld = True
        clr = vbRed
        bld = False
        clr = vbBlack
    End If
    Label1.FontBold = bld
    Label1.ForeColor = clr
End Sub

Private Sub Form_Load()
    Timer1.Interval = 200
    ScaleMode = vbTwips
End Sub

Private Function MouseIsOver(ctl As Control) As Boolean
    Dim pt As POINTAPI, rc As RECT
    GetCursorPos pt         ' get cursor position
    rc = GetRect(Label1)    ' control rectangle
    MouseIsOver = PtInRect(rc, pt.X, pt.Y)
End Function

Private Function GetRect(ctl As Control) As RECT
' we cannot use API GetWindowRect directly, light controls don't have hwnd
' this works also for non-lite controls
' ScaleMode must be vbTwips!
    Dim px As Integer, py As Integer
    Dim ret As Long, pt As POINTAPI
    Dim objParent As Object ' form or control
    On Error Resume Next
    px = Screen.TwipsPerPixelX
    py = Screen.TwipsPerPixelY
    Set objParent = ctl.Container ' form or container control
    ' convert point (0,0) to screen coordinate
    ret = ClientToScreen(objParent.hWnd, tmppt)
    ' tmppt contains screen coordinates of client point 0,0
    ' to get rectangle, use Ctl.left, .top, .width and .height properties
    GetRect.Left = pt.X + ctl.Left / px
    GetRect.Right = GetRect.Left + ctl.Width / px
    GetRect.Top = pt.Y + ctl.Top / py
    GetRect.Bottom = GetRect.Top + ctl.Height / py
End Function
LVL 15

Accepted Solution

ameba earned 100 total points
ID: 2531748
Little typo in GetRect
"tmppt" should be "pt"

Author Comment

ID: 2531862

Due respect and thanks - to the pub!

LVL 15

Expert Comment

ID: 2532028
:) Thanks for the points.

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

717 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