如何在工具栏中放CComboBox控件

来源:互联网 发布:里约奥运 周琦 数据 编辑:程序博客网 时间:2024/05/12 21:12

出处:http://blog.sina.com.cn/s/blog_3fbb32970100effh.html


工具栏上默认的只能放按钮,其实也可以放CComboBox等其它控件。

 

首先,在头文件中定义 CComboBox    m_wndCmbBx; 打开资源视图的工具栏,在需要放置CComboBox控件的地方增加一个按钮,其ID也设置为CComboBox 的 ID。(我们这里设置为ID_COMBOBOX)

 

然后用函数

CToolBar::SetButtonInfo( int nIndex, UINT nID, UINT nStyle, int iImage );

设置该按钮的为分隔条(nStyle=TBBS_SEPARATOR)和宽度(也就是CComboBox控件的宽度,用iImage参数)。

 

最后用函数

CComboBox::Create( DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID );

创建CComboBox控件即可。

 

 

在框架类的return 0;之前,添加以下代码即可。

 

 //在工具条上创建CComboBox控件
 int index=0;
 RECT rect;
 while (m_wndToolBar.GetItemID(index)!=ID_COMBOBOX)
     index++;
    //设置指定工具项的宽度并获取新的区域,80是宽度
 m_wndToolBar.SetButtonInfo(index,ID_COMBOBOX,TBBS_SEPARATOR,80);
    //设置位置
 m_wndToolBar.GetItemRect(index,&rect);
 rect.top+=1;
 rect.bottom+=200;
    //创建并显示控件
 if (!m_wndCmbBx.Create(WS_CHILD|WS_VISIBLE|WS_VSCROLL|CBS_DROPDOWNLIST,rect,&m_wndToolBar,ID_COMBOBOX))
 {
  TRACE0("Failed to create status bar\n");
  return -1;      // fail to create
 }
 m_wndCmbBx.SetItemHeight(-1,14);
 m_wndCmbBx.ShowWindow(SW_SHOW);
 //创建列表项
 m_wndCmbBx.AddString("%30");
 m_wndCmbBx.AddString("%30");
 m_wndCmbBx.AddString("%30");
 m_wndCmbBx.AddString("%30");
 m_wndCmbBx.SetCurSel(0);


原创粉丝点击