获取具有多个Montior的整个屏幕的DeviceContext [英] Get DeviceContext of Entire Screen with Multiple Montiors

查看:166
本文介绍了获取具有多个Montior的整个屏幕的DeviceContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用C#在所有内容上画一条线(用鼠标).我可以使用P/Invoke获取桌面窗口的Graphics对象:

I need to draw a line (with the mouse) over everything with C#. I can get a Graphics object of the desktop window by using P/Invoke:

DesktopGraphics = Graphics.FromHdc(GetDC(IntPtr.Zero));

DesktopGraphics = Graphics.FromHdc(GetDC(IntPtr.Zero));

但是,我使用此图形对象绘制的所有内容仅显示在左侧监视器上,而没有显示在右侧监视器上.它不会失败,也不会显示任何东西.

However, anything I draw using this graphics object is only showing on the left monitor, and nothing on the right monitor. It doesn't fail or anything, it just doesn't show.

创建Graphics对象后,它显示的可见剪辑区域为1680 x 1050,这是我的左监视器的分辨率.我只能假设它只是为左监视器获取设备上下文.是他们获取两个(或任意数量)监视器的设备上下文的一种方法吗?

After I create the Graphics object, it shows the visible clip region to be 1680 x 1050 which is the resolution of my left monitor. I can only assume that it's only getting a device context for the left monitor. Is their a way to get the device context for both (or any number) monitors?

编辑2009年3月7日: 有关我使用的修复程序的其他信息.

EDIT 3/7/2009: Additional information about the fix I used.

我使用colithium提供的修复程序提供了以下代码,用于为每个监视器创建图形对象,以及一种存储偏移量的方法,以便可以将全局鼠标点转换为图形表面上的有效点.

I used the fix provided by colithium to come up with the following code for creating a graphics object for each monitor as well as a way to store the offset so that I can translate global mouse points to valid points on the graphics surface.

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);

推荐答案

这里是到另一个有相同问题的人的链接.通过调用以下方法解决了该问题:

Here is a link to another person that had the same problem. It was solved with a call to:

CreateDC(TEXT("DISPLAY"),NULL,NULL,NULL)

这将向所有监视器返回DC.

which will return a DC to all monitors.

这篇关于获取具有多个Montior的整个屏幕的DeviceContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