Visual c++ 设置MFC对话框窗体标题

Visual c++ 设置MFC对话框窗体标题,visual-c++,mfc,Visual C++,Mfc,我需要设置对话框窗体标题。我试图创建CString变量,该变量可以通过类向导绑定到标题。但在选择菜单中没有主窗体控件。这样做的方法是什么 这是我的对话形式: #include "stdafx.h" #include "MyDlg.h" #include "afxdialogex.h" // MyDlg dialog IMPLEMENT_DYNAMIC(MyDlg, CDialog) MyDlg::MyDlg(CWnd* pParent /*=NULL*/) : CDialog(M

我需要设置对话框窗体标题。我试图创建CString变量,该变量可以通过类向导绑定到标题。但在选择菜单中没有主窗体控件。这样做的方法是什么

这是我的对话形式:

#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;
}

希望我能正确理解你的问题:

// 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();
}

如果尚未完成此操作,则首先需要在OnInitDialog的dialog类中添加覆盖。这是对话框及其控件窗口存在后,您可以执行代码的第一个位置。您可以将SetWindowText(_T(“BLABLABLA”))放在OnInitDialog中以设置对话框标题。

类似于Dlg.SetWindowText(“BLABLABLA”)?这段代码会引发异常,当您确定窗口存在时,必须执行此操作。正如@ScottMcP MVP所建议的,您可以将其放在OnInitDialog中。如果您事先知道您的标题是什么,请放置一个CString成员变量和一个Set方法来填充它,在MyDlg-dlg之间调用该方法;和dlg.DoModal();在OnInit对话框中,您可以调用SetWindowText(m_yourMemberVariable);