VS2010 状态栏信息显示
步骤:
<1> : 现在CMainFrame类中定义:
CMFCStatusBar m_wndStatusBar;//有MFC3字母,很多教材都使用CStatusBar
<2> : 定义indicator指示器
static UINT indicators[] =
{
ID_SEPARATOR,
ID_SEPARATOR, // status line indicator
ID_INDICATOR_STR,
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};
红色的系统已经预定义了,添加一个自定义的举例
在res资源中在string中添加ID_INDICATOR_STR字段,预设值"asssd",根据需要.
<3> : 在CMainFrame类创建OnCreate中:
if (!m_wndStatusBar.Create(this))
{
TRACE0("Failed to create status bar\n");
return -1; // fail to create
}
m_wndStatusBar.SetIndicators(indicators, sizeof(indicators)/sizeof(UINT));
<4> : 显示信息,显示鼠标坐标
在C***View类中添加鼠标移动事件,第一种显示方式:
void C***View::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CString strr;
strr.Format(_T("X=%d,Y=%d"),point.x,point.y);
((CFrameWnd*)GetParent())->SetMessageText(strr);
CView::OnMouseMove(nFlags, point);
}
第二种显示方式
void CHelloWorld2View::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CString strr;
CMFCStatusBar* pStatus;
strr.Format(_T("X=%d,Y=%d"),point.x,point.y);
pStatus=(CMFCStatusBar*)(AfxGetApp()->m_pMainWnd->GetDescendantWindow(AFX_IDW_STATUS_BAR));
pStatus->SetPaneText(1,strr);
CView::OnMouseMove(nFlags, point);
}
<5> : 在状态栏显示时间:
a> : 先创建一个指示器元素ID_INDICATOR_TIMER字段,字符"Show Time";
b> : 在CMainFrame类中的OnCreate中添加:
SetTimer(1,1000,NULL);
CTime time=CTime::GetCurrentTime();
CString str=time.Format("%H:%M:%S");
CClientDC dc(this);
CSize sz=dc.GetTextExtent(str);
m_wndStatusBar.SetPaneInfo(1,ID_INDICATOR_TIMER,SBPS_POPOUT,sz.cx);
m_wndStatusBar.SetPaneText(0,str);
c> : 然后添加OnTimer事件
void CMainFrame::OnTimer(UINT_PTR nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CTime time=CTime::GetCurrentTime();
CString str=time.Format("%H:%M:%S");
m_wndStatusBar.SetPaneText(0,str);
CFrameWndEx::OnTimer(nIDEvent);
}
d> : 运行即可得到结果!
e> : 添加状态栏几个常用属性设置:
m_wndStatusBar.SetPaneWidth(0,250);
m_wndStatusBar.SetPaneTextColor (0, RGB(255,0,0));
m_wndStatusBar.SetPaneBackgroundColor(0,RGB(255,255,255));
......