在工具條裏面創建 CComboBox

1.
打開工作區的資源標籤,打開工具欄的位圖資源。
在位圖資源上添加一個新的按鈕,
分配標識號爲:ID_BTN_COMBOBOX
要記着這個按鈕的位置 ( 從第一個開始,以0開始計數。分隔符也算一個,本例是第10個)
在以後的CMainFrame::OnCreate  中要調用。

2.
在CMainFrame 類中添加成員變量:
 public:
 CComboBox m_ComboBox;


3.
爲CComboBox 控件添加標識號,通過菜單View | Resource Sysmbols ...
在資源中添加 IDC_TOOL_COMBOBOX 的標識號, 數值(Value) 取默認值。

4.
定位到CMainFrame::OnCreate ,在其中添加創建CComboBox 控件的代碼。

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
 if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
  return -1;
 
 if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
  | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
  !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
 {
  TRACE0("Failed to create toolbarn");
  return -1;      // fail to create
 }
 
 
 CRect rect;
 m_wndToolBar.SetButtonInfo(10, ID_BTN_COMBOBOX, TBBS_SEPARATOR, 160) ;
 m_wndToolBar.GetItemRect(10, &rect);
 
 rect.bottom += 100;

 if ( ! m_ComboBox.Create(CBS_DROPDOWNLIST | WS_VISIBLE | WS_TABSTOP | CBS_AUTOHSCROLL,  rect, &m_wndToolBar, IDC_TOOL_COMBOBOX) )
  return -1 ;
 m_ComboBox.InitStorage(2, 10 );
 m_ComboBox.AddString("變量1 ");
 m_ComboBox.AddString("變量2 ");
 m_ComboBox.SetCurSel(0);
 
 ..........
 
 return 0;
}

5.
定位到   MainFrm.h 中類的定義處,在其中添加消息響應函數的原型。


 //{{AFX_MSG(CMainFrame)
   afx_msg void OnSelchangeCombo() ;
  // NOTE - the ClassWizard will add and remove member functions here.
  //    DO NOT EDIT what you see in these blocks of generated code!
 //}}AFX_MSG
 DECLARE_MESSAGE_MAP()

6.
定位到 MainFrm.cpp 中的消息映射宏的定義處,在其中添加ComboBox 控件的 ON_CBN_CHANGE  消息的消息映射宏。

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
 //{{AFX_MSG_MAP(CMainFrame)
  // NOTE - the ClassWizard will add and remove mapping macros here.
  //    DO NOT EDIT what you see in these blocks of generated code !
 ON_WM_CREATE()

ON_CBN_SELCHANGE(IDC_TOOL_COMBOBOX, OnSelchangeCombo) 
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()

7.
在  MainFrm.cpp 最後添加消息響應函數體:
void CMainFrame::OnSelchangeCombo()
{
 
 CString sTmp;
 Int nIndex;
 nIndex = m_ComboBox.GetCurSel();
 m_ComboBox.GetLBText( nIndex , sTmp);
 MessageBox(sTmp);

}

發佈了0 篇原創文章 · 獲贊 0 · 訪問量 25萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章