#define ID_MYEDIT 1820
#include <afxwin.h>
#include "Resource.h"
#include "dlg.h"
//构造函数生成CDialog的子类
CDlg::CDlg(CWnd* pParent) : CDialog(IDD_DIALOG, pParent)
{
}
void CDlg::OnCreate()
{
CWnd *wnd;
CRect rect;
//判断EDIT控件是否生成
if (!(wnd =GetDlgItem(ID_MYEDIT)))
{
//生成EDIT控件
if (m_MyEdit.CreateEx(WS_EX_CLIENTEDGE, _T("EDIT"), NULL,
ES_AUTOHSCROLL | ES_LEFT | WS_CHILD,
5,5,130,20, this->m_hWnd,(HMENU) ID_MYEDIT) == NULL)
{
MessageBox(TEXT("未能创建EDIT控件"), TEXT("提示窗口"));
}
else
{
//显示EDIT控件
m_MyEdit.ShowWindow(SW_SHOW);
//将焦点放在EDIT控件上
m_MyEdit.SetFocus();
}
rect = CRect(5,30,135,50);
//生成BUTTON控件
if (m_MyButton.CreateEx(0, _T("BUTTON"), "动态添加控件",
BS_AUTOCHECKBOX | BS_LEFTTEXT | WS_CHILD,
rect, this, ID_MYEDIT) == NULL)
{
MessageBox(TEXT("未能创建BUTTON控件"), TEXT("提示窗口"));
}
else
{
//显示BUTTON控件
m_MyButton.ShowWindow(SW_SHOW);
}
}
}
void CDlg::OnDelete()
{
CWnd *wnd;
//判断控件是否生成
while (wnd =GetDlgItem(ID_MYEDIT))
//销毁控件
wnd->DestroyWindow() ;
}
void CDlg::OnExit()
{
//调用父类关闭窗口方法
CDialog::OnCancel();
}
BEGIN_MESSAGE_MAP(CDlg, CDialog)
//点击动态生成按钮事件
ON_BN_CLICKED(IDC_CREATE, OnCreate)
//点击删除控件按钮事件
ON_BN_CLICKED(IDC_DELETE, OnDelete)
//点击退出按钮事件
ON_BN_CLICKED(IDC_EXIT, OnExit)
END_MESSAGE_MAP()
评论1
最新资源