如何在工具条上创建一个“组合框”-急 如何在工具条上创建一个“组合框”-急 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://www.vckbase.com/document/viewdoc.asp?id=469 1.从CTOOLBARCTRL派生一个工具栏类,然后在.h和.Cpp中用CToolBar替换CToolBarCtrl2.在工具栏上添加一个按钮.3.在派生工具栏类中增加一个函数如Load()4.为派生类添加一个成员变量CCombobox m_ComboBOOL CXXToolBar::LoadEx(UNIT id){ BOOL bRet; bRet = CXXToolBar::LoadToolBar(id); int pos = commandToIndex(//按钮ID,假设为IDC_BUTTONCOMBO); SetBuffonInfo(pos,IDC_BUTTONCOMBO,TBBS_SEPARATOR,COMBOLEN); CRect rect; GetItemRect(pos,&rect); rect.bottom+= COMBODROP; m_Combo.Create(WS_CHILD|WS+VISIBLE|CBS_DROPDOWNLIST,rect,this,IDC_BUTTONCOMB); return TURE;} 我的方法和楼上差不多。BOOL CMainFrame::CreatePropertyBar(){ const int nDropHeight = 100 ; if(!m_wndPropBar.Create(this, WS_CHILD|WS_VISIBLE|CBRS_TOP| CBRS_SIZE_DYNAMIC|CBRS_TOOLTIPS|CBRS_FLYBY, IDR_PROPERTYBAR)|| !m_wndPropBar.LoadBitmap(IDR_PROPERTYBAR)|| !m_wndPropBar.SetButtons(PropertyButtons,sizeof(PropertyButtons)/sizeof(UINT))) { TRACE0("Failed to create propertybar\n") ; return FALSE ; } //设置 IDW_LINESTYLE m_wndPropBar.SetButtonInfo(1,ID_SEPARATOR, TBBS_SEPARATOR, 12) ; //设置线型组合框的宽度 m_wndPropBar.SetButtonInfo(2,IDW_LINESTYLE , TBBS_SEPARATOR, 150) ; // CRect rc ; /*绘制线型组合框*/ //得到线型组合框的矩形框 m_wndPropBar.GetItemRect(2,&rc) ; rc.bottom = rc.top + nDropHeight ; //创建线型组合框 if(!m_wndPropBar.m_wndLineStyleCmb.Create( CBS_DROPDOWNLIST|WS_VISIBLE|WS_TABSTOP| WS_VSCROLL|CBS_OWNERDRAWVARIABLE, rc, &m_wndPropBar, IDW_LINESTYLE)) { TRACE0("Failed to create linestyle combo--box!") ; return FALSE ; } //在线型组合框添加选项 m_wndPropBar.m_wndLineStyleCmb.AddItem(PS_SOLID, "Solid") ; m_wndPropBar.m_wndLineStyleCmb.AddItem(PS_DASH, "Dash") ; m_wndPropBar.m_wndLineStyleCmb.AddItem(PS_DOT, "Dot") ; m_wndPropBar.m_wndLineStyleCmb.AddItem(PS_DASHDOT, "DashDot") ; m_wndPropBar.m_wndLineStyleCmb.AddItem(PS_DASHDOTDOT, "DashDotDot") ; m_wndPropBar.m_wndLineStyleCmb.AddItem(PS_NULL, "Null") ; m_wndPropBar.m_wndLineStyleCmb.AddItem(PS_INSIDEFRAME, "InsideFrame") ; //设置当前缺省的选项 m_wndPropBar.m_wndLineStyleCmb.SetCurSel(0) ; m_wndPropBar.EnableDocking( CBRS_ALIGN_TOP ); EnableDocking( CBRS_ALIGN_TOP ); DockControlBar(&m_wndPropBar); return TRUE ;}把这个函数家在CMainFrame::OnCreate ,就可以了。注意:m_wndLineStyleCmb是我自定义的类说明的一个对象,你把它换成你需要的就可以了。 如果要响应combo消息的话可以在ToolBar类里面也可以在MainFrame里面直接自己手动写消息映射就可以了因为command消息是有自己的消息发送路径的 先在工具条上定义一个ID(比如:IDR_MYCOMBO), 然后Create一个组合框 我前几天也遇到这样的问题,结果我给他简化处理了,不过我觉得也挺好的,推荐一下。1,创建一个对话,然后增加一个组合框,设置对框为无标题。2,在对应的工具栏按钮的事件中弹出这个对框,作相应的处理。注:1,对话框在OnShowWindow(BOOL bShow, UINT nStatus) 中处理显示位置;2,记得使用 CMainFrame *pFrame=((CMainFrame*)AfxGetMainWnd()); CRect mRect; pFrame->GetWindowRect(&mRect); CRect rect; pFrame->m_wndToolBar.GetItemRect(7,&rect); 非继承法:1、在需要加组合框的位置增加一个button,设定ID(我这里是IDC_COMBO_TEST);2、在button前加一个分隔符(即将button向右拉一下);3、在CMainFrame::OnCreate()工具条创建之后添加如下代码: int nWidth = 100;combobox 宽度 int nHeight = 125;//combobox 下拉高度 // Configure the combo place holder //SetButtonInfo的第一参数为组合框的位置索引(zero base,include seperator) m_wndToolBar.SetButtonInfo(4, IDC_COMBO_TEST, TBBS_SEPARATOR, nWidth); // Get the colorbar height CRect rect; m_wndToolBar.GetItemRect(4, &rect); rect.bottom = rect.top + nHeight; // Create the combo box m_ctlTest.Create(WS_CHILD | WS_VISIBLE | WS_VSCROLL | CBS_DROPDOWNLIST, rect, &m_wndToolBar, IDC_COMBO_TEST); m_ctlTest.AddString("Test1"); m_ctlTest.AddString("Test2"); m_ctlTest.AddString("Test3"); m_ctlTest.AddString("Test4"); m_ctlTest.AddString("Test5"); // TODO: Delete these three lines if you don't want the toolbar to // be dockable m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY); EnableDocking(CBRS_ALIGN_ANY); DockControlBar(&m_wndToolBar);4、自己手动映射combobox的OnSelChange事件。 .h protected: afx_msg void OnSelChangeTest(); //{{AFX_MSG(CMainFrame).cpp//}}AFX_MSG_MAP ON_CBN_SELCHANGE(IDC_COMBO_TEST, OnSelChangeTest)END_MESSAGE_MAP()void CMainFrame::OnSelChangeTest(){ int nIndex =m_ctlTest.GetCurSel(); if (nIndex == CB_ERR) return; CString str;/* switch(nIndex) { case 0: case 1: case 2: case 3: case 4: str.Format("Test%d",nIndex+1); break; default: str.Format("No selected"); }*/ m_ctlTest.GetLBText(nIndex,str); AfxMessageBox(str);} 基于MFC对话框的EXE工程,单步调试不会死机,但运行时会死机,找不到问题出在哪了? 有关mschart的问题 directshow视频播放窗口怎么随picture控件动态变化 求助??? 散分!庆祝“嫦娥一号”发射成功! 如何在菜单栏上面加一个编辑框?(SDK) hook的回调函数 请教一个消息响应的问题 急救,我用MFC编了一个简单上位机出现错误 请问在TC下的源文件如何在VC++中编译? 我给libtiff库增加了JBIG压缩支持 文本编辑框(Edit Box)的属性设置了多行(Multiline),如何在程序里知道输入的文本在哪里换行的?
2.在工具栏上添加一个按钮.
3.在派生工具栏类中增加一个函数如Load()
4.为派生类添加一个成员变量CCombobox m_Combo
BOOL CXXToolBar::LoadEx(UNIT id)
{
BOOL bRet;
bRet = CXXToolBar::LoadToolBar(id);
int pos = commandToIndex(//按钮ID,假设为IDC_BUTTONCOMBO);
SetBuffonInfo(pos,IDC_BUTTONCOMBO,TBBS_SEPARATOR,COMBOLEN);
CRect rect;
GetItemRect(pos,&rect);
rect.bottom+= COMBODROP;
m_Combo.Create(WS_CHILD|WS+VISIBLE|CBS_DROPDOWNLIST,rect,this,IDC_BUTTONCOMB);
return TURE;
}
BOOL CMainFrame::CreatePropertyBar()
{
const int nDropHeight = 100 ;
if(!m_wndPropBar.Create(this, WS_CHILD|WS_VISIBLE|CBRS_TOP|
CBRS_SIZE_DYNAMIC|CBRS_TOOLTIPS|CBRS_FLYBY, IDR_PROPERTYBAR)||
!m_wndPropBar.LoadBitmap(IDR_PROPERTYBAR)||
!m_wndPropBar.SetButtons(PropertyButtons,sizeof(PropertyButtons)/sizeof(UINT)))
{
TRACE0("Failed to create propertybar\n") ;
return FALSE ;
}
//设置 IDW_LINESTYLE
m_wndPropBar.SetButtonInfo(1,ID_SEPARATOR, TBBS_SEPARATOR, 12) ;
//设置线型组合框的宽度
m_wndPropBar.SetButtonInfo(2,IDW_LINESTYLE , TBBS_SEPARATOR, 150) ;
//
CRect rc ;
/*绘制线型组合框*/
//得到线型组合框的矩形框
m_wndPropBar.GetItemRect(2,&rc) ;
rc.bottom = rc.top + nDropHeight ;
//创建线型组合框
if(!m_wndPropBar.m_wndLineStyleCmb.Create(
CBS_DROPDOWNLIST|WS_VISIBLE|WS_TABSTOP|
WS_VSCROLL|CBS_OWNERDRAWVARIABLE,
rc, &m_wndPropBar, IDW_LINESTYLE))
{
TRACE0("Failed to create linestyle combo--box!") ;
return FALSE ;
}
//在线型组合框添加选项
m_wndPropBar.m_wndLineStyleCmb.AddItem(PS_SOLID, "Solid") ;
m_wndPropBar.m_wndLineStyleCmb.AddItem(PS_DASH, "Dash") ;
m_wndPropBar.m_wndLineStyleCmb.AddItem(PS_DOT, "Dot") ;
m_wndPropBar.m_wndLineStyleCmb.AddItem(PS_DASHDOT, "DashDot") ;
m_wndPropBar.m_wndLineStyleCmb.AddItem(PS_DASHDOTDOT, "DashDotDot") ;
m_wndPropBar.m_wndLineStyleCmb.AddItem(PS_NULL, "Null") ;
m_wndPropBar.m_wndLineStyleCmb.AddItem(PS_INSIDEFRAME, "InsideFrame") ;
//设置当前缺省的选项
m_wndPropBar.m_wndLineStyleCmb.SetCurSel(0) ;
m_wndPropBar.EnableDocking( CBRS_ALIGN_TOP );
EnableDocking( CBRS_ALIGN_TOP );
DockControlBar(&m_wndPropBar);
return TRUE ;
}把这个函数家在CMainFrame::OnCreate ,就可以了。注意:m_wndLineStyleCmb是我自定义的类说明的一个对象,你把它换成你需要的就可以了。
因为command消息是有自己的消息发送路径的
1,创建一个对话,然后增加一个组合框,设置对框为无标题。
2,在对应的工具栏按钮的事件中弹出这个对框,作相应的处理。
注:1,对话框在OnShowWindow(BOOL bShow, UINT nStatus) 中处理显示位置;
2,记得使用 CMainFrame *pFrame=((CMainFrame*)AfxGetMainWnd());
CRect mRect;
pFrame->GetWindowRect(&mRect);
CRect rect;
pFrame->m_wndToolBar.GetItemRect(7,&rect);
1、在需要加组合框的位置增加一个button,设定ID(我这里是IDC_COMBO_TEST);
2、在button前加一个分隔符(即将button向右拉一下);
3、在CMainFrame::OnCreate()工具条创建之后添加如下代码:
int nWidth = 100;combobox 宽度
int nHeight = 125;//combobox 下拉高度 // Configure the combo place holder
//SetButtonInfo的第一参数为组合框的位置索引(zero base,include seperator)
m_wndToolBar.SetButtonInfo(4, IDC_COMBO_TEST, TBBS_SEPARATOR, nWidth); // Get the colorbar height
CRect rect;
m_wndToolBar.GetItemRect(4, &rect);
rect.bottom = rect.top + nHeight; // Create the combo box
m_ctlTest.Create(WS_CHILD | WS_VISIBLE | WS_VSCROLL |
CBS_DROPDOWNLIST, rect, &m_wndToolBar, IDC_COMBO_TEST); m_ctlTest.AddString("Test1");
m_ctlTest.AddString("Test2");
m_ctlTest.AddString("Test3");
m_ctlTest.AddString("Test4");
m_ctlTest.AddString("Test5");
// TODO: Delete these three lines if you don't want the toolbar to
// be dockable
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);4、自己手动映射combobox的OnSelChange事件。
.h
protected:
afx_msg void OnSelChangeTest();
//{{AFX_MSG(CMainFrame).cpp
//}}AFX_MSG_MAP
ON_CBN_SELCHANGE(IDC_COMBO_TEST, OnSelChangeTest)
END_MESSAGE_MAP()void CMainFrame::OnSelChangeTest()
{
int nIndex =m_ctlTest.GetCurSel();
if (nIndex == CB_ERR)
return;
CString str;
/* switch(nIndex)
{
case 0:
case 1:
case 2:
case 3:
case 4:
str.Format("Test%d",nIndex+1);
break;
default:
str.Format("No selected");
}
*/ m_ctlTest.GetLBText(nIndex,str);
AfxMessageBox(str);
}