简体   繁体   English

c#直接在屏幕上绘制-坐标错误

[英]c# draw directly on screen - wrong coordinates

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但是它绘制我的矩形的坐标是错误的,因为我在窗口中有 150% 的缩放比例 - 如果我想在 bootom 右角附近绘制矩形(例如 new Rectangle(2500,1400,30,30)),它会在屏幕中间附近绘制它

when i try rescale rect and when rect is near top left corner of screen - it is ok ;)当我尝试重新缩放 rect 并且当 rect 靠近屏幕的左上角时 - 没关系;)

but if rect is near bottom right corner it not draw it at all ;(但是如果 rect 在右下角附近,则根本不会绘制它;(

the same happens when i use g.ScaleTransform(1.5f)当我使用 g.ScaleTransform(1.5f) 时也会发生同样的情况

problem is g.VisibleClipBounds - that is: 2560 width and 1440 height on 4k screen问题是 g.VisibleClipBounds - 即:4k 屏幕上的 2560 宽度和 1440 高度

what should i to to get rectangle in right coordinates?我应该怎样才能在正确的坐标中获得矩形?

after reading Jimi comment i read something about DpiAwareness, after that i add to my App.config lines:在阅读 Jimi 评论后,我阅读了一些关于 DpiAwareness 的内容,之后我添加到我的 App.config 行:

<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)问题消失了——我可以缩放我的矩形并且我看到了,我也可以使用 g.ScaleTransform(visualclipRect 现在是 4096x2160)

thanks Jimi ;)谢谢吉米;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM