September 14th, 2009

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.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.