Can you create an information context for the display?

Adrian McCarthy asks, "Can you create an information context for the display? ... I can call CreateDC("DISPLAY"), but perhaps that wouldn't generalize for a multiple-monitor display with different settings on each screen. I'm trying to avoid constantly creating and destroying DCs when all I need to do is measure strings, check color depth, dpi, etc."

I admire the effort of trying to avoid creating a whole DC when all you want is to perform some inquiries. Some inquiries are monitor-independent, like getting the DPI or measuring strings, so you can just use GetDC(NULL) to get a temporary DC. This is cheaper than a full-on CreateDC since GetDC goes through the DC cache, so you're just grabbing a DC out of the cache temporarily, doing a little computation, and then returning it to the cache (with ReleaseDC).

If you are doing something that is monitor-specific, like getting its color depth, you can call EnumDisplayMonitors on the desktop DC to look at each monitor.

(And just for completeness, to get the name for a specific monitor if you really do want to create an IC for it, call GetMonitorInfo with a MONITORINFOEX and look at the szDevice member.)

Update: Original text said "DC" in the last sentence by mistake.

Comments (9)
  1. Ben Voigt [C++ MVP] says:

    … and the answer to the original question is?  I get the impression of “no, but it doesn’t matter because you can GetDC cheaper” but a straight answer would be nice.

    [See last sentence, though since I use DC’s far more than IC’s, I mistyped “DC” for “IC” but it should be clear from context. -Raymond]
  2. rs says:

    MSDN states that ICs are somehow more efficient than DCs:

    Presumably the overhead of creating certain structures was significant a long time ago. How relevant is the distinction in more recent Windows versions (>= Win2K)?

    [If your choice is between CreateDC and CreateIC, then CreateIC is better. But GetDC is better than both of them. -Raymond]
  3. porter says:

    > How relevant is the distinction in more recent Windows versions (>= Win2K)?

    Wouldn’t faster hardware have made more impact than the version of windows?

    I’m sure you can maintain the same quality of experience for the user by soaking up any advantages from faster hardware by inefficient programming.

  4. Gabe says:

    I wonder what it would be like to be in a world where DPI was monitor-dependent.

  5. Adrian says:

    Thanks for the info and the clarification of IC/DC in the last paragraph.

  6. Zan Lynx says:

    Actual dots per inch has always been monitor dependent.

    Windows is still working on getting its software DPI to agree with the real thing.

    I’m sure Windows will get there in another two or three versions.

  7. porter says:

    > Windows is still working on getting its software DPI to agree with the real thing.

    They may find it quicker to change the definition of inches.

  8. Mike says:

    "Software DPI" and the real thing will probably never match.  Nor does it make a lot of sense for them to do so for many devices.  An obvious case is a projector screen where physical dpi depends on the somewhat random distance between the projector and whatever it’s projecting onto.

    It’s just a poorly named UI scaling factor.

  9. Aaron G says:

    I’m wondering how this extends to .NET.  Will Graphics.FromHdc(IntPtr.Zero) perform a similar function?  Or would one have to use interop to actually create the DC via GetDC(0) and use that as the handle?  Or would neither of those work?

    (I tried looking it up, but I don’t see anything interesting in the Graphics class documentation)

Comments are closed.

Skip to main content