?
Solved

GetCurrentPositionEx - Easy

Posted on 2000-02-17
8
Medium Priority
?
381 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
[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
  • 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
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

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
 
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 400 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…
Suggested Courses

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