上一篇 | 下一篇

在工具条里面创建 CComboBox

发布: 2008-6-26 14:28 | 作者: admin | 来源: | 查看: 46次

下载本文所附源代码

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 toolbar\n");

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 ED99v 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 ED99v what you see in these blocks of generated code !

ON_WM_CREATE()

//}}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);

}

字号: | 推荐给好友

21/212>

评分:0

我来说两句