How do I get the color depth of the screen?

Raymond Chen


How do I get the color depth of the screen? This question already makes an assumption that isn’t always true, but we’ll answer the question first, then discuss why the answer is wrong.

If you have a device context for the screen, you can query the color depth with a simple arithmetic calculation:

colorDepth = GetDeviceCaps(hdc, BITSPIXEL) *
             GetDeviceCaps(hdc, PLANES);

Now that you have the answer, I’ll explain why it’s wrong, but you can probably guess the reason already.

Two words: Multiple monitors.

If you have multiple monitors connected to your system, each one can be running at a different color depth. For example, your primary monitor might be running at 32 bits per pixel, while the secondary is stuck at 16 bits per pixel. When there was only one monitor, there was such a thing as the color depth of the screen, but when there’s more than one, you first have to answer the question, “Which screen?”

To get the color depth of each monitor, you can take your device context and ask the window manager to chop the device context into pieces, each corresponding to a different monitor.

EnumDisplayMonitors(hdc, NULL, MonitorEnumProc, 0);
// this function is called once for each "piece"
BOOL CALLBACK MonitorEnumProc(HMONITOR hmon, HDC hdc,
                              LPRECT prc, LPARAM lParam)
   // compute the color depth of monitor "hmon"
   int colorDepth = GetDeviceCaps(hdc, BITSPIXEL) *
                    GetDeviceCaps(hdc, PLANES);
   return TRUE;

If you decide to forego splitting the DC into pieces and just ask for “the” color depth, you’ll get the color depth information for the primary monitor.

As a bonus (and possible optimization), there is a system metric GetSystemMetrics(SM_SAMEDISPLAYFORMAT) which has a nonzero value if all the monitors in the system have the same color format.


Comments are closed. Login to edit/delete your existing comments