Methods for changing the title of the Visual C ++ window

Source: Internet
Author: User
Some people often ask this question in forums, so today I have sorted out several methods to change the window title, hoping to help these friends.
I. Changes in the title of a Single-Document Interface (SDI) window
First, let's look at the structure of the title bar of the Windows SDI type window. Use the Appwizard of VC to create a single-document project named "sdititle". After the project is created, compile and run the project. The title of the Main Window of the program is as follows:
Document title main window title
1. Document title change
The document title is controlled by the corresponding document class in the project, so we can use the settitle () function to change the document title.
For example, the function of modifying csdititledoc: onnewdocument () is:
Bool csdititledoc: onnewdocument ()
{
If (! Cdocument: onnewdocument ())
Return false;
Settitle ("document title ");
Return true;
}
To run the program, the document title is changed to "document title-sdititle" each time you select the "file" new option in the menu ".
2. Change the title of the Main Window
The title of the main window is the project file name by default. After the sdititle project generated by appwazied is executed, the title bar is in the form of "(document name)-sdititle ". (Please note this sentence)
There are two methods to change the title of the main window:
Method 1: Use resourceview to open project resources and modify the idr_mainframe value in string table. Replace the original "sdititle/n/Ntitle. change document/Ntitle document to "title change example/n/Ntitle. document/Ntitle document ". After this change, the form of the title bar of the main window is "(-Example of title change ".
Method 2: The specific change method is as follows:
Change the member function precreatewindow () of csdimainframe:
Bool cmainframe: precreatewindow (createstruct & CS)
{
M_strtitle = "VC online programming network ";
Return cframewnd: precreatewindow (CS );
}
After this change, the title bar is in the form of "(-VC online programming network ".
Among the two change methods, the second method has a high priority. That is to say, after the m_strtitle value is changed, the value of idr_mainframe does not work.
2. Change the title of the window of the Multi-Document Interface (MDI) Type
In the MDI type window title, the order of the title of the main window and the document is different from that in the SDI type window. Use the Appwizard of VC to create a multi-text block project named "mdititle. After the project is created, compile and run the project. The title of the main window is as follows: the title of the main window (currently open) Document
1. Document title change
The document title of the MDI window is also controlled by the corresponding document class. The change method is the same as that in the SDI window.
Similarly, to change the title, you only need to use the settitle () function in the corresponding document class to change the document label.
Question.
2. Change the title of the Main Window
The method for changing the title of the MDI type main window is similar to that of the SDI type window. You can change the title of the main window by changing the value of idr_mainframe or m_strtitle. Similarly, after the m_strtitle value is changed, the value of idr_mainframe does not work.
3. Change the subwindow title
By default, MFC automatically sets the subwindow title as the document title opened in the subwindow. However, the title of a subwindow is not the same as the title of the document opened by the subwindow title. We can do the following experiments:
(Use resourceview to open the resource, modify the idr_mainframe value in string table to "VC online programming network", and change the title of the main window to "VC online programming network ".
(Change the bool cmdititledoc: onnewdocument () function:
Bool cmdititledoc: onnewdocument ()
{
If (! Cdocument: onnewdocument ())
Return false;
Static int COUNT = 0;
Char message [10];
Wsprintf (message, "% S % d", "document", count );
Settitle (Message );
Count ++;
Return true;
}
Set the document title to "document 0" and "Document 1 "....
(R) change the cchildframe: precreatewindow () function:
Bool cchildframe: precreatewindow (createstruct & CS)
{
CS. Style & = ~ (Long) fws_addtotitle;
Return response ichildwnd: precreatewindow (CS );
}
Change the window type. This step is required. The constant fws_addtotitle is defined in the file/msdev/mfc/include/afxwin. h.
(Add the oninitialupdate () function to the cmdititleview class:
Void cmdititleview: oninitialupdate ()
{
Cview: oninitialupdate ();
Getparent ()-> setwindowtext (getdocument ()-> gettitle () + "-VC window title example ");
} In this way, the title bar of the subwindow is in the form of "document name (-VC window title example ".

The modified program running effect is as follows: the above example can also be used as a reference to modify various headers of the MDI type window.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.