0

i try to draw rectangle directly on screen with that function:

public static void rectOnScreen2(Rectangle rect, int width = 3, Color? kolor = null)
    {   
        IntPtr desktopPtr = GetDC(IntPtr.Zero);
        Pen pen = new Pen(new SolidBrush(kolor ?? Color.Black), width);
        Graphics g = Graphics.FromHdc(desktopPtr);
        g.DrawRectangle(pen, rect);
        g.Dispose();
        ReleaseDC(IntPtr.Zero, desktopPtr);
    }

but coordinates where it draw my rect is wrong because i have 150% scaling in windows - if i want rect near bootom right corner (eg. new Rectangle(2500,1400,30,30)) it draws it near the middle of screen

when i try rescale rect and when rect is near top left corner of screen - it is ok ;)

but if rect is near bottom right corner it not draw it at all ;(

the same happens when i use g.ScaleTransform(1.5f)

problem is g.VisibleClipBounds - that is: 2560 width and 1440 height on 4k screen

what should i to to get rectangle in right coordinates?

barpas
  • 75
  • 1
  • 9
  • [Using SetWindowPos with multiple monitors](https://stackoverflow.com/a/53026765/7444103) - Don't skip the DpiAwareness part, it's quite important. -- (Unrelated) The pen object is also disposable. You should declare all disposable objects with `using` statements. -- If you need to get the bounds of a Window, use `DwmGetWindowAttribute()`, passing `DWMWA_EXTENDED_FRAME_BOUNDS` as the `DWMWINDOWATTRIBUTE`. – Jimi Jul 06 '22 at 21:48
  • ok, but my problem is: g.DrawRectangle() uses real screen coordinates but i can't draw outside virtual screen coordinates - i don't understand how your comment can help me... (i can rescale my rect - but i can't draw it outside g.VisibleClipBounds) – barpas Jul 06 '22 at 22:12

1 Answers1

0

after reading Jimi comment i read something about DpiAwareness, after that i add to my App.config lines:

<System.Windows.Forms.ApplicationConfigurationSection>
  <add key="DpiAwareness" value="PerMonitorV2" />
</System.Windows.Forms.ApplicationConfigurationSection>

the problem is gone - i can scale my rect and i see it, i can use g.ScaleTransform too (visualclipRect is 4096x2160 now)

thanks Jimi ;)

barpas
  • 75
  • 1
  • 9