1. Create a non-modal dialog box:
Class * object = new class
BOOL object-> Create (ID, this );
Call the ShowWindow function to display the dialog box after creation.
Object-> ShowWindow (SW_SHOW );
When you click OK or cancel in the non-modal dialog box, the dialog box is hidden instead of destroyed. To destroy the dialog box, you must call the DestroyWindow function.
2. Dynamic Creation button:
Method 1 adds a private CButton variable m_btn to the class to which you want to add a button, and also adds a BOOL-type private member m_bIsCreated to determine whether a button is created.
If (m_blsCreated = FALSE) /// determine if no button is created
{
M_btn.Create ("new", //// text displayed on the button
BS_DEFPUSHBUTTON | WS_VISIBLE | WS_CHILD, // If WS_VISIBLE is not specified, you must call ShowWindow to display it.
CRect (100,100, 100,100), // coordinate () in the upper left corner, length:
This,
123); the ID address is 123
M_blsCreated = TRUE;
}
Else
{
M_btn.DestroyWindow ();
M_blsCreated = false;
}
Method 2 use the member object m_hWnd of the CWnd class to save the window handle associated with the window object. If the window object is not associated with any window, the value is NULL.
If (! M_btn.m_hWnd)
{
M_btn.Create ("new", BS_DEFPUSHBUTTON | WS_VISIBLE | WS_CHILD, CRect (100,100, 123), this );
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 isNULL, 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
CallCreateTo create a modeless dialog box using a dialog-box template from a resource. You can put the callCreateInside the constructor or call it after the constructor is invoked.
Two forms ofCreateMember 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. IfPParentWndIsNULL, The dialog box will be created with its parent or owner window set to the main application window.
TheCreateMember function returns immediately after it creates the dialog box.
UseWS_VISIBLEStyle in the dialog-box template if the dialog box shoshould appear when the parent window is created. Otherwise, you must callShowWindow. 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 inClass Library Reference.
UseCWnd: DestroyWindowFunction to destroy a dialog box created byCreateFunction.
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"); }