首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> Delphi

Delphi 獲取系統顏色位數:GetDeviceCaps 實現

     編輯:Delphi

函數實現

function GetSysColorNum: Integer;
var
  ScreenDC: HDC;
  NumBitsPixel: Integer;
begin
  Result := 0;
  ScreenDC := GetDC(0);
  try
    NumBitsPixel := GetDeviceCaps(ScreenDC, BITSPIXEL);
    Result := 1 shl NumBitsPixel; // 2^位數 = 顏色數
  finally
    ReleaseDC(0, ScreenDC);
  end;
end;

原理解析

  • GetDC(0) 獲取螢幕 DC。
  • GetDeviceCaps(..., BITSPIXEL) 返回每像素位數 (如 4/8/16/24/32)。
  • 1 shl n 計算 2^n,即顏色總數 (16/256/65536/16M/4G)。

常見返回值對照

應用建議

  • 程序啟動時檢測,若 < 16 位提示用戶調整顯示設置。
  • 根據顏色位數動態選擇圖片資源 (8位用索引圖,16位+用真彩色)。
  • 繪圖代碼中根據位數選擇優化路徑。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved
BitsPixel顏色數典型模式
416VGA 16 色
8256256 色調色板
1665,536High Color (565/555)
2416,777,216True Color
324,294,967,29632-bit (含 Alpha)