0

I draw a so-called reversible line in a .NET Framework WinForms app using code like this:

ControlPaint.DrawReversibleLine(
    PointToScreen(new Point(x, y1)),
    PointToScreen(new Point(x, y2)),
    Color.Black);

If high-dpi support is not turned on in the app and the app is launched on a hi-res screen, the x, y1 and y2 coordinates come from the mouse events as if the app worked on a 96 dpi screen because of the Windows dpi virtualization. But when I pass these numbers to the Control.PointToScreen() function, it processes them taking into account the real resolution of the screen. As a result, the points I get from PointToScreen are shifted to the left-top corner on the screen on 4K screens.

Is there a simple way to overcome this problem? Please, take into account modern multi-monitor configurations in which every monitor may have its own resolution.

TecMan
  • 2,743
  • 2
  • 30
  • 64
  • Make your app DpiAware. – Jimi Jun 04 '20 at 14:34
  • @Jimi, this construction must work in both environments - when dpi awareness is on or off. – TecMan Jun 04 '20 at 15:23
  • What *environments*? It's your app that declares itself DpiAware. When you do that, then you receive non-virtualized measures. When you don't, you may receive virtualized and non-virtualized measures, depending on what method/property you call/access. Not a difficult choice to make... – Jimi Jun 04 '20 at 15:27
  • @Jimi, actually it is a control used on WinForms forms by others. So I can't control whether the surrounding 'environment' (app) is dpi-aware or not. – TecMan Jun 04 '20 at 15:30
  • 1
    Start from here: [Detect if non DPI-aware application has been scaled/virtualized](https://stackoverflow.com/a/36864741/7444103). This may also help: [Using SetWindowPos with multiple monitors](https://stackoverflow.com/a/53026765/7444103). Take a look at the Mixed-Mode DPI Scaling and DPI and Device-Independent Pixels info. Also, note that you don't actually need `DrawReversibleLine`, you can draw directly to the Screen. And/or handle yourself [MapWindowPoints](https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-mapwindowpoints) (the function `PointToScreen()` calls) – Jimi Jun 04 '20 at 16:10

0 Answers0