可能重复:
如何从C#winform应用程序检索屏幕分辨率?
如何在Windows窗体中获取屏幕大小?
我正在使用以下代码在单个显示器上绘制:
Point cursorLocation;
NativeMethods.GetCursorPos(out cursorLocation);
Screen screen = Screen.FromPoint(cursorLocation);
Point h1 = new Point(screen.Bounds.Left, cursorLocation.Y);
Point h2 = new Point(screen.Bounds.Right, cursorLocation.Y);
Point v1 = new Point(cursorLocation.X, screen.Bounds.Top);
Point v2 = new Point(cursorLocation.X, screen.Bounds.Bottom);
using (Graphics graphics = Graphics.FromHwnd(NativeMethods.GetDesktopWindow())) {
NativeMethods.SHChangeNotify(0x8000000, 0x1000, IntPtr.Zero, IntPtr.Zero);
graphics.DrawLine(Pens.Red, h1, h2);
graphics.DrawLine(Pens.Red, v1, v2);
}
Run Code Online (Sandbox Code Playgroud)
本身,这应该 从理论上得出任何显示器上。但是,它仅利用初级。所以,为了解决这个问题,我得到了所有显示器的 DC 并尝试这样做。
IntPtr hdc = NativeMethods.CreateDC("DISPLAY", IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
Graphics graphics = Graphics.FromHdc(hdc);
graphics.DrawLine(Pens.Red, h1, h2);
graphics.DrawLine(Pens.Red, v1, v2);
graphics.Dispose();
NativeMethods.ReleaseDC(IntPtr.Zero, hdc);
Run Code Online (Sandbox Code Playgroud)
想想看,这甚至根本没有绘制到屏幕上。我为 CreateDC 尝试了各种重载,并搜索了 SO …