![]() |
![]() |
|||||
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
|
Q: Determine the color resolution of screen(256 color, 16 bit, etc)Answer: The API GetDeviceCaps function allows you to determine the color resolution of the screen. GetDeviceCaps takes two arguments; a handle to a device context and an identifier for the parameter that you want to find. int GetDeviceCaps( HDC hdc, // device-context handle int nIndex); // parameter index In order to call GetDeviceCaps, you must provide a handle to a device context. In other programming environments, you would have to create a DC just for the sake of calling GetDeviceCaps, but in BCB, you can just borrow the Handle property of the form's Canvas property. To get the color resolution, you pass either BITSPIXEL or PLANES as the index value. BITSPIXEL returns the number of bits used to store the color of a single pixel. PLANES returns the number of color planes used by the system. Most systems use one color plane and multiple bits to represent the color of a pixel. Some systems use one pixel with multiple color planes. Either way, one of the values will be one. You can multiply the return values to yield one value that represents the color resolution of the system. The following code snippet demonstrates how to determine the color resolution of the system. As a bonus, it also shows how GetDeviceCaps can tell you directly if a system is using palettes. int BitsPerPixel = GetDeviceCaps(Canvas->Handle,BITSPIXEL); int Planes = GetDeviceCaps(Canvas->Handle,PLANES); BitsPerPixel *= Planes; // either Planes or BitsPerPixel will be // equal to 1, so eliminate one of the values. bool UsesPalettes = (bool)(GetDeviceCaps(Canvas->Handle, RASTERCAPS) & RC_PALETTE); if(UsesPalettes) Label1->Caption = "The screen utilizes palettes"; else Label1->Caption = "The screen does not use palettes"; switch (BitsPerPixel) { case 24: Label2->Caption = "24-bit true color"; break; case 16: Label2->Caption = "16-bit high color"; break; case 8: Label2->Caption = "256 color mode"; break; case 4: Label2->Caption = "16 color mode"; break; case 1: Label2->Caption = "Monochrome mode"; break; default: Label2->Caption = "The screen supports " + IntToStr(1<< BitsPerPixel) + " different colors"; break; } | ||||||
All rights reserved. |