Last Updated 2011/05/06
Programming Tips Visual C++ MFC SDI  索 引 
SDIタイトル変更
2004/07/14

AppWizard で作成したアプリケーションは標準で "Untitled-test" と表示される.

CFrameWnd::m_strTitle を NULL しただけでは "-" がついてしまう.

"Untitled-" を消すには以下の方法がある.
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
    cs.style ^= FWS_ADDTOTITLE;    //これでUntitle -が消えます
    return CFrameWnd::PreCreateWindow(cs);
}


アプリケーションのタイトルを任意のものに変更するには,InitInstance() で TRUE を
返す前に
    AfxGetMainWnd()->SetWindowText( _T("任意のタイトル") );
とする.


OnUpdateFrameTitle() をオーバーライドする方法もある.

void CMainFrame::OnUpdateFrameTitle(BOOL bAddToTitle)
{
    ベースクラス::OnUpdateFrameTitle(bAddToTitle);
    SetWindowText( _T("任意のタイトル") );
}



また,"-" と "test" を非表示にするには UpdateFrameTitleForDocument() をオーバーライドする.

class CMainFrame : public CFrameWnd
{
    // オーバライド
    void UpdateFrameTitleForDocument( LPCTSTR lpszDocName );
    virtual void OnUpdateFrameTitle( BOOL bAddToTitle );
};


void CMainFrame::OnUpdateFrameTitle(BOOL bAddToTitle)
{
    if( (GetStyle() & FWS_ADDTOTITLE) == 0 )
        return;     // leave it alone!
    UpdateFrameTitleForDocument(NULL);
}


void CMainFrame::UpdateFrameTitleForDocument(LPCTSTR lpszDocName)
{
    // copy first part of title loaded at time of frame creation
    TCHAR szText[ 256 + _MAX_PATH ];

    if (GetStyle() & FWS_PREFIXTITLE)
    {
        szText[0] = '\0';   // start with nothing
            // get name of currently active view
        if (lpszDocName != NULL)
        {
            lstrcpy(szText, lpszDocName);
                 // add current window # if needed
            if (m_nWindow > 0)
                 wsprintf(szText + lstrlen(szText), _T(":%d"), m_nWindow);
            lstrcat(szText, _T(" - "));
        }
        lstrcat(szText, m_strTitle);
    }
    else
    {
        // get name of currently active view
        lstrcpy(szText, m_strTitle);
        if (lpszDocName != NULL)
        {
            lstrcat(szText, _T(" - "));
            lstrcat(szText, lpszDocName);
                // add current window # if needed
            if (m_nWindow > 0)
                wsprintf(szText + lstrlen(szText), _T(":%d"), m_nWindow);
        }
    }

    // set title if changed, but don't remove completely
    // Note: will be excessive for MDI Frame with maximized child
    SetWindowText(szText);
}


参照
MDIタイトル変更
前後のTips
SDIタイトル変更

DSS ProgrammingTipsCGI Ver2.02