转自:http://joeblackzqq.blog.163.com/blog/static/16259543220109100224481/
新建一个单文档的MFC程序,在菜单的最后添加一个菜单项:
ID: IDM_TEST
Caption: test
去掉separator前面的“钩”,结果如下:
通过向导,给test菜单项添加点击事件,添加代码:
void CMainFrame::OnTest()
{
TCHAR* szText;
static int count = 0;
CString temp;
count++;
temp.Format("Hello world %d", count);
szText = temp.GetBuffer(temp.GetLength());
::SendMessage(m_wndStatusBar, SB_SETTEXT, (WPARAM)0, (LPARAM)szText); // 第三个参数用来控制状态的第几栏(索引以0开始)
temp.ReleaseBuffer(); // 在调用CString.GetBuffer()后,一般要调用CString.ReleaseBuffer()来释放空间
/*
CString::ReleaseBuffer
void ReleaseBuffer( int nNewLength = -1 );
{
TCHAR* szText;
static int count = 0;
CString temp;
count++;
temp.Format("Hello world %d", count);
szText = temp.GetBuffer(temp.GetLength());
::SendMessage(m_wndStatusBar, SB_SETTEXT, (WPARAM)0, (LPARAM)szText); // 第三个参数用来控制状态的第几栏(索引以0开始)
temp.ReleaseBuffer(); // 在调用CString.GetBuffer()后,一般要调用CString.ReleaseBuffer()来释放空间
/*
CString::ReleaseBuffer
void ReleaseBuffer( int nNewLength = -1 );
[Parameters]
nNewLength
The new length of the string in characters, not counting a null terminator.
If the string is null-terminated, the -1 default value sets the CString size to the current length of the string.
[Remarks]
Use ReleaseBuffer to end use of a buffer allocated by GetBuffer.
If you know that the string in the buffer is null-terminated, you can omit the nNewLength argument.
If your string is not null-terminated, then use nNewLength to specify its length.
The address returned by GetBuffer is invalid after the call to ReleaseBuffer or any other CString operation.
*/
}
nNewLength
The new length of the string in characters, not counting a null terminator.
If the string is null-terminated, the -1 default value sets the CString size to the current length of the string.
[Remarks]
Use ReleaseBuffer to end use of a buffer allocated by GetBuffer.
If you know that the string in the buffer is null-terminated, you can omit the nNewLength argument.
If your string is not null-terminated, then use nNewLength to specify its length.
The address returned by GetBuffer is invalid after the call to ReleaseBuffer or any other CString operation.
*/
}
快运行看看吧,达到了给状态栏发送消息的目的。
注:在子线程中一般是不可以直接修改状态栏上的文本信息的,所以最好的方法是通过发送消息的方法来实现。