Date: 02-16-2022
Return to Index
created by gbSnippets
'Converts, or maps, a set of points from a coordinate space relative
'to one Window to a coordinate space relative to another Window.
'It's versatile, in that it can do window-to-window, screen-to-window
'and window-to-screen conversions. It can also do multiple points at a
'time by passing an array of POINT structures
'Compiler Comments:
'This code was written to compilete in PBWin10. To compile with PBWin9, use pt
'in place of VarPtr(pt) in the MapWindowPoints API (two places)
'Primary Code:
Local pt(5) as Point
MapWindowPoints hDlgFrom, hDlgTo, pt(0), UBound(pt) 'window to window
MapWindowPoints hDlgFrom, %NULL, pt(0), UBound(pt) 'window to screen
MapWindowPoints hDlgFrom, %HWND_DESKTOP, pt(0), UBound(pt) 'window to screen
MapWindowPoints %NULL, hDlgTo, pt(0), UBound(pt) 'screen to to window
'Compilable Example: (Jose Includes)
'This example puts the screen coordinates in a label as the mouse is moved.
'Note that %WM_MouseMove is not received when the cursor is away from
'the dialog, so the coordinates change only when the mouse is over the dialog.
'Also, WM_MouseMove is not received by the Dialog when the cursor is over
'most controls, since most control window procedures handle mouse events.
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg 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
Control Add Option, hDlg, 200,"Screen To Client", 20,40,120,20
Control Add Option, hDlg, 201,"Client To Screen", 20,70,120,20
Control Set Option hDlg, 200, 200, 201
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Select Case CB.Msg
Case %WM_MouseMove 'returns client coordinates
Local pt As Point, iResult&
Control Get Check hDlg, 200 To iResult&
If iResult& Then
GetCursorPos pt 'screen coordinates
MapWindowPoints %NULL, hDlg, ByVal VarPtr(pt), 1 'screen to client
Else
pt.x = Lo(Word,cb.lParam) 'client coordinates
pt.y = Hi(Word,cb.lParam) 'client coordinates
MapWindowPoints hDlg, %NULL, ByVal VarPtr(pt), 1 'client to screen
End If
Control Set Text hDlg, 100, "X:Y " + Str$(pt.x) + ":" + Str$(pt.y)
End Select
End Function
'gbs_00028
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm