以前參加一次面試有這道題,當時沒有網絡沒有做出來,今天在網上整理了一下,實現如下.
.h中增加下面代碼
//生成的消息映射函數
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnButtonClick(UINT uID);
DECLARE_MESSAGE_MAP()
.cpp中增加下面代碼
BEGIN_MESSAGE_MAP(CVS2008DialogTestDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_BUTTON4, &CVS2008DialogTestDlg::OnBnClickedButton4)
ON_COMMAND_RANGE(10000,10000+10-1,OnButtonClick)
ON_BN_CLICKED(IDC_BUTTON1, &CVS2008DialogTestDlg::OnBnClickedButton1)
END_MESSAGE_MAP()
//動態創建按鈕
CButton* btn;
void CVS2008DialogTestDlg::OnBnClickedButton4()
{
int buttonID=10000;
int n = 10;
btn = new CButton[n];
DWORD dwStyle = WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON;
for(int i = 0; i < 10; i++){
btn[i].Create("1", dwStyle,CRect(i*10,i*10,i*20,i*20),this,buttonID+i);
}
}
//動態刪除按鈕
void CVS2008DialogTestDlg::OnBnClickedButton1()
{
for(int i = 0; i < 10; i++){
btn[i].DestroyWindow();
}
}
我把代碼上傳上去了,這裡是下載地址,不需要積分啊. http://download.csdn.net/detail/wisdomfriend/9053069