vc 動態創建對話框和按鈕


 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

               }

 

 

 

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"); }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM