单文档MFC程序中关于状态栏的一些修改

2024-11-07 15:44:17
推荐回答(1个)
回答1:

实现这两个功能一般有两个方法,一个就是在CStatusBar的派生类中实现,另一种不采用派生类,直接在状态栏的窗口中实现,我说的方法是第二种:

(1)在String Table中新建字符串:ID_INDICATOR_BUTTON、ID_INDICATOR_PAINT,一个是按钮的,一个是绘图的;
(2)在indicators[]中增加该指示器:
static UINT indicators[] =
{
ID_SEPARATOR, // 状态行指示器
ID_INDICATOR_BUTTON,
ID_INDICATOR_PAINT,
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};
(2)在CMainFrame中定义按钮对象:CButton cBtn;
(3)在OnCreate中添加按钮创建代码:
//获取工具栏BUTTON大小
CRect rect;
m_wndStatusBar.GetItemRect(m_wndStatusBar.CommandToIndex(ID_INDICATOR_BUTTON), &rect);
m_cBtn.Create(L"测试",WS_VISIBLE|WS_CHILD,rect,&m_wndStatusBar,ID_BUTTON_TEST);
(4)增加椭圆绘制函数StatusDraw():
void CMainFrame::StatusDraw()
{
CDC *pDC = m_wndStatusBar.GetDC();
CRect rect;
m_wndStatusBar.GetItemRect(m_wndStatusBar.CommandToIndex(ID_INDICATOR_PAINT),&rect);
m_wndStatusBar.SetPaneText(m_wndStatusBar.CommandToIndex(ID_INDICATOR_PAINT),L"");
CPen pen(PS_SOLID,1,RGB(255,0,0));
pDC->SelectObject(&pen);
pDC->Ellipse(&rect);
}
(5)添加OnSize消息处理函数,然后:
void CMainFrame::OnSize(UINT nType, int cx, int cy)
{
CFrameWnd::OnSize(nType, cx, cy);

// TODO: 在此处添加消息处理程序代码
if(m_cBtn.GetSafeHwnd()!=NULL)
{
CRect rect;
m_wndStatusBar.GetItemRect(m_wndStatusBar.CommandToIndex(ID_INDICATOR_BUTTON), &rect);
m_cBtn.MoveWindow(rect);

StatusDrawLine();
}
}