您的位置:首页 > 其它

工具栏上创建ComboBox组合框控件,框架响应消息

2013-08-08 14:18 288 查看
点击打开链接

由于业务需要,在原有程序的基础上需要增加几项新的功能。主程序是没有顶部菜单的MDI窗口,考虑在多视环境下的操作实现比较麻烦,故采用主框架响应组合框的消息函数。

创建ComboBox控件

步骤:

1〉在资源里添加一个按钮,命名为ID_COMMBO。

2〉、添加新类class CComboToolBar,

class CComboToolBar : public CToolBar

{

public:

CComboBox m_comboBox; //手动添加

};

我的方法是:向导-〉MFC类-〉CToolBarCtrl,然后将CToolBarCtrl改为CToolBar即可。

3〉、在框架类的OnCreate()函数中添加创建代码。

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)

{

int index = 0;

CRect rect;

while (m_wndToolBar.GetItemID(index) != ID_COMMBO) index++;

m_wndToolBar.SetButtonInfo(index,ID_COMMBO,TBBS_SEPARATOR,112);

m_wndToolBar.GetItemRect(index,&rect);

rect.InflateRect(0, 0, 0, 100);

if (!m_wndToolBar.m_comboBox.Create(WS_CHILD|WS_VISIBLE|CBS_AUTOHSCROLL|

CBS_DROPDOWNLIST|CBS_HASSTRINGS,

rect, &m_wndToolBar, ID_COMMBO))

{

TRACE0(“Failed to create combo-boxn”);

return FALSE;

}

m_wndToolBar.m_comboBox.ShowWindow(SW_SHOW);

m_wndToolBar.m_comboBox.AddString(“单点多日”);

m_wndToolBar.m_comboBox.AddString(“多点单日”);

m_wndToolBar.m_comboBox.AddString(“月度统计”);

m_wndToolBar.m_comboBox.AddString(“年度统计”);

m_wndToolBar.m_comboBox.SetCurSel(0);

/*********方法二****************************/

// CString item[4] = {“单点多日”,”多点单日”,”月度统计”,”年度统计”};

// for(int i=0; i<4; i++)

// m_wndToolBar.m_comboBox.AddString(item[i]);

// m_wndToolBar.m_comboBox.SetCurSel(0);

}

记得在框架类中加上 #include “CComboToolBar.h”

4>、在框架中添加消息函数

由于组合框是动态创建的,所以消息函数只能手动添加

//MainFrame.h

//{{AFX_MSG(CMainFrame)

afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);

afx_msg void OnButtonui();

//}}AFX_MSG

afx_msg void OnDropdown();

DECLARE_MESSAGE_MAP()

//MainFrame.cpp

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)

//{{AFX_MSG_MAP(CMainFrame)

//}}AFX_MSG_MAP

ON_CBN_SELCHANGE(ID_COMMBO,OnDropdown)

END_MESSAGE_MAP()

void CMainFrame::OnDropdown()

{

int m_nMode = m_wndToolBar.m_comboBox.GetCurSel();

CString ss;

ss.Format(“nSel=%d”,nSel);

AfxMessageBox(ss);

}

OK,大功告成。
http://blog.csdn.net/shunan/article/details/1471647
今天终于把这个给搞清楚了,之前那个图形编辑器因为不知道如何往toolbar中添加combobox,因此就没做场景的同比扩大,与缩小。现在终于明白如何用sdk实现此功能了,并且实现相关的combobox消息。说起来也是巧合,中午看到一个同学用mfc实现的把combobox加到toolbar中,发现也就这么回事,只需要设置combobox的父窗口是toolbar窗口就行了。但是当时的问题是如何接受到从combobox中得到的消息,因为主窗口是combobox的父亲的父亲,所以发送的WM_COMMAND ,wParam,lParam
主窗口无法接受。而那家伙的mfc程序也没有实现此功能,本来还可以根据它实现一下。正在苦思冥想中,突然眼前豁然开朗,msdn中的toolbar control 一块出现了如下语句:(介绍如何实现主窗口得到combobox改变选择后的消息)

You might want the edit control notifications to go to another window, such as the toolbar's parent. To do this, create the edit control as a child of the toolbar's parent window. Then change the parent of the edit control to the toolbar. Notifications go to
the original parent; therefore, the edit control messages go to the parent of the toolbar and yet the edit window resides in the toolbar window. The following code example demonstrates this.
// Create the edit control. Notice that hWndParent, parent of
// the toolbar, is used as the parent of the edit control.
hWndEdit = CreateWindowEx(0L, "Edit", NULL, WS_CHILD | WS_BORDER
| WS_VISIBLE | ES_LEFT | ES_AUTOVSCROLL | ES_MULTILINE,
0, 0, cx_edit, cy_edit, hWndParent, (HMENU) IDM_EDIT, hInst, 0 );

// Set the toolbar window as the parent of the edit control
// window. You must set the toolbar as the parent of the edit
// control for it to appear embedded in the toolbar.
SetParent (hWndEdit, hWndToolbar);
////以下是combobox的
hCombobox = CreateWindow(TEXT("combobox"),NULL,WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST,//!!!!
400,30,50,100,hWnd,(HMENU)IDM_COMBOX,hInst,NULL);
for(i=0;i<3;i++)
{
SendMessage(hCombobox,CB_ADDSTRING,0,(LPARAM)str[i]);
}
SendMessage(hCombobox,CB_SETCURSEL,1,0);
SetParent(hCombobox,hToolBar);
这样就可以让hWnd窗口接受来自combobox的消息了
这么看来在createwindow 中设立的父窗口 和用setparent设定的貌似有一点区别阿,正在研究中,目前还不知道。
现在都2007年了,跨年度的发现阿,真是值得纪年一下
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: