WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Tutorials
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Screen Coordinates Explained

 Keywords:  Screen Window Pixel Coordinate Absolute Relative Virtual 1000 Dialog Unit Upper Lower Bottom Top Left Right Corner X Y Point


WinBatch has functions that deal with three basic types of screen coordinates:

  1. WinBatch Standard: Virtual 1000 x 1000 Screen
  2. Dialog Units
  3. Screen Pixels

What is a coordinate?

WinBatch specifies the position of a window on the screen in 'screen coordinates'. For screen coordinates, the origin is the upper-left corner of the screen. The full position of a window is often described by a Rectangle structure containing the screen coordinates of two points that define the upper-left and lower-right corners of the window.
[x,y]
[*]---------------------------
|                             |
|                             |
|                             |
|                             |
|                             |
|                             |
 ----------------------------[*]
                            [x,y]
A window coordinate, in WinBatch, is a string variable (actually a list) containing four numbers separated by commas or spaces. These four numbers define two points on the screen. The first number is the "X" coordinate of the first point, the second number is the "Y" coordinate of the first point, the third number is the "X" coordinate of the second point, and finally the fourth number is the "Y" coordinate of the second point.

The "0,0" point is in the upper left of the screen, and the "999,999" point is at the lower right.

With just these two points, WinBatch can size and place a number of items.

WinBatch Standard: Virtual 1000x1000 screen:

Coordinates are based on a virtual 1000 x 1000 screen. "x-upper left corner", "y-upper left corner", "x-bottom right corner", and "y-bottom right corner" are based on a logical screen that is 1000 points wide by 1000 points high.

The benefit of the 1000x1000 screen is that it helps users easily calculate relative screen positions regardless of screen resolution. The drawback is possible loss of precision, caused by screens with with resolutions greater than 1000 pixels to be scaled down to 1000. One way to avoid this is to grab and save the coordinates from the beginning of the script and do all calculations from that initial point. You avoid 'coordinate creep' that way.

Multi-Monitor Systems

All WinBatch functions can deal with multi-monitor systems. 1000x1000 virtual screen size is based on the primary monitor in a multi-monitor display configuration. Basically WinBatch regards the 0-999 as the virtual coordinates of the main monitor and assuming the second monitor is to the right starting at x (horizontal) coordinate of 1000. Also note that WinBatch does not assume that the second monitor is to the right. The second monitor can be to the left, above or below and WinBatch will handle it with monitors to the logical left and above have negative coordinate values. WinBatch can also handle more than 2 monitors.

On multi-monitor systems the 1000 x 1000 virtual screen units may have some counter-intuitive behavior when you have multiple monitors that operate at different resolutions.

For Example:

If you have 3 monitors with the resolution 1600 x 1200, here are the screen coordinates for each monitor:

  1. 0, 0, 999, 999 would be the main monitor.
  2. 1000, 0, 1999, 999 would be the middle monitor.
  3. 2000, 0, 2999, 999 would be the right hand monitor.
However, if the main monitor is 1920 x 1200 instead of 1600 x 1200, but the 2 other monitors remain at 1600 x 1200 and are again set up as the middle and right hand monitors with the display stretched across all 3 monitors, you will get the following:
  1. 0, 0, 999, 999 are the main laptop panel.
  2. 1000, 0, 1832, 999 are the middle monitor.
  3. 1833, 0, 2666, 999 are the right hand monitor.
  4. 2000, 0, 2999, 999 would be the right hand monitor.
Given that 1920 / 1000 is 1.92, and 1600 / 1.92 is 833.33333..., it is obvious that the virtual screen coordinates are scaled based on the resolution of the primary display, and that external displays running at different resolutions are not actually addressable in exact multiples of 1000 screen coordinate units. Again, this is expected behavior because 1000x1000 virtual screen size is based on the primary monitor in a multi-monitor display configuration. Something to keep in mind.

Things to know:

Functions: BoxBitmap, BoxButtonDraw, BoxDrawCircle, BoxDrawLine, BoxDrawRect, BoxDrawText, BoxesUp, BoxmapMode, BoxNew, IntControl 63, MouseCoords, MouseInfo, MouseMove, WinPlace, WinPlaceChild, WinPlaceGet, WinPlaceSet, WinPosition, WinPositionChild

Dialog Units:

Dialog Units are used primarily by the Dialog function. Dialog units are based on the system font, DPI, and screen resolution. System font is defined as the default font defined by Windows. Dialog Units basically speaking: 1 width unit = 1/4 width of system font and 1 height unit = 1/8 height of system font.

Functions: Dialog, IntControl 75, Dialog {UserDefinedCallback}, WinMetrics.

The WinMetrics function in the Windows Interface Language help file can help you figure out what current Dialog unit size is.

	   pixperunit_horz = WinMetrics(-5)
	   pixperunit_vert = WinMetrics(-6)
	
