Solved

GetCurrentPositionEx - Easy

Posted on 2000-02-17
8
349 Views
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?

Cheers

Simon

0
Comment
Question by:simonbennett
  • 5
  • 2
8 Comments
 
LVL 2

Expert Comment

by:johnny6
ID: 2531067
simonbennet:
      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.

John
0
 
LVL 15

Expert Comment

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

Author Comment

by:simonbennett
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?

Cheers

Simon
0
 
LVL 15

Expert Comment

by:ameba
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
    Cls
   
    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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 15

Expert Comment

by:ameba
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
    Else
        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
0
 
LVL 15

Accepted Solution

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

Author Comment

by:simonbennett
ID: 2531862
Ameba,

Due respect and thanks - to the pub!

Simon
0
 
LVL 15

Expert Comment

by:ameba
ID: 2532028
:) Thanks for the points.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

867 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

22 Experts available now in Live!

Get 1:1 Help Now