Can not set MFC mainframe title

Ken 46 Reputation points
2020-12-26T12:48:39.257+00:00

Hi, everyone.
I created a office style MFC application in VS2015. I need change the title of main window. I tried by SetWindowTextW, but it doesn't seams to work.
I wrote a test project that only implement a button click. It doesn't work.

void CMainFrame::OnButton2()
{
SetWindowTextW(_T("This is my title"));
}
51180-1.png

Developer technologies C++
0 comments No comments
{count} votes

Accepted answer
  1. RLWA32 49,536 Reputation points
    2020-12-27T12:58:03.173+00:00

    Lets try a different approach. In an Office style MFC SDI application (error checking omitted) -

    void CMainFrame::OnButton2()  
    {  
        SetTitle(_T("This is the new title"));  
        UpdateFrameTitleForDocument(GetActiveView()->GetDocument()->GetTitle());  
    }  
      
    

    Before button click -

    51424-sdibefore.png

    After button click -

    51425-sdiafter.png


2 additional answers

Sort by: Most helpful
  1. David Lowndes 4,726 Reputation points
    2020-12-26T13:20:09.74+00:00

    The title is set by default from resource strings, such as IDR_MAINFRAME; try changing that.


  2. RLWA32 49,536 Reputation points
    2020-12-26T13:54:54.653+00:00

    Try this -

    void CMainFrame::OnButton2()
    {
        SetTitle(_T("This is the new title"));
        RedrawWindow(NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW | RDW_FRAME | RDW_ERASE);
    }
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.