2014-04-04 105 views
0

我需要設置對話框表單標題。我試圖創建CString變量,可能綁定到類嚮導的標題。但在選擇菜單中沒有主窗體控件。這樣做的方式是什麼?設置MFC對話框表單標題

這是我的對話形式:

#include "stdafx.h" 
#include "MyDlg.h" 
#include "afxdialogex.h" 


// MyDlg dialog 

IMPLEMENT_DYNAMIC(MyDlg, CDialog) 

MyDlg::MyDlg(CWnd* pParent /*=NULL*/) 
    : CDialog(MyDlg::IDD, pParent) 
    , m_edit(_T("")) 
{ 

} 

MyDlg::~MyDlg() 
{ 
} 

void MyDlg::DoDataExchange(CDataExchange* pDX) 
{ 
    CDialog::DoDataExchange(pDX); 
    DDX_Text(pDX, IDC_EDIT1, m_edit); 
} 


BEGIN_MESSAGE_MAP(MyDlg, CDialog) 
    ON_BN_CLICKED(IDOK, &MyDlg::OnBnClickedOk) 
END_MESSAGE_MAP() 


// MyDlg message handlers 


void MyDlg::OnBnClickedOk() 
{ 

    // TODO: Add your control notification handler code here 
    CDialog::OnOK(); 
    txt=m_edit; 
} 

這是創建對話框代碼:

BOOL CPreparationApp::InitInstance() 
{ 

    MyDlg Dlg; 
//how to tell Dlg to have form caption "BLABLABLA"? 
    Dlg.DoModal(); 


     return TRUE; 
} 

回答

1

希望我已經明白你的問題的正確方法:

// MyDlg.h 
class MyDlg 
{ 
public: // private is fine too if you're OOP nazi but you have to provide a SetDlgCaption method then. 
    CString m_strDlgCaption; 
}; 

// MyDlg.cpp 
BOOL MyDlg::OnInitDialog() 
{ 
    SetWindowText(m_strDlgCaption); 
} 


BOOL CPreparationApp::InitInstance() 
{ 

    MyDlg Dlg; 

    Dlg.m_strDlgCaption = _T("A fancy caption for your dialog"); 
    Dlg.DoModal(); 
} 
+0

類似於Dlg.SetWindowText(「BLABLABLA」); ?此代碼引發異常 – vico

+0

您必須在確定您的窗口存在時執行此操作。正如@ ScottMcP-MVP所建議的,你可以把它放在OnInitDialog中。如果你事先知道你的標題是什麼,請使用一個CString成員變量和一個Set方法來填充它,調用MyDlg dlg之間的方法;和dlg.DoModal();並且在你的OnInitDialog中你可以調用SetWindowText(m_yourMemberVariable); – IssamTP

0

如果你還沒有完成它,你首先需要在OnInitDialog的對話框類中添加一個覆蓋。在對話框及其控制窗口存在之後,這是第一個可以執行代碼的地方。您可以將SetWindowText(_T(「BLABLABLA」))放入OnInitDialog中以設置對話框標題。