3.2.2 工具栏上动态创建控件combobox


p120,单文档

// MainFrm.h : interface of the CMainFrame class
//
/

#if !defined(AFX_MAINFRM_H__B9DC682A_7F5D_42C3_99E1_724FCC8FF3C6__INCLUDED_)
#define AFX_MAINFRM_H__B9DC682A_7F5D_42C3_99E1_724FCC8FF3C6__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class CMainFrame : public CFrameWnd
{
	
protected: // create from serialization only
	CMainFrame();
	DECLARE_DYNCREATE(CMainFrame)
	CComboBox m_comBox;
// Attributes
public:

// Operations
public:

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CMainFrame)
	virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
	//}}AFX_VIRTUAL

// Implementation
public:
	virtual ~CMainFrame();
#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif

protected:  // control bar embedded members
	CStatusBar  m_wndStatusBar;
	CToolBar    m_wndToolBar;

// Generated message map functions
protected:
	//{{AFX_MSG(CMainFrame)
	afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
	afx_msg void OnSelchangeCombo1();
		// 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()
};

/

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_MAINFRM_H__B9DC682A_7F5D_42C3_99E1_724FCC8FF3C6__INCLUDED_)


// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "dds.h"

#include "MainFrm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/
// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

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_MYCOMBOX, OnSelchangeCombo1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

/
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	
}

CMainFrame::~CMainFrame()
{
}

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
	}

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	// 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);
	CToolBarCtrl& toolCtrl=m_wndToolBar.GetToolBarCtrl();
	
	int iCount=toolCtrl.GetButtonCount();
	m_wndToolBar.SetButtonInfo(iCount-1,ID_SEPARATOR,TBBS_SEPARATOR,100);

	CRect r;
	m_wndToolBar.GetItemRect(iCount-1,&r);//r的位置确定了
	r.bottom=r.top+120;//指定r的高度

	if(!m_comBox.Create(CBS_DROPDOWN|WS_VISIBLE|WS_VSCROLL|CBS_AUTOHSCROLL,r,&m_wndToolBar,IDC_MYCOMBOX))
	MessageBox("failed to create combobox");
	else
	{
		m_comBox.AddString("first");
		m_comBox.AddString("second");
				m_comBox.AddString("third");
	}
	m_comBox.SetCurSel(0);	
	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return TRUE;
}

/
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG

/
// CMainFrame message handlers

void CMainFrame::OnSelchangeCombo1() 
{
	// TODO: Add your control notification handler code here
	int nSel=m_comBox.GetCurSel();
	CString strMess;
	strMess.Format("selected %d",nSel+1);
	MessageBox(strMess);
}
1.在工具栏上添加一个按钮,以做新生成的combobox的位置。当然不添加也可以,可以把create的combox放在其他按钮的位置,比如打开,保存de.
2.获取一个资源符号。view->resource symbol,选择new,填入名称IDC_MYCOMBOX。之后会在resource.h中多出一行
#define IDC_MYCOMBOX                    101   //看来 IDC_MYCOMBOX就是一个数字
3.为类CMainFrame增加一个CComboBox 型成员,    CComboBox  m_comBox;
4.使用如下方式生成资源,即创建窗口,即m_comBox的外形,并指定生成的资源id为IDC_MYCOMBOX,即第2步申请的资源id。
 m_comBox.Create(CBS_DROPDOWN|WS_VISIBLE|WS_VSCROLL|CBS_AUTOHSCROLL,r,&m_wndToolBar,IDC_MYCOMBOX)
此步将对象和资源绑定了。&m_wndToolBar指父窗口是工具栏, r表达了控件在父窗口中的位置和控件的尺寸

其实资源id可以任意指定一个未用到的数字,比如直接指定666也可以,那么这个资源的id就是666了。但为了防止重复,一般用第2步由系统生成。
5.添加消息映射
在CMainFrame的头文件里添加
    afx_msg void OnSelchangeCombo1();
在CMainFrame的实现文件里添加

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_MYCOMBOX, OnSelchangeCombo1)//---添加的,此处资源id: IDC_MYCOMBOX和事件处理函数关联起来了。
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

void CMainFrame::OnSelchangeCombo1()
{
    // TODO: Add your control notification handler code here
    int nSel=m_comBox.GetCurSel();
    CString strMess;
    strMess.Format("selected %d",nSel+1);
    MessageBox(strMess);
}

以上是动态生成一个控件(combobox),看一下如果是从工具箱里拖到窗体上的combobox,ide自动给我们做了什么?
还是用上面的那个单文档吧,用它的about对话框。
a.从工具箱里拖到about窗体上一个combobox控件,在 resource.h中多出一行--对应2
#define ID_COMBOX                       32771
b.按ctrl+w为这个资源关联一个CComboBox 型对象m_comBox1,则会多出一个类成员CComboBox    m_comBox1;---对应3
c.并在DoDataExchange函数里面讲对象和资源绑定了。
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CAboutDlg)
    DDX_Control(pDX, IDC_COMBO1, m_comBox1);----对应4
    //}}AFX_DATA_MAP
}
d.该控件添加消息映射,按ctrl+w,在message maps标签页,为classname选CAboutDlg,objectID选ID_COMBOX,则在
message里会出现一些可以添加的CBN消息,双击CBN_SELCHANGE,则添加了一个消息。对应添加的代码同上5.------对应5.

可以看出,动态创建和编译前静态创建控件(即从工具箱中拖de)的原理都是一样的。

CButton的Create成员函数
CButton::Create 
BOOL Create( LPCTSTR lpszCaption, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID );

Return Value

Nonzero if successful; otherwise 0.

Parameters

lpszCaption

Specifies the button control’s text.

dwStyle

Specifies the button control’s style. Apply any combination of button styles to the button.

rect

Specifies the button control’s size and position. It can be either a CRect object or a RECT structure.

pParentWnd

Specifies the button control’s parent window, usually a CDialog. It must not be NULL.

nID

Specifies the button control’s ID.

Remarks

You construct a CButton object in two steps. First call the constructor, then call Create, which creates the Windows button control and attaches it to the CButton object.

If the WS_VISIBLE style is given, Windows sends the button control all the messages required to activate and show the button.

Apply the following window styles to a button control: 

WS_CHILD   Always


WS_VISIBLE   Usually


WS_DISABLED   Rarely


WS_GROUP   To group controls


WS_TABSTOP   To include the button in the tabbing order 
Example

CButton myButton1, myButton2, myButton3, myButton4;

// Create a push button.
myButton1.Create(_T("My button"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, 
   CRect(10,10,100,30), pParentWnd, 1);

// Create a radio button.
myButton2.Create(_T("My button"), WS_CHILD|WS_VISIBLE|BS_RADIOBUTTON, 
   CRect(10,40,100,70), pParentWnd, 2);

// Create an auto 3-state button.
myButton3.Create(_T("My button"), WS_CHILD|WS_VISIBLE|BS_AUTO3STATE, 
   CRect(10,70,100,100), pParentWnd, 3);

// Create an auto check box.
myButton4.Create(_T("My button"), WS_CHILD|WS_VISIBLE|BS_AUTOCHECKBOX, 
   CRect(10,100,100,130), pParentWnd, 4);



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值