2 回答

TA貢獻1801條經驗 獲得超16個贊
剛又想到個idea 再來第三種方法
void CTestDlg::OnButton3()
{
// TODO: Add your control notification handler code here
static calTNT dlg;//想想static int x = 1;是神馬語法現象
static int flag = 0;
if(!flag)
{
dlg.Create(IDD_DIALOG1);
flag = 1;
}
dlg.ShowWindow(SW_SHOW);
}
模特、非模態兩種方法都給你寫一個程序里頭了 vc6 mfc 對話框工程
主對話框類名CTestDlg 彈出的對話框名 calTNT
主對話框上添加一個編輯框 兩個按鈕
編輯框關聯CString類型變量m_TNT2
彈出對話框類 添加一個編輯框 關聯變量CString類型m_TNT1
給彈出對話框的OK按鈕添加響應函數
void calTNT::OnOK()
{
// TODO: Add extra validation here
UpdateData(TRUE);
((CTestDlg *)AfxGetMainWnd())->m_TNT2 = m_TNT1;
AfxGetMainWnd()->UpdateData(FALSE);
CDialog::OnOK();
}
主對話框類 的頭文件和源文件 都加上 彈出對話框類的頭文件
先來模態的,主對話框的button1
void CTestDlg::OnButton1()
{
// TODO: Add your control notification handler code here
calTNT dlg;
dlg.DoModal();
}
很簡單
再來看非模態 給主對話框類添加頭文件類聲明中添加成員指針變量 和析構函數聲明
public:
calTNT *p;
CTestDlg(CWnd* pParent = NULL); // standard constructor
~CTestDlg();
主對話框類源文件 構造函數中將此指針 初始化為NULL
CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTestDlg)
m_TNT2 = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
p = NULL;
}
添加析構函數實現
CTestDlg::~CTestDlg()
{
if(p)
{
delete p;
}
}
最后按鈕2 建立非模態
void CTestDlg::OnButton2()
{
// TODO: Add your control notification handler code here
if(!p)
{
p = new calTNT();
p->Create(IDD_DIALOG1);
}
p->ShowWindow(SW_SHOW);
}
- 2 回答
- 0 關注
- 224 瀏覽
添加回答
舉報