Solved

GetCurrentPositionEx - Easy

Posted on 2000-02-17
8
348 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…

747 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

12 Experts available now in Live!

Get 1:1 Help Now