運行時動態將 `CComboBox` 從 `CBS_DROPDOWN` (可編輯) 切換為 `CBS_DROPDOWNLIST` (僅選擇),無 `SetType` 方法。
// 在需要切換的位置 (如 OnInitDialog、按鈕點擊、配置讀取後)
m_combo.ModifyStyle(CBS_DROPDOWN, CBS_DROPDOWNLIST); // 移除下拉編輯樣式,添加只選樣式
// 必須重建控件窗口使樣式生效
RecreateComboBox(&m_combo);
// 等效手動重建 (參考 MFC 源碼)
void RecreateComboBox(CComboBox* pCombo) {
if (!pCombo->GetSafeHwnd()) return;
CRect rect; pCombo->GetWindowRect(&rect); pCombo->GetParent()->ScreenToClient(&rect);
DWORD style = pCombo->GetStyle();
DWORD exStyle = pCombo->GetExStyle();
CFont* pFont = pCombo->GetFont();
CStringArray items; // 保存現有項目
for (int i = 0; i < pCombo->GetCount(); ++i) {
CString s; pCombo->GetLBText(i, s); items.Add(s);
}
int curSel = pCombo->GetCurSel();
pCombo->DestroyWindow();
pCombo->Create(style, rect, pCombo->GetParent(), pCombo->GetDlgCtrlID());
pCombo->SetFont(pFont);
for (const auto& s : items) pCombo->AddString(s);
pCombo->SetCurSel(curSel);
}
核心口訣:**ModifyStyle 改樣式,RecreateComboBox 重建窗口,保存內容恢復選擇**。