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

在工具欄上添加扁平風格下拉框控件

     編輯:關於VC++

效果演示

工具欄扁平下拉框

實現步驟

  1. 創建單文檔專案,資源編輯器添加工具欄按鈕(ID: ID_TOOL_ZOOM),作為佔位符。
  2. 派生 CMainToolBar : public CToolBar,聲明 CFlatComboBox m_wndZoom(使用 Kirk Stowell 的扁平下拉框類)。
  3. MainFrm.h 包含頭文件,將 m_wndToolBar 類型改為 CMainToolBar。
  4. OnCreate 中定位佔位按鈕,設置寬度、獲取矩形區域,在該區域創建下拉框並填充項目。
  5. 資源編輯器將工具欄按鈕高度改為 20 像素,容納下拉框。
  6. 添加 FlatComboBox.h/cpp 到專案,替換 CComboBox 為 CFlatComboBox 實現扁平風格。
  7. 在 CMainFrame 添加 ON_CBN_SELENDOK(ID_TOOL_ZOOM, OnSelectZoomed) 處理選擇變更消息。

關鍵代碼


// OnCreate 末尾
int idx = 0;
while (m_wndToolBar.GetItemID(idx) != ID_TOOL_ZOOM) ++idx;
m_wndToolBar.SetButtonInfo(idx, ID_TOOL_ZOOM, TBBS_SEPARATOR, 80);
CRect rc; m_wndToolBar.GetItemRect(idx, &rc);
rc.top += 2; rc.bottom += 200;
m_wndToolBar.m_wndZoom.Create(WS_CHILD|WS_VISIBLE|CBS_AUTOHSCROLL|CBS_DROPDOWNLIST|CBS_HASSTRINGS, rc, &m_wndToolBar, ID_TOOL_ZOOM);
m_wndToolBar.m_wndZoom.ShowWindow(SW_SHOW);
m_wndToolBar.m_wndZoom.AddString(_T("25%")); // ... 添加 50%-200%
m_wndToolBar.m_wndZoom.SetCurSel(3); // 默認 100%

// 消息處理
void CMainFrame::OnSelectZoomed()
{
    CString str; m_wndToolBar.m_wndZoom.GetWindowText(str);
    AfxMessageBox(str);
}

三大問題解決

問題解決方案
工具欄高度不足資源編輯器調整按鈕 Height=20
下拉框非扁平引入 CFlatComboBox 類替換 CComboBox
無法捕獲選擇消息手動添加 ON_CBN_SELENDOK 映射到 CMainFrame
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved