Results 1 to 5 of 5

Thread: how to Pass a Point Structure to the Windows API by Value

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2000
    Location
    New Delhi, India
    Posts
    75
    I am trying to use ChildWindowFromPoint API function. I dont know how to pass POINT structure to the function. can anybody help me.

    thanks and regards

  2. #2
    Lively Member
    Join Date
    Jan 2000
    Posts
    102
    look what i found on msdn.
    itay.

    How to Pass a Point Structure to the Windows API by Value

    --------------------------------------------------------------------------------
    The information in this article applies to:

    Microsoft Visual Basic Standard, Professional, and Enterprise Editions, 16-bit and 32-bit, for Windows, version 4.0

    --------------------------------------------------------------------------------


    SUMMARY
    The WindowFromPoint and ChildWindowFromPoint API functions require that a Point structure be passed by value. Visual Basic is not capable of passing a structure by value, only by reference. However, it is possible to pass the elements of the user-defined type in the correct order so that these API functions can be used. This article explains and demonstrates how to pass the Point structure by value to the API in the 16-bit and 32-bit editions of Visual Basic.



    MORE INFORMATION
    The Windows 16-bit API is unusal because it allows you to pass the Point structure to the WindowFromPoint functions by value. Typically, user- defined types are passed to functions by reference because passing them by value can use up a lot of stack space and be much slower than passing the address of the structure. An exception was made for the Point structure in certain functions (such as ChildWindowFromPoint and WindowFromPoint) because in 16-bit programming the size of the structure was only 32 bits long, which is the same length as a far address anyway.

    However, on a 32-bit operating system, this structure becomes 64 bits. To preserve compatibility, Microsoft decided that the Point structure should still be passed by value for these two API functions in the 32-bit user dynamic link library (DLL).

    Visual Basic is not capable of passing structures by value, but the value of the elements of the structure can be placed on the stack in the correct order. Therefore, when passing the elements of the Point structure in 32-bit programming, two long values should be passed. When passing the elements in 16-bit programming, two integers should be passed to the function.

    Another important consideration is that 32-bit Visual Basic uses the C convention (stdcall) of passing parameters. This convention specifies that arguments are placed on the stack from right to left. 16-Bit Visual Basic maintains the Pascal convention of passing parameters from left to right. (API functions are declared using the Pascal calling convention.) As a result, the elements of the structure must be listed in reverse order (that is, element y followed by x) when calling the WindowFromPoint function using 32-Bit Visual Basic. When using 16-bit Visual Basic, element x is passed to the API function before element y.

    Step-by-Step Example of How to Call the WindowFromPoint API
    The code in the following example uses conditional compilation so that the code can be placed in either the 16-bit or 32-bit version of Visual Basic.


    Start a new project in Visual Basic. Form1 is created by default.


    Change the ScaleMode of the form to 3 - Pixels. (The coordinates of the Point must be passed in pixels.)


    Insert a new code module in the project (on the Insert menu, click New Module). Place the following code in the new module:

    #If Win32 Then
    Declare Function WindowFromPointXY Lib "User32" _
    Alias "WindowFromPoint" (ByVal xPoint As Long, _
    ByVal yPoint As Long) As Long
    #Else
    Declare Function WindowFromPointYX Lib "User" _
    Alias "WindowFromPoint" (ByVal yPoint As Integer, ByVal _
    xPoint As Integer) As Integer
    #End If

    Function VBWindowFromPoint(ByVal x As Long, ByVal y As Long) As Long
    #If Win32 Then
    VBWindowFromPoint = WindowFromPointXY(x, y)
    #Else
    VBWindowFromPoint = WindowFromPointYX(y, x)
    #End If
    End Function



    Place the following code in the MouseDown event of Form1:

    ' Convert form coordinates to screen coordinates
    screenX = X + Form1.Left / Screen.TwipsPerPixelX
    screenY = Y + Form1.Top / Screen.TwipsPerPixelY

    ' Retrieve the window handle
    hWindow = VBWindowFromPoint(screenX, screenY)

    If hWindow = Form1.hWnd Then
    MsgBox "Success"
    End If



    Run the code by pressing the F5 key. Click Form1. The message box should appear indicating that the API call successfully returned the window handle of Form1.



    Additional query words: 4.00 vb4win vb4all

    Keywords :
    Version :
    Platform :
    Issue type :
    Technology :


    Last Reviewed: September 17, 1999
    © 2000 Microsoft Corporation. All rights reserved. Terms of Use.


  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2000
    Location
    New Delhi, India
    Posts
    75
    Even i had also seen it before posting this question. The problem is that the example shows the usage of WindowFromPointXY and does not actuall tell ANYTHING about ChildWindowFromPoint except in the first para. I need a practical example if possible.

  4. #4
    Lively Member
    Join Date
    Jan 2000
    Posts
    102
    from what i understand in the example,
    you need to replace:
    instead of one paramter of type POINT,
    you use two longs (if that's win32 system).

    so, in your case, if the api declaration of
    ChildWindoFromPoint is:
    HWND ChildWindowFromPoint(
    HWND hWndParent, // handle to parent window
    POINT Point // structure with point coordinates
    );

    then you declare it like:
    Code:
    Declare Function ChildWindowFromPointXY Lib "User32" _
           Alias "ChildWindowFromPoint" (hWnd as long, ByVal xPoint As Long, ByVal yPoint As Long) As Long
    and call it like
    Code:
    ChildWindowFromPointXY (hparent, x, y)

    i didn't test it, but you are welcomed, until you
    find a better sample.

    itay.


    [Edited by itay222 on 01-04-2001 at 04:44 AM]

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jul 2000
    Location
    New Delhi, India
    Posts
    75

    Question

    thanks for that answer. Even though the program exectued the result is not correct. Please go through the following code. I have created a picture box (picturebox1) and put two picture boxes into it as children (picturebox2 and picturebox 3). both message boxes return zero instead of long integer. what may be the problem

    Private Declare Function GetCursorPos Lib "User32" (lpPoint As POINTAPI) As Long
    'Private Declare Function ChildWindowFromPoint Lib "User32" (ByVal hWndParent As Long, ByVal pt As POINTAPI) As Long

    Private Declare Function ChildWindowFromPointXY Lib "User32" _
    Alias "ChildWindowFromPoint" (hWnd As Long, ByVal xPoint As Long, ByVal yPoint As Long) As Long


    Private Sub Picture2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim P As POINTAPI
    GetCursorPos P
    MsgBox ChildWindowFromPointXY(Picture1.hWnd, P.X, P.Y)
    End Sub

    Private Sub Picture3_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim P As POINTAPI
    GetCursorPos P
    MsgBox ChildWindowFromPointXY(Picture1.hWnd, P.X, P.Y)
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width