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 Dis开发者_运维百科play 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?
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.
精彩评论