【问题标题】:Set MFC dialog form caption设置 MFC 对话框表单标题
【发布时间】:2014-04-04 08:21:40
【问题描述】:

我需要设置对话框表单标题。我试图创建可能使用类向导绑定到标题的 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;
}

【问题讨论】:

    标签: visual-c++ mfc


    【解决方案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();
    }
    

    【讨论】:

    • 类似 Dlg.SetWindowText("BLABLABLA"); ?此代码引发异常
    • 当您确定您的窗口存在时,您必须这样做。正如@ScottMcP-MVP 建议的那样,您可以将它放在 OnInitDialog 中。如果你事先知道你的标题是什么,放一个CString成员变量和一个Set方法来填充它,调用MyDlg dlg之间的方法;和 dlg.DoModal();在您的 OnInitDialog 中,您可以调用 SetWindowText( m_yourMemberVariable );
    【解决方案2】:

    如果您还没有这样做,您首先需要在对话框类中为 OnInitDialog 添加一个覆盖。这是对话框及其控制窗口存在后您可以执行代码的第一个位置。您可以在 OnInitDialog 中放入 SetWindowText(_T("BLABLABLA")) 来设置对话框标题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-29
      • 1970-01-01
      • 2011-07-21
      • 2012-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多