vc 动态创建对话框和按钮
2012-03-06 10:28
645 查看
1.创建非模态对话框:
类 *对象=new 类
BOOL 对象->Create(ID,this);
创建后需调用ShowWindow函数将对话框显示出来
对象->ShowWindow(SW_SHOW);
在非模态对话框中点击确定和取消时,对话框并不销毁,而是隐藏起来,要想销毁,需调用DestroyWindow函数
2.动态创建按钮:
方法1为要加按钮的类添加一个私有的CButton成为变量m_btn,还要添加一个BOOL型的私有成员量m_bIsCreated用来确定是否创建了按钮
if(m_blsCreated==FALSE)////判断如果没有创建按钮
{
m_btn.Create("new",/////按钮上显示的文本
BS_DEFPUSHBUTTON|WS_VISIBLE|WS_CHILD,///如果没有制定WS_VISIBLE还要调用ShowWindow将其显示出来
CRect(0,0,100,100),/////左上角的坐标(0,0),长度为100,100
this,
123);ID地址为123
m_blsCreated=TRUE;
}
else
{
m_btn.DestroyWindow();
m_blsCreated=false;
}
方法2用CWnd类的成员对象m_hWnd用来保存与窗口对象相关联的窗口句柄,如果窗口对象没有与任何窗口相关联,该值为NULL
if(!m_btn.m_hWnd)
{
m_btn.Create("new",BS_DEFPUSHBUTTON|WS_VISIBLE|WS_CHILD,CRect(0,0,100,100),this,123);
m_blsCreated=TRUE;
}
else
{
m_btn.DestroyWindow();
m_blsCreated=false;
}
BOOL Create( UINT nIDTemplate, CWnd* pParentWnd = NULL );
Return Value
Both forms return nonzero if dialog-box creation and initialization were successful; otherwise 0.
Parameters
lpszTemplateName
Contains a null-terminated string that is the name of a dialog-box template resource.
pParentWnd
Points to the parent window object (of type CWnd) to which the dialog object belongs. If it is NULL, the dialog object’s parent window is set to the main application window.
nIDTemplate
Contains the ID number of a dialog-box template resource.
Remarks
Call Create to create a modeless dialog box using a dialog-box template from a resource. You can put the call to Create inside the constructor or call it after the constructor is invoked.
Two forms of the Create member function are provided for access to the dialog-box template resource by either template name or template ID number (for example, IDD_DIALOG1).
For either form, pass a pointer to the parent window object. If pParentWnd is NULL, the dialog box will be created with its parent or owner window set to the main application window.
The Create member function returns immediately after it creates the dialog box.
Use the WS_VISIBLE style in the dialog-box template if the dialog box should appear when the parent window is created. Otherwise, you must call ShowWindow. For further dialog-box styles and their application, see the http://technet.microsoft.com/zh-cn/library/ms645394structure in the Win32 SDK documentation and Window Styles in the Class Library Reference.
Use the CWnd::DestroyWindow function to destroy a dialog box created by the Create function.
Example
类 *对象=new 类
BOOL 对象->Create(ID,this);
创建后需调用ShowWindow函数将对话框显示出来
对象->ShowWindow(SW_SHOW);
在非模态对话框中点击确定和取消时,对话框并不销毁,而是隐藏起来,要想销毁,需调用DestroyWindow函数
2.动态创建按钮:
方法1为要加按钮的类添加一个私有的CButton成为变量m_btn,还要添加一个BOOL型的私有成员量m_bIsCreated用来确定是否创建了按钮
if(m_blsCreated==FALSE)////判断如果没有创建按钮
{
m_btn.Create("new",/////按钮上显示的文本
BS_DEFPUSHBUTTON|WS_VISIBLE|WS_CHILD,///如果没有制定WS_VISIBLE还要调用ShowWindow将其显示出来
CRect(0,0,100,100),/////左上角的坐标(0,0),长度为100,100
this,
123);ID地址为123
m_blsCreated=TRUE;
}
else
{
m_btn.DestroyWindow();
m_blsCreated=false;
}
方法2用CWnd类的成员对象m_hWnd用来保存与窗口对象相关联的窗口句柄,如果窗口对象没有与任何窗口相关联,该值为NULL
if(!m_btn.m_hWnd)
{
m_btn.Create("new",BS_DEFPUSHBUTTON|WS_VISIBLE|WS_CHILD,CRect(0,0,100,100),this,123);
m_blsCreated=TRUE;
}
else
{
m_btn.DestroyWindow();
m_blsCreated=false;
}
CDialog::Create
BOOL Create( LPCTSTR lpszTemplateName, CWnd* pParentWnd = NULL );BOOL Create( UINT nIDTemplate, CWnd* pParentWnd = NULL );
Return Value
Both forms return nonzero if dialog-box creation and initialization were successful; otherwise 0.
Parameters
lpszTemplateName
Contains a null-terminated string that is the name of a dialog-box template resource.
pParentWnd
Points to the parent window object (of type CWnd) to which the dialog object belongs. If it is NULL, the dialog object’s parent window is set to the main application window.
nIDTemplate
Contains the ID number of a dialog-box template resource.
Remarks
Call Create to create a modeless dialog box using a dialog-box template from a resource. You can put the call to Create inside the constructor or call it after the constructor is invoked.
Two forms of the Create member function are provided for access to the dialog-box template resource by either template name or template ID number (for example, IDD_DIALOG1).
For either form, pass a pointer to the parent window object. If pParentWnd is NULL, the dialog box will be created with its parent or owner window set to the main application window.
The Create member function returns immediately after it creates the dialog box.
Use the WS_VISIBLE style in the dialog-box template if the dialog box should appear when the parent window is created. Otherwise, you must call ShowWindow. For further dialog-box styles and their application, see the http://technet.microsoft.com/zh-cn/library/ms645394structure in the Win32 SDK documentation and Window Styles in the Class Library Reference.
Use the CWnd::DestroyWindow function to destroy a dialog box created by the Create function.
Example
CMyDialog* pDialog; void CMyWnd::OnSomeAction() { //pDialog initialized to NULL in the constructor of CMyWnd class pDialog = new CMyDialog(); //Check if new succeeded and we got a valid pointer to a dialog object if(pDialog != NULL) { BOOL ret = pDialog->Create(IDD_MYDIALOG,this); if(!ret) //Create failed. AfxMessageBox("Error creating Dialog"); pDialog->ShowWindow(SW_SHOW); } else AfxMessageBox("Error Creating Dialog Object"); }
相关文章推荐
- vc 动态创建对话框和按钮
- VC 动态创建按钮
- 对话框和MDI中动态创建按钮CButton,并响应事件
- MFC动态创建对话框中的按钮控件并创建其响应消息
- 对话框和MDI中动态创建按钮CButton,并响应事件
- VC动态创建工具栏及工具栏上的按钮及为按钮添加消息响应函数
- vc创建带新建文件夹按钮的保存对话框
- VC2010 单文档动态创建按钮和响应函数 (请CSDN改进博客支持截图)
- VC下动态创建了BUTTON按钮后,如何设置按钮的Modal Frame风格???
- VC 动态创建按钮
- 在VC中,让动态创建的ActiveX控件响应Windows消息[转]
- Android: 创建一个AlertDialog对话框,必须按确定或取消按钮才能关闭对话框,禁止按[返回键]或[搜索键]关闭
- 【MFC动态创建按钮,并在按钮上实现位图的切换显示】
- VC动态创建控件以及动态创建
- VC动态创建控件
- VC++动态生成带图像的按钮
- VC++中将对话框的背景设为自己想要的图片,增加打开文件按钮
- SDK动态创建按钮
- vc对话框关闭按钮和取消按钮效果一样的原因
- vs2008中,创建基于对话框的mfc动态库步骤