前言
TScreen 封裝應用程式執行時期的螢幕裝置資訊,提供解析度、字體清單、游標控制、作用中表單/控制項追蹤等功能。C++Builder 自動建立全域實例 Screen,開發者可直接使用。
類別繼承
TObject
└── TPersistent
└── TComponent
└── TScreen
核心屬性分類
1. 解析度與尺寸
| 屬性 | 型別 | 說明 |
| Width / Height | int | 螢幕寬/高(像素) |
| PixelsPerInch | int | 垂直 DPI(通常 96/120/144) |
2. 字體與游標
| 屬性 | 型別 | 說明 |
| Fonts | TStrings* | 系統安裝字體名稱清單(只讀) |
| Cursor | TCursor | 全域游標圖示(設定即時生效) |
| Cursors | TCursors* | 應用程式可用游標集合 |
| IconFont | TFont* | 檔案選取對話框圖示標籤字體 |
3. 表單與資料模組追蹤
| 屬性 | 型別 | 說明 |
| FormCount / CustomFormCount | int | 目前顯示的表單/自訂表單數量 |
| Forms / CustomForms[int Index] | TForm* / TCustomForm* | 索引器存取表單實例 |
| DataModuleCount | int | 已建立實例的資料模組數 |
| DataModules[int Index] | TDataModule* | 資料模組實例索引器 |
4. 焦點與輸入法
| 屬性 | 型別 | 說明 |
| ActiveControl | TWinControl* | 目前擁有輸入焦點的控制項(只讀) |
| ActiveCustomForm | TCustomForm* | 目前作用中的自訂表單 |
| DefaultIme / DefaultKbLayout | HKL / HKL | 預設輸入法編輯器與鍵盤配置 |
| Imes | TStrings* | 系統安裝 IME 清單 |
關鍵方法
| 方法 | 說明 |
| Create / Destroy | 建構/解構(框架自動管理) |
重要事件
| 事件 | 觸發時機 |
| OnActiveControlChange | 輸入焦點切換到新視窗控制項時 |
| OnActiveFormChange | 新表單成為作用中狀態後 |
實戰應用範例
列印所有系統字體
for (int i = 0; i < Screen->Fonts->Count; ++i)
Memo1->Lines->Add(Screen->Fonts->Strings[i]);
暫時變更游標為等待狀態
TCursor oldCursor = Screen->Cursor;
Screen->Cursor = crHourGlass;
try {
DoLengthyOperation();
}
__finally {
Screen->Cursor = oldCursor; // 確保還原
}
遍歷所有開啟表單
for (int i = 0; i < Screen->FormCount; ++i) {
TForm* frm = Screen->Forms[i];
if (frm->Visible)
Log(Format("Form: %s, Caption: %s", [frm->Name, frm->Caption]));
}
取得目前焦點控制項
if (Screen->ActiveControl)
ShowMessage("Focused: " + Screen->ActiveControl->Name);
else
ShowMessage("No control focused");
注意事項
Screen 是全域單例,不可自行建立或刪除
Fonts、Imes 等 TStrings* 屬性為框架管理,勿手動 delete
- 多監視器環境下,
Width/Height 為虛擬螢幕總尺寸,單一監視器資訊需呼叫 Win32 API GetMonitorInfo
- 高 DPI 應用需在 Manifest 宣告
dpiAware,否則取得的尺寸可能經縮放
結語
TScreen 是存取螢幕裝置資訊與追蹤應用程式視覺狀態的核心介面。善用 Screen->Fonts 實現字體選擇器、Screen->Cursor 改善操作回饋、Screen->Forms 實現 MDI 視窗管理等功能,能大幅提升使用者體驗。