應用程式在不同色深(16 色、256 色、High Color 16-bit、True Color 32-bit)的螢幕上顯示效果差異大。開發時在高色深環境調整的介面,若在低色深機器執行可能嚴重失真。因此需在程式啟動時偵測系統色深,動態調整介面配色或提示使用者。
function GetSysColorNum: Integer;
var
ScreenDC: HDC;
NumBitsPixel: Integer;
begin
Result := 0;
ScreenDC := GetDC(0); // 取得螢幕裝置內容
NumBitsPixel := GetDeviceCaps(ScreenDC, BITSPIXEL); // 每像素位元數
ReleaseDC(0, ScreenDC); // 釋放 DC
Result := 1 shl NumBitsPixel; // 2^位元數 = 色彩數
end;
GetDC(0):取得整個螢幕的裝置內容(DC)。GetDeviceCaps(DC, BITSPIXEL):回傳每像素色彩位元數(如 4、8、16、24、32)。ReleaseDC:釋放 DC,避免資源洩漏。1 shl NumBitsPixel:左移運算等同 2 的 N 次方,快速計算色彩總數。| BITSPIXEL | 色彩數 | 常見模式 |
|---|---|---|
| 4 | 16 | VGA 16 色 |
| 8 | 256 | 256 色調色盤 |
| 16 | 65,536 | High Color (16-bit) |
| 24 | 16,777,216 | True Color (24-bit) |
| 32 | 4,294,967,296 | True Color + Alpha (32-bit) |
在表單 `OnCreate` 事件中呼叫此函數,若色深低於 16-bit(< 65536),可自動切換至低色深介面模式,或顯示建議訊息:「偵測到系統色深較低,建議調整為 16-bit 以上以獲得最佳顯示效果。」