This example shows how to use various declares and Xojo API2 code to dynamically retrieve the width and height of a screen so that these dimensions can be used to resize your main window.

Screen Resolution API2

 

 

Above are two screen grabs where the Xojo program is running on the same computer with two different resolutions set on the two screens for the computer. 

 

Sub Action() Handles Action
//Create position variables for
//x and y coordinates
Dim PosX, PosY as Int32

//Get the point of the cursor on the screen
GetCursorPoint(PosX, PosY)

//Show the mouse x,y position in
// the TextArea1 Control
TextArea1.Value = "Global Mouse Coordinates: X: " + PosX.ToText + ", Y: " + PosY.ToText + EndOfLine

//Get the handle for the monitor based on the mouse position
Dim hdlMonitor as Integer = GetMonitorHandle(PosX, PosY)
TextArea1.Value = TextArea1.Value + "Monitor Handle: " + hdlMonitor.ToText + EndOfLine

//Get the monitor display text from the handle
Dim MonitorText as String = ShowMonitorDisplayText(hdlMonitor)

//Show the display number
TextArea1.Value = TextArea1.Value + "Cursor in monitor number: " + MonitorText +_
", or screen #: " + MonitorText.Right(1) + EndOfLine

Dim SF as Integer
Call GetScaleFactorForMonitor(hdlMonitor, SF)
TextArea1.Value = TextArea1.Value + "ScaleFactor (incorrect): " + sf.ToText + EndOfLine

Dim Effxdpi, Effydpi as UInt32
Call GetDpiForMonitor(hdlMonitor, MDT_Effective_DPI, Effxdpi, Effydpi)

Dim MyRatio as Double = Effxdpi/96*100
TextArea1.Value = TextArea1.Value + "Effective xdpi: " + Effxdpi.ToText + EndOfLine +_
"ratio: " + MyRatio.ToText + "%" + EndOfLine

TextArea1.Value = TextArea1.Value + "Xojo Screen Number: " + WhichScreen(PosX,PosY).ToText + EndOfLine

TextArea1.Value = TextArea1.Value + "Left=" + Str(Screen.ScreenAt(WhichScreen(PosX,PosY)).AvailableLeft) + EndOfLine
TextArea1.Value = TextArea1.Value + "Width=" + Str(Screen.ScreenAt(WhichScreen(PosX,PosY)).AvailableWidth) + EndOfLine
TextArea1.Value = TextArea1.Value + "Top=" + Str(Screen.ScreenAt(WhichScreen(PosX,PosY)).AvailableTop) + EndOfLine
TextArea1.Value = TextArea1.Value + "Height=" + Str(Screen.ScreenAt(WhichScreen(PosX,PosY)).AvailableHeight) + EndOfLine
End Sub
 

 

The main part of the code is to use the Screen.ScreenAt(WhichScreen(PosX,PosY)).AvailableHeight, and AvailableWidth calls when the declares retrieve the Xojo screen number. A timer is used to update the resolution data and also captures the mouse position.


The code is available on Github at: MultiScreen Resolution Xojo 2020 r1 API2

An older working version of the program has also been created in the Xojo 2015 r1 IDE that is available in Github.