我正在使用以下代码在单个显示器上绘图:
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);
}
理论上,这应该 在任一监视器上使用。但是,它只利用初级。所以,为了解决这个问题,我得到了所有显示器的 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);
去想,这甚至根本没有绘制到屏幕上。我已经为 CreateDC 尝试了各种重载,并搜索了 SO 和其他资源,但我很难过。
一旦解决了这个问题,任何人都知道如何通过使用 SHCHangeNotify 刷新桌面来消除闪烁?我只画了两条线,它像疯了一样闪烁..)