动态创建按钮

一.完全动态
先为某个类添加一个public成员    CButton  m_btn;                   
再为这个对象create一个资源:
                          m_btn.Create("new",/按钮上显示的文本
                              BS_DEFPUSHBUTTON|WS_VISIBLE|WS_CHILD,///如果没有制定WS_VISIBLE还要调用ShowWindow将其显示出来
                              CRect(0,0,100,100),/左上角的坐标(0,0),长度为100,100
                              this,//父窗口
                              123);
说明①123是资源id,但一般可用如下方式获取一个资源符号。view->resource symbol,选择new,填入名称IDC_xxx。之后会在resource.h中多出一行。。
②用 CRect(0,0,100,100)来指定按钮在父窗口中的位置,左上角(0,0),右下角(100,100)
CComboBox::Create
BOOL Create( DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID );

Return Value

Nonzero if successful; otherwise 0.

Parameters

dwStyle

Specifies the style of the combo box. Apply any combination of combo-box styles to the box.  

rect

Points to the position and size of the combo box. Can be a RECT structure or a CRect object.

pParentWnd

Specifies the combo box’s parent window (usually a CDialog). It must not be NULL.

nID

Specifies the combo box’s control ID.

Remarks

You construct a CComboBox object in two steps. First call the constructor, then call Create, which creates the Windows combo box and attaches it to the CComboBox object. 

When Create executes, Windows sends the WM_NCCREATE, WM_CREATE, WM_NCCALCSIZE, and WM_GETMINMAXINFO messages to the combo box. 

These messages are handled by default by the OnNcCreate, OnCreate, OnNcCalcSize, and OnGetMinMaxInfo member functions in the CWnd base class. To extend the default message handling, derive a class from CComboBox, add a message map to the new class, and override the preceding message-handler member functions. Override OnCreate, for example, to perform needed initialization for a new class.

Apply the following window styles to a combo-box control. : 

WS_CHILD   Always


WS_VISIBLE   Usually


WS_DISABLED   Rarely


WS_VSCROLL   To add vertical scrolling for the list box in the combo box


WS_HSCROLL   To add horizontal scrolling for the list box in the combo box


WS_GROUP   To group controls


WS_TABSTOP   To include the combo box in the tabbing order 
Example

// pParentWnd is an external pointer to the parent window.
extern CWnd* pParentWnd;
// The pointer to my combo box.
extern CComboBox* pmyComboBox;

pmyComboBox->Create(
      WS_CHILD|WS_VISIBLE|WS_VSCROLL|CBS_DROPDOWNLIST,
      CRect(10,10,200,100), pParentWnd, 1);


二.用ctrl+w为某个button资源关联一个cbutton类型的成员(对象)。--一般的用法

三.为派生类的一个对象关联已经存在的button资源。---在dialog项目里。
如果是已经存在的类,比如    CBitmapButton,可以用一下方法①
1.拖到对话框一个button,id是IDC_BUTTON1,勾选owner draw和bitmap属性。insert一个位图资源,id是IDB_BITMAP1。
2.在xxDlg.h为类CxxDlg添加public成员  CBitmapButton m_bt1;
3.在xxDlg.cpp中OnInitDialog函数中,添加
    m_bt1.LoadBitmaps(IDB_BITMAP1);
    m_bt1.SubclassDlgItem(IDC_BUTTON1,this);//CBitmapButton 型对象关联资源IDC_BUTTON1
    m_bt1.SizeToContent();//
也可以如下
1.拖到对话框一个button,id是IDC_BUTTON2,勾选owner draw属性。
2.在xxDlg.h为类CxxDlg添加public成员  CBitmapButton m_bt2;
3.ctrl+w为了CxxDlg添加消息WM_DRAWITEM
void CGghDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
	// TODO: Add your message handler code here and/or call default	
	if (IDC_BUTTON2==nIDCtl)//筛选出IDC_BUTTON2
	{
		CDC dc;
		dc.Attach(lpDrawItemStruct->hDC);

		CWnd* pWnd=GetDlgItem(IDC_BUTTON2);//取得IDC_BUTTON2指针
		CRect r;
		CString strCaption;
		if (NULL!=pWnd)	{
			pWnd->GetClientRect(&r);//使r代表按钮在父窗口中的位置和尺寸
			pWnd->GetWindowText(strCaption);//取得按钮上的文字
		}
		dc.SetTextColor(RGB(255,0,0));//使按钮上文字的颜色为red
		dc.DrawText(strCaption,&r,DT_CENTER|DT_VCENTER);//在按钮上重写文字

	}
	CDialog::OnDrawItem(nIDCtl, lpDrawItemStruct);
}

如果是自己新建的类,比如    CMyButton,可以用一下方法

1.拖到对话框一个button,id是IDC_BUTTON3.
2.ctrl+w新建一个类 CMyButton,继承于CButton,
3.ctrl+w在member标签页的class name选择CxxDlg,Control IDs选择IDC_BUTTON3,为IDC_BUTTON3添加一个control对象,类型是CMyButton。此时会在xxDlg.h中类CxxDlg多出一个成员
    CMyButton    m_mybt3;
并在CxxDlg的DoDataExchange函数中多出
    DDX_Control(pDX, IDC_BUTTON3, m_mybt3);//使得MyButton型对象关联到资源IDC_BUTTON3
4.ctrl+w在messages标签页,class name选择CMyButton,object id框只有一个CMybutton,messages框有很多CMybutton类可用的虚拟函数和消息。


可见,在vc里面,
①一个窗口对象(由CWnd派生下来的类的),如果要显示出来,除了要创建这个对象本身以外,还要为该对象关联一个资源(资源用资源ID表示)。关联的方法有几种,
可以是m_object.create()创建
可以是load
可以是SubclassDlgItem
可以是attach
等,不同的类(对象)有不同的用于关联资源的成员函数
②资源可以不绑定对象,也可响应消息。
③ctrl+w在message框中,可以为类添加改写的虚函数,和消息,可以为窗口里的某个控件ID添加控件通知消息(这个消息发生后需要通知父窗口)。所以要为某个控件改写虚函数和消息,可以创建一个派生类,然后ctrl+w中就会出现此类就可以改写了,然后就用该类创建的对象去关联一个资源。
refer to
http://blog.csdn.net/halibobo520/article/details/3371955
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值