首页 动态创建控件

动态创建控件

小白鼠 2019-05-12 11:51:42 0 2692

以按钮和图片为例

CButton* myDlg::NewMyButton(int nID,CRect rect,int nStyle)
{
	CString m_Caption;
	m_Caption.LoadString( nID ); //取按钮标题
	CButton *p_Button = new CButton();
	ASSERT_VALID(p_Button);
	p_Button->Create( m_Caption, WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | nStyle, rect, this, nID ); //创建按钮

	return p_Button;
}

CStatic*myDlg::NewMyBtm(int nID,CRect rect,int nStyle)
{
	CString m_Caption;
	m_Caption.LoadString( nID ); //取按钮标题
	CStatic *p_Button = new CStatic();
	ASSERT_VALID(p_Button);
	//p_Button->Create( m_Caption, WS_CHILD | WS_VISIBLE|SS_BITMAP | nStyle, rect, this, nID ); //创建按钮
	p_Button->Create( m_Caption,SS_CENTER|WS_CHILD|WS_VISIBLE|SS_BITMAP|SS_CENTERIMAGE | nStyle, rect, this, nID ); //创建按钮
	p_Button->ModifyStyle(0xF,SS_BITMAP);//设置静态控件的样式,使得它可以使用位图
	return p_Button;
}


//设置图片
void myDlg::setBitmap(LPCWSTR bmpPath){
	//CBitmap *mCBmp;
	HBITMAP mHdlBmp;
	//BITMAP m_Bitmap;

	mHdlBmp=(HBITMAP)LoadImage(NULL,bmpPath,IMAGE_BITMAP,bmp_x,bmp_y,LR_CREATEDIBSECTION | LR_LOADFROMFILE);

	p_MyBtm->SetBitmap(mHdlBmp);
	p_MyBtm->ShowWindow(TRUE);
}

控件 · 动态 · 创建 ·

发表评论