GetCurrentPositionEx - Easy

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?



Who is Participating?

Improve company productivity with a Business Account.Sign Up

amebaConnect With a Mentor Commented:
Little typo in GetRect
"tmppt" should be "pt"
      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.

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

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

simonbennettAuthor Commented:
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?


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
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
simonbennettAuthor Commented:

Due respect and thanks - to the pub!

:) Thanks for the points.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.