The following code will work in both 32-bit and 64-bit:
The s_rect structure:
global type s_rect from structure
long l_left descriptor "comment" = "X-coordinate of upper-left corner of rectangle (can be negative)"
long l_top descriptor "comment" = "Y-coordinate of upper-left corner of rectangle (can be negative)"
long l_right descriptor "comment" = "X-coordinate of lower-right corner of rectangle (can be negative)"
long l_bottom descriptor "comment" = "Y-coordinate of lower-right corner of rectangle (can be negative)"
end type
The s_monitorinfo structure:
global type s_monitorinfo from structure
unsignedlong ul_structsize descriptor "comment" = "The size of this structure, in bytes (always 40)"
s_rect str_monitor descriptor "comment" = "The display monitor rectangle, in virtual screen coordinates"
s_rect str_workarea descriptor "comment" = "The work area rectangle of the display monitor, in virtual screen coordinates"
unsignedlong ul_flags descriptor "comment" = "Bit flags. Bit 1 (xxxx xxx1) = 1 when this is the primary monitor"
end type
External function declarations:
FUNCTION Longptr MonitorFromWindow ( &
Longptr hWnd, &
UnsignedLong dwFlags &
) LIBRARY "user32.dll"
FUNCTION Boolean GetMonitorInfo ( &
Longptr hMonitor , &
REF s_monitorinfo str_MonitorInfo ) &
LIBRARY "user32.dll" ALIAS FOR "GetMonitorInfoW"
Code that calls the MonitorFromWindow and GetMonitorInfo API functions:
Constant UnsignedLong MONITOR_DEFAULTTONULL = 0
Constant UnsignedLong MONITOR_DEFAULTTOPRIMARY = 1
Constant UnsignedLong MONITOR_DEFAULTTONEAREST = 2
Boolean lb_rc
Longptr llptr_hwnd, llptr_monitor
Long ll_monitor_x, ll_monitor_y, &
ll_monitor_right, ll_monitor_bottom, &
ll_monitor_width, ll_monitor_height
s_monitorinfo lstr_mi
llptr_hwnd = Handle(Parent)
llptr_monitor = MonitorFromWindow(llptr_hwnd,MONITOR_DEFAULTTONEAREST)
lstr_mi.ul_structsize = 40 // size of s_monitor structure in memory: 4 + (4+4+4+4) + (4+4+4+4) + 4
lb_rc = GetMonitorInfo(llptr_monitor,lstr_mi)
ll_monitor_x = lstr_mi.str_monitor.l_left
ll_monitor_y = lstr_mi.str_monitor.l_top
ll_monitor_right = lstr_mi.str_monitor.l_right
ll_monitor_bottom = lstr_mi.str_monitor.l_bottom
ll_monitor_width = ll_monitor_right - ll_monitor_x
ll_monitor_height = ll_monitor_bottom - ll_monitor_y
MessageBox('Monitor Information', 'Position = ('+String(ll_monitor_x)+','+ &
String(ll_monitor_y)+')~r~n~r~n'+ &
'Width = '+String(ll_monitor_width)+'~r~n' + &
'Height = '+String(ll_monitor_height))
Sample result:
Potentially, yes, that MIGHT NOT work in 64-bit, as all Windows handles are 64-bit integers in a 64-bit process. True, I've never seen a 64-bit Windows handle that would not fit entirely in a 32-bit integer, but there is no guarantee that will always be the case.
Since there is no Longptr(string) conversion function in PowerScript, you would need to ensure you are saving/retrieving a 64-bit integer into the Registry as a numeric value when the app runs as 64-bit, or use the LongLong(string) PowerScript function if you choose to save the monitor handle as a string in the Registry.. Rather than go to all the trouble and add bitness-dependent code to your app, I suggest you always perform the MonitorFromWindow API function, as it is very efficient and will always return the monitor handle in the proper bitness as long as you use the Longptr (pseudo-)datatype in 64-bit. Using the Handle(window) PowerScript function is also very efficient.
Something to consider.
That was really helpful. Thank you :-)