Use WinMetrics to translate dialog units to pixels:
	   ScreenWidth = WinMetrics(0)/WinMetrics(-6) ;Determine screen width in dialog units
	   ScreenHeight = WinMetrics(1)/WinMetrics(-5) ;Determine screen height in dialog units
	   Message (ScreenWidth,ScreenHeight)
	
This information can be used for calculations as follows:
	   DlgWidth = ScreenWidth*0.663 ;Make a dialog 2/3 width of screen
	   DlgHeight= ScreenHeight*0.5  ;Make a dialog 1/2 height of screen
	   DlgX=(ScreenWidth-DlgWidth) /2 ;Determine X co-ordinate for a dialog to be centered
	   DlgY=(ScreenHeight-DlgHeight)/2;Determine Y co-ordinate for a dialog to be centered
	

Screen Pixels:

Coordinates expressed in Windows virtual screen pixel coordinates, in the format: "left,top,right,bottom". A few functions (see below) can return virtual screen pixel coordinates, but only one known function accepts these coordinates as a parameter. That is SysParamInfo 47 SetWorkArea.

How do I get the size of the active windows in pixels?

	   winpos = WinPosition(WinGetactive())
	   xulc = ItemExtract(1, winpos, ",")
	   yulc = ItemExtract(2, winpos, ",")
	   xbrc = ItemExtract(3, winpos, ",")
	   ybrc = ItemExtract(4, winpos, ",")
	   width = (xbrc - xulc) * WinMetrics(0) / 1000
	   height = (ybrc - yulc) * WinMetrics(1) / 1000
	   Message(width, height)
	

Functions: MouseInfo, SysParaminfo, WinMetrics and {DllCalls to Windows API Functions}


Code Samples

Dialog Units to Pixels. Translate dialog units to pixels:

; Converts horizontal dialog units to pixels.  (x axis)
#DefineFunction ToPixelsHorizontal(du)
   Return Int(du*WinMetrics(-6))
#EndFunction

; Converts vertical dialog units to pixels. ( y axis)
#DefineFunction ToPixelsVertical(du)
   Return Int(du*WinMetrics(-5))
#EndFunction

; Test data.
nDuX = 40  ; Horizontal dimension in du.
nDuY = 11  ; Vertical dimension in du.

; Test.
nPixelsX = ToPixelsHorizontal( nDuX )
nPixelsY = ToPixelsVertical( nDuY )

; Result.
Message("Dialog Units x = %nDuX%, y = %nDuy%", "Pixels x = %nPixelsX%, y = %nPixelsY%")

Screen Pixels to WinBatch standard. Translate screen pixels to WinBatch standard 1000x1000 :

sWorkArea      = SysParamInfo(48, "", 0)
iLeft       = ItemExtract(1, sWorkArea, ",")
iTop       = ItemExtract(2, sWorkArea, ",")
iRight       = ItemExtract(3, sWorkArea, ",")
iBottom       = ItemExtract(4, sWorkArea, ",")

; Calculation : WorkArea multiplied by 1000 then divided by actual screen height.
; xulc = iLeft*1000/WinMetrics(0)
; yulc = iTop*1000/WinMetrics(1)
; xbrc = iRight*1000/WinMetrics(0)
; ybrc = iBottom*1000/WinMetrics(1)

; More Precise MulDiv Calculation
xulc  = DllCall(DirWindows(1):"kernel32.dll",long:"MulDiv",long:iLeft,long:1000,long:WinMetrics(0))
yulc = DllCall(DirWindows(1):"kernel32.dll",long:"MulDiv",long:iTop,long:1000,long:WinMetrics(1))
xbrc = DllCall(DirWindows(1):"kernel32.dll",long:"MulDiv",long:iRight,long:1000,long:WinMetrics(0))
ybrc = DllCall(DirWindows(1):"kernel32.dll",long:"MulDiv",long:iBottom,long:1000,long:WinMetrics(1))

Pause('GetWorkArea Window Resize Results',xulc:',':yulc:',':xbrc:',':ybrc)
title = '' ; Blank string refers to the Main WIL window
WinPlace( xulc, yulc, xbrc, ybrc,  title )
TimeDelay(5)
Exit

WinBatch Standard to Screen Pixels. Translate WinBatch standard 1000x1000 to screen pixels :

winpos = WinPosition(WinGetactive())
xulc = ItemExtract(1, winpos, ",")
yulc = ItemExtract(2, winpos, ",")
xbrc = ItemExtract(3, winpos, ",")
ybrc = ItemExtract(4, winpos, ",")
width = (xbrc - xulc) * WinMetrics(0) / 1000
height = (ybrc - yulc) * WinMetrics(1) / 1000
Message(width, height)

Article ID:   W17511
Filename:   Screen Coordinates Explained.txt
File Created: 2011:11:29:08:36:46
Last Updated: 2011:11:29:08:36:46