@@ -1704,7 +1704,11 @@ public Control getCursorControl () {
17041704 */
17051705public Point getCursorLocation () {
17061706 checkDevice ();
1707- return DPIUtil .autoScaleDown (getCursorLocationInPixels ());
1707+ Point cursorLocationInPixels = getCursorLocationInPixels ();
1708+ if (isRescalingAtRuntime ()) {
1709+ return translateLocationInPointInDisplayCoordinateSystem (cursorLocationInPixels .x , cursorLocationInPixels .y );
1710+ }
1711+ return DPIUtil .autoScaleDown (cursorLocationInPixels );
17081712}
17091713
17101714Point getCursorLocationInPixels () {
@@ -2183,14 +2187,22 @@ Monitor getMonitor (long hmonitor) {
21832187 OS .GetMonitorInfo (hmonitor , lpmi );
21842188 Monitor monitor = new Monitor ();
21852189 monitor .handle = hmonitor ;
2186- Rectangle boundsInPixels = new Rectangle (lpmi .rcMonitor_left , lpmi .rcMonitor_top , lpmi .rcMonitor_right - lpmi .rcMonitor_left ,lpmi .rcMonitor_bottom - lpmi .rcMonitor_top );
2187- monitor .setBounds (DPIUtil .autoScaleDown (boundsInPixels ));
2188- Rectangle clientAreaInPixels = new Rectangle (lpmi .rcWork_left , lpmi .rcWork_top , lpmi .rcWork_right - lpmi .rcWork_left , lpmi .rcWork_bottom - lpmi .rcWork_top );
2189- monitor .setClientArea (DPIUtil .autoScaleDown (clientAreaInPixels ));
2190+ Rectangle boundsInPixels = new Rectangle (lpmi .rcMonitor_left , lpmi .rcMonitor_top , lpmi .rcMonitor_right - lpmi .rcMonitor_left ,lpmi .rcMonitor_bottom - lpmi .rcMonitor_top );
2191+ Rectangle clientAreaInPixels = new Rectangle (lpmi .rcWork_left , lpmi .rcWork_top , lpmi .rcWork_right - lpmi .rcWork_left , lpmi .rcWork_bottom - lpmi .rcWork_top );
21902192 int [] dpiX = new int [1 ];
21912193 int [] dpiY = new int [1 ];
21922194 int result = OS .GetDpiForMonitor (monitor .handle , OS .MDT_EFFECTIVE_DPI , dpiX , dpiY );
21932195 result = (result == OS .S_OK ) ? DPIUtil .mapDPIToZoom (dpiX [0 ]) : 100 ;
2196+
2197+ if (DPIUtil .isAutoScaleOnRuntimeActive ()) {
2198+ int autoscaleZoom = DPIUtil .getZoomForAutoscaleProperty (result );
2199+ monitor .setBounds (getMonitorBoundsInPointsInDisplayCoordinateSystem (boundsInPixels , autoscaleZoom ));
2200+ monitor .setClientArea (getMonitorBoundsInPointsInDisplayCoordinateSystem (clientAreaInPixels , autoscaleZoom ));
2201+ } else {
2202+ monitor .setBounds (DPIUtil .autoScaleDown (boundsInPixels ));
2203+ monitor .setClientArea (DPIUtil .autoScaleDown (clientAreaInPixels ));
2204+
2205+ }
21942206 if (result == 0 ) {
21952207 System .err .println ("***WARNING: GetDpiForMonitor: SWT could not get valid monitor scaling factor." );
21962208 result = 100 ;
@@ -2203,6 +2215,13 @@ Monitor getMonitor (long hmonitor) {
22032215 return monitor ;
22042216}
22052217
2218+ private Rectangle getMonitorBoundsInPointsInDisplayCoordinateSystem (Rectangle boundsInPixels , int zoom ) {
2219+ Rectangle bounds = DPIUtil .scaleDown (boundsInPixels , zoom );
2220+ bounds .x = boundsInPixels .x ;
2221+ bounds .y = boundsInPixels .y ;
2222+ return bounds ;
2223+ }
2224+
22062225/**
22072226 * Returns an array of monitors attached to the device.
22082227 *
@@ -2944,6 +2963,9 @@ boolean isValidThread () {
29442963public Point map (Control from , Control to , Point point ) {
29452964 checkDevice ();
29462965 if (point == null ) error (SWT .ERROR_NULL_ARGUMENT );
2966+ if (isRescalingAtRuntime ()) {
2967+ return map (from , to , point .x , point .y );
2968+ }
29472969 int zoom = getZoomLevelForMapping (from , to );
29482970 point = DPIUtil .scaleUp (point , zoom );
29492971 return DPIUtil .scaleDown (mapInPixels (from , to , point ), zoom );
@@ -2991,6 +3013,20 @@ Point mapInPixels (Control from, Control to, Point point) {
29913013 */
29923014public Point map (Control from , Control to , int x , int y ) {
29933015 checkDevice ();
3016+ if (isRescalingAtRuntime ()) {
3017+ Point mappedPointInPoints ;
3018+ if (from == null ) {
3019+ Point mappedPointInpixels = mapInPixels (from , to , getPixelsFromPoint (to .getShell ().getMonitor (), x , y ));
3020+ mappedPointInPoints = DPIUtil .scaleDown (mappedPointInpixels , to .getZoom ());
3021+ } else if (to == null ) {
3022+ Point mappedPointInpixels = mapInPixels (from , to , DPIUtil .scaleUp (new Point (x , y ), from .getZoom ()));
3023+ mappedPointInPoints = getPointFromPixels (from .getShell ().getMonitor (), mappedPointInpixels .x , mappedPointInpixels .y );
3024+ } else {
3025+ Point mappedPointInpixels = mapInPixels (from , to , DPIUtil .scaleUp (new Point (x , y ), from .getZoom ()));
3026+ mappedPointInPoints = DPIUtil .scaleDown (mappedPointInpixels , to .getZoom ());
3027+ }
3028+ return mappedPointInPoints ;
3029+ }
29943030 int zoom = getZoomLevelForMapping (from , to );
29953031 x = DPIUtil .scaleUp (x , zoom );
29963032 y = DPIUtil .scaleUp (y , zoom );
@@ -3058,6 +3094,9 @@ private int getZoomLevelForMapping(Control from, Control to) {
30583094public Rectangle map (Control from , Control to , Rectangle rectangle ) {
30593095 checkDevice ();
30603096 if (rectangle == null ) error (SWT .ERROR_NULL_ARGUMENT );
3097+ if (isRescalingAtRuntime ()) {
3098+ return map (from , to , rectangle .x , rectangle .y , rectangle .width , rectangle .height );
3099+ }
30613100 int zoom = getZoomLevelForMapping (from , to );
30623101 rectangle = DPIUtil .scaleUp (rectangle , zoom );
30633102 return DPIUtil .scaleDown (mapInPixels (from , to , rectangle ), zoom );
@@ -3107,6 +3146,20 @@ Rectangle mapInPixels (Control from, Control to, Rectangle rectangle) {
31073146 */
31083147public Rectangle map (Control from , Control to , int x , int y , int width , int height ) {
31093148 checkDevice ();
3149+ if (isRescalingAtRuntime ()) {
3150+ Rectangle mappedRectangleInPoints ;
3151+ if (from == null ) {
3152+ Rectangle mappedRectangleInPixels = mapInPixels (from , to , translateRectangleInPixelsInDisplayCoordinateSystem (x , y , width , height , to .getShell ().getMonitor ()));
3153+ mappedRectangleInPoints = DPIUtil .scaleDown (mappedRectangleInPixels , to .getZoom ());
3154+ } else if (to == null ) {
3155+ Rectangle mappedRectangleInPixels = mapInPixels (from , to , DPIUtil .scaleUp (new Rectangle (x , y , width , height ), from .getZoom ()));
3156+ mappedRectangleInPoints = translateRectangleInPointsInDisplayCoordinateSystem (mappedRectangleInPixels .x , mappedRectangleInPixels .y , mappedRectangleInPixels .width , mappedRectangleInPixels .height , from .getShell ().getMonitor ());
3157+ } else {
3158+ Rectangle mappedRectangleInPixels = mapInPixels (from , to , DPIUtil .scaleUp (new Rectangle (x , y , width , height ), from .getZoom ()));
3159+ mappedRectangleInPoints = DPIUtil .scaleDown (mappedRectangleInPixels , to .getZoom ());
3160+ }
3161+ return mappedRectangleInPoints ;
3162+ }
31103163 int zoom = getZoomLevelForMapping (from , to );
31113164 x = DPIUtil .scaleUp (x , zoom );
31123165 y = DPIUtil .scaleUp (y , zoom );
@@ -3130,6 +3183,57 @@ Rectangle mapInPixels (Control from, Control to, int x, int y, int width, int he
31303183 return new Rectangle (rect .left , rect .top , rect .right - rect .left , rect .bottom - rect .top );
31313184}
31323185
3186+ Point translateLocationInPixelsInDisplayCoordinateSystem (int x , int y ) {
3187+ Monitor monitor = getContainingMonitor (x , y );
3188+ return getPixelsFromPoint (monitor , x , y );
3189+ }
3190+
3191+ Point translateLocationInPointInDisplayCoordinateSystem (int x , int y ) {
3192+ Monitor monitor = getContainingMonitorInPixelsCoordinate (x , y );
3193+ return getPointFromPixels (monitor , x , y );
3194+ }
3195+
3196+ Rectangle translateRectangleInPixelsInDisplayCoordinateSystemByContainment (int x , int y , int width , int height ) {
3197+ Monitor monitorByLocation = getContainingMonitor (x , y );
3198+ Monitor monitorByContainment = getContainingMonitor (x , y , width , height );
3199+ return translateRectangleInPixelsInDisplayCoordinateSystem (x , y , width , height , monitorByLocation , monitorByContainment );
3200+ }
3201+
3202+ private Rectangle translateRectangleInPixelsInDisplayCoordinateSystem (int x , int y , int width , int height , Monitor monitor ) {
3203+ return translateRectangleInPixelsInDisplayCoordinateSystem (x , y , width , height , monitor , monitor );
3204+ }
3205+
3206+ private Rectangle translateRectangleInPixelsInDisplayCoordinateSystem (int x , int y , int width , int height , Monitor monitorOfLocation , Monitor monitorOfArea ) {
3207+ Point topLeft = getPixelsFromPoint (monitorOfLocation , x , y );
3208+ int zoom = getApplicableMonitorZoom (monitorOfArea );
3209+ int widthInPixels = DPIUtil .scaleUp (width , zoom );
3210+ int heightInPixels = DPIUtil .scaleUp (height , zoom );
3211+ return new Rectangle (topLeft .x , topLeft .y , widthInPixels , heightInPixels );
3212+ }
3213+
3214+ Rectangle translateRectangleInPointsInDisplayCoordinateSystemByContainment (int x , int y , int widthInPixels , int heightInPixels ) {
3215+ Monitor monitorByLocation = getContainingMonitor (x , y );
3216+ Monitor monitorByContainment = getContainingMonitor (x , y , widthInPixels , heightInPixels );
3217+ return translateRectangleInPointsInDisplayCoordinateSystem (x , y , widthInPixels , heightInPixels , monitorByLocation , monitorByContainment );
3218+ }
3219+
3220+ private Rectangle translateRectangleInPointsInDisplayCoordinateSystem (int x , int y , int widthInPixels , int heightInPixels , Monitor monitor ) {
3221+ return translateRectangleInPointsInDisplayCoordinateSystem (x , y , widthInPixels , heightInPixels , monitor , monitor );
3222+ }
3223+
3224+
3225+ private Rectangle translateRectangleInPointsInDisplayCoordinateSystem (int x , int y , int widthInPixels , int heightInPixels , Monitor monitorOfLocation , Monitor monitorOfArea ) {
3226+ Point topLeft = getPointFromPixels (monitorOfLocation , x , y );
3227+ int zoom = getApplicableMonitorZoom (monitorOfArea );
3228+ int width = DPIUtil .scaleDown (widthInPixels , zoom );
3229+ int height = DPIUtil .scaleDown (heightInPixels , zoom );
3230+ return new Rectangle (topLeft .x , topLeft .y , width , height );
3231+ }
3232+
3233+ private int getApplicableMonitorZoom (Monitor monitor ) {
3234+ return DPIUtil .getZoomForAutoscaleProperty (isRescalingAtRuntime () ? monitor .zoom : getDeviceZoom ());
3235+ }
3236+
31333237long messageProc (long hwnd , long msg , long wParam , long lParam ) {
31343238 switch ((int )msg ) {
31353239 case SWT_RUNASYNC : {
@@ -4355,7 +4459,12 @@ public void sendPostExternalEventDispatchEvent () {
43554459 */
43564460public void setCursorLocation (int x , int y ) {
43574461 checkDevice ();
4358- setCursorLocationInPixels (DPIUtil .autoScaleUp (x ), DPIUtil .autoScaleUp (y ));
4462+ if (isRescalingAtRuntime ()) {
4463+ Point cursorLocationInPixels = translateLocationInPixelsInDisplayCoordinateSystem (x , y );
4464+ setCursorLocationInPixels (cursorLocationInPixels .x , cursorLocationInPixels .y );
4465+ } else {
4466+ setCursorLocationInPixels (DPIUtil .autoScaleUp (x ), DPIUtil .autoScaleUp (y ));
4467+ }
43594468}
43604469
43614470void setCursorLocationInPixels (int x , int y ) {
@@ -5390,4 +5499,63 @@ private boolean setDPIAwareness(int desiredDpiAwareness) {
53905499 return true ;
53915500}
53925501
5502+ private Monitor getContainingMonitor (int x , int y ) {
5503+ Monitor [] monitors = getMonitors ();
5504+ for (Monitor currentMonitor : monitors ) {
5505+ Rectangle clientArea = currentMonitor .getClientArea ();
5506+ if (clientArea .contains (x , y )) {
5507+ return currentMonitor ;
5508+ }
5509+ }
5510+ return getPrimaryMonitor ();
5511+ }
5512+
5513+ private Monitor getContainingMonitor (int x , int y , int width , int height ) {
5514+ Rectangle rectangle = new Rectangle (x , y , width , height );
5515+ Monitor [] monitors = getMonitors ();
5516+ Monitor selectedMonitor = getPrimaryMonitor ();
5517+ int highestArea = 0 ;
5518+ for (Monitor currentMonitor : monitors ) {
5519+ Rectangle clientArea = currentMonitor .getClientArea ();
5520+ Rectangle intersection = clientArea .intersection (rectangle );
5521+ int area = intersection .width * intersection .height ;
5522+ if (area > highestArea ) {
5523+ selectedMonitor = currentMonitor ;
5524+ highestArea = area ;
5525+ }
5526+ }
5527+ return selectedMonitor ;
5528+ }
5529+
5530+ private Monitor getContainingMonitorInPixelsCoordinate (int xInPixels , int yInPixels ) {
5531+ Monitor [] monitors = getMonitors ();
5532+ for (Monitor current : monitors ) {
5533+ Rectangle clientArea = getMonitorClientAreaInPixels (current );
5534+ if (clientArea .contains (xInPixels , yInPixels )) {
5535+ return current ;
5536+ }
5537+ }
5538+ return getPrimaryMonitor ();
5539+ }
5540+
5541+ private Rectangle getMonitorClientAreaInPixels (Monitor monitor ) {
5542+ int zoom = getApplicableMonitorZoom (monitor );
5543+ int widthInPixels = DPIUtil .scaleUp (monitor .clientWidth , zoom );
5544+ int heightInPixels = DPIUtil .scaleUp (monitor .clientHeight , zoom );
5545+ return new Rectangle (monitor .clientX , monitor .clientY , widthInPixels , heightInPixels );
5546+ }
5547+
5548+ private Point getPixelsFromPoint (Monitor monitor , int x , int y ) {
5549+ int zoom = getApplicableMonitorZoom (monitor );
5550+ int mappedX = DPIUtil .scaleUp (x - monitor .clientX , zoom ) + monitor .clientX ;
5551+ int mappedY = DPIUtil .scaleUp (y - monitor .clientY , zoom ) + monitor .clientY ;
5552+ return new Point (mappedX , mappedY );
5553+ }
5554+
5555+ private Point getPointFromPixels (Monitor monitor , int x , int y ) {
5556+ int zoom = getApplicableMonitorZoom (monitor );
5557+ int mappedX = DPIUtil .scaleDown (x - monitor .clientX , zoom ) + monitor .clientX ;
5558+ int mappedY = DPIUtil .scaleDown (y - monitor .clientY , zoom ) + monitor .clientY ;
5559+ return new Point (mappedX , mappedY );
5560+ }
53935561}
0 commit comments