Note that there are some explanatory texts on larger screens.

plurals
  1. POGet DeviceContext of Entire Screen with Multiple Montiors
    primarykey
    data
    text
    <p>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:</p> <p>DesktopGraphics = Graphics.FromHdc(GetDC(IntPtr.Zero));</p> <p>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.</p> <p>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?</p> <hr> <p><strong>EDIT 3/7/2009: Additional information about the fix I used.</strong></p> <p>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.</p> <pre><code>private void InitializeGraphics() { // Create graphics for each display using compatibility mode CompatibilitySurfaces = Screen.AllScreens.Select(s =&gt; 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 =&gt; 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); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload