Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Visual C++ and MFC Fundamentals Chapter 15: Fundamental Controls


CDialog::OnInitDialog();

// TODO: Add extra initialization here
SetWindowText("Windows Fundamentals");

return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}

Another technique you can use consist of first declaring a null-terminated string variable,
assign it a value, and then pass it the lpszString argument to the SetWindowText()
function.

If you are using resources in your MFC application, you can also create a global value in
the string table to be used as the window name:

You can call the string of such an identifier, store it in a CString variable, and then pass
it to the CWnd::SetWindowText() method. Here is an example:

CMainFrame::CMainFrame()
{
// Declare a window class variable
WNDCLASS WndCls;
const char *StrWndName = "Application Name";

...


const char *StrClass = AfxRegisterWndClass(WndCls.style, WndCls.hCursor,
WndCls.hbrBackground, WndCls.hIcon);

Create(StrClass, StrWndName);

CString Str;
Str.LoadString(IDS_CURAPPNAME);
SetWindowText(Str);
}

To change the name of a window, instead of calling SetWindowText(), you can call the
CWnd::SendMessage() method. Since you want to change the text, the message
argument must be WM_SETTEXT. The wParam argument is not used. The lParam
argument holds the string that will be the new value of the window name. You must cast
the string to LPARAM. Here is an example that allows the user to click a menu item that
changes the title of the frame window:

void CMainFrame::OnEditChangeTitle()
Free download pdf