ChildWindowFromPoint

Category: API Functions

Date: 02-16-2022

Return to Index


 
'Sometimes you need to know which child window is at a point, most often
'associated with the position of the cursor. The WindowFromPoint function
'does the trick.
 
'Compiler Comments:
'This code was written to compilete in PBWin10. To compile with PBWin9, split pt
'into pt.x and pt.y as arguments wherever the ChildWindowFromPoint() API is used.
 
'Primary Code:
pt.x = 500 : pt.y = 500
hDlg = ChildWindowFromPoint( hDlg, pt )
 
 
'Compilable Example:  (Jose Includes)
'This example displays the control under the cursor
#Compiler PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg, h100, h200, h300, h400 as Dword
 
Function PBMain() As Long
   Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg
   Control Add Label, hDlg, 100,"", 50,10,100,20, %WS_Border
   Control Add Label, hDlg, 200,"", 50,40,100,20, %WS_Border
   Control Add Label, hDlg, 300,"", 50,70,100,20, %WS_Border
   Control Add Label, hDlg, 400,"", 50,100,100,20, %WS_Border
   Control Handle hDlg, 100 To h100 : Control Handle hDlg, 200 To h200
   Control Handle hDlg, 300 To h300 : Control Handle hDlg, 400 To h400
   Dialog Show Modal hDlg Call DlgProc
End Function
 
CallBack Function DlgProc() As Long
   Select Case CB.Msg
      Case %WM_SetCursor
         Local pt as Point, hTemp as Dword
         GetCursorPos pt    'p.x and p.y are in screen coordinates
         ScreenToClient hDlg, pt  'p.x and p.y are now dialog client coordinates
         Control Set Text hDlg, 200, "X:Y " + Str$(pt.x) + ":" + Str$(pt.y)
         hTemp = ChildWindowFromPoint( hDlg, pt )
         Select Case hTemp
            Case h100 : Control Set Text hDlg, 100, "Label 100"
            Case h200 : Control Set Text hDlg, 100, "Label 200"
            Case h300 : Control Set Text hDlg, 100, "Label 300"
            Case h400 : Control Set Text hDlg, 100, "Label 400"
            Case Else  : Control Set Text hDlg, 100, "No Match"
         End Select
   End Select
End Function
 
'gbs_00019
'Date: 03-10-2012


created by gbSnippets
http://www.garybeene.com/sw/gbsnippets.htm