我需要用鼠标在所有东西上画一条线。我可以使用p/invoke获取桌面窗口的图形对象:
desktopgraphics=graphics.fromhdc(getdc(intptr.zero));
但是,我使用这个图形对象绘制的任何内容只显示在左监视器上,而在右监视器上则不显示。它没有失败,或者什么,只是没有表现出来。
创建图形对象后,它显示可见剪辑区域为1680 x 1050,这是我的左监视器的分辨率。我只能假设它只为左监视器获取设备上下文。它们是获取两个(或任何数量)监视器的设备上下文的方法吗?
编辑3/7/2009:
有关我使用的修复程序的其他信息。
我使用了由colithium提供的修复程序来创建每个监视器的图形对象以及存储偏移量的方法,这样我就可以将全局鼠标点转换为图形表面上的有效点。
private void InitializeGraphics()
{
// Create graphics for each display using compatibility mode
CompatibilitySurfaces = Screen.AllScreens.Select(s => new CompatibilitySurface()
{
SurfaceGraphics = Graphics.FromHdc(CreateDC(null, s.DeviceName, null, IntPtr.Zero)),
Offset = new Size(s.Bounds.Location)
}).ToArray();
}
private class CompatibilitySurface : IDisposable
{
public Graphics SurfaceGraphics = null;
public Size Offset = default(Size);
public PointF[] OffsetPoints(PointF[] Points)
{
return Points.Select(p => PointF.Subtract(p, Offset)).ToArray();
}
public void Dispose()
{
if (SurfaceGraphics != null)
SurfaceGraphics.Dispose();
}
}
[DllImport("gdi32.dll")]
static extern IntPtr CreateDC(string lpszDriver, string lpszDevice, string lpszOutput, IntPtr lpInitData);