views:

26

answers:

1

So I'm trying to get the right Device Context so I can set the gamma ramp on individual monitors in an N monitor configuration (2+).

I've tried

[DllImport("gdi32.dll")]
static extern IntPtr CreateDC(string lpszDriver, string lpszDevice, string lpszOutput, IntPtr lpInitData);

With this method I'm using the string "DISPLAY" for lpszDriver, I'm enumerating displays with another pInvoke method and getting the Display device name which ends up being something like "\Registry\Machine\System\CurrentControlSet\Control\Class{4d36e96e-e325-11ce-bfc1-08002be10318}\0042" and passing in as lpszDevice. lpszOutput is null and lpInitData is IntPtr.Zero. The hDC that comes back works, but seems to be global.

and

[DllImport("user32.dll")]
public static extern IntPtr GetDC(IntPtr hWnd);

With this method I've tried using the actual window form handle.

I'm using

[DllImport("gdi32.dll")]
private static extern int SetDeviceGammaRamp(IntPtr hDC, ref RAMP lpRamp);

It does set the gamma ramp, but it always sets both monitors. Any ideas?

+2  A: 

Using the device name "DISPLAY" is getting you a DC for the whole display system, so setting the gamma ramp (for your example) affects all displays in the system.

You can get a device name for an individual monitor by calling EnumDisplayMonitors to retrieve an HMONITOR for each monitor, then GetMonitorInfo with a MONITORINFOEX structure for each HMONITOR. The MONITORINFOEX contains an szDevice member that you pass to GetDC to get a DC specifically for (the card driving) that monitor, and set the gamma ramp for it.

Note that this actually gets you a device name for the graphics card to which the monitor is attached. If memory serves, with older hardware and/or software, two monitors attached to the same card were always stuck with the same gamma ramp and such. With current hardware/software, a single card with two monitors will look to the system like two cards (with something like a ":0" or ":1" on the end of the name, if memory serves), so even if the two monitors are attached to the same physical card, from a viewpoint of setting the gamma ramp, it's still treated as two separate ones). Offhand, I'm not sure of exactly how new of hardware and/or software is needed to treat a single physical card driving multiple monitors as multiple virtual cards.

Jerry Coffin
Thank you for the response, I'm trying to get this working.The one odd thing is that I'm getting blank szDevice names coming back.I'm actually working in .net using interop.
Joel Barsotti
Also wouldn't you need to call CreateDC?GetDC only taks a hWnd as an argument, where as szDevice is a name?
Joel Barsotti
Figured out my issue with the null names, dumb mistake on my part.Thanks so much.
Joel Barsotti
Also this is the same name you can get from windows.forms.screens.allscreens
Joel Barsotti
Oops -- yes, it should be CreateDC, not GetDC. As far as blank szDevice names go, I'm not sure -- I'm getting names like "\\.\DISPLAY1" (using Windows 7 x64). OTOH, I am writing native code -- the problem could be arising in using P/Invoke (with which I'm afraid I can't or at least won't try to help).
Jerry Coffin