📄 statusbarwithprogress.cpp
字号:
#include "StdAfx.H"
#include "StatusBarWithProgress.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define ID_STATUS_PROGRESS 17234//状态条中的进度条标识
IMPLEMENT_DYNCREATE(CStatusBarWithProgress,CStatusBar)
BEGIN_MESSAGE_MAP(CStatusBarWithProgress,CStatusBar)
//{{AFX_MSG_MAP(CStatusBarWithProgress)
ON_WM_SIZE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
//---------------------------------------------------------------------------
CStatusBarWithProgress::CStatusBarWithProgress(int nProgressBarWidth):
m_nProgressWidth(nProgressBarWidth),
m_bProgressVisible(FALSE)
{
}
//创建状态条和进度条,但不显示进度条
//---------------------------------------------------------------------------
BOOL CStatusBarWithProgress::Create(CWnd *pParentWnd, DWORD dwStyle, UINT nID)
{
// Default creation
BOOL bCreatedOK =CStatusBar::Create(pParentWnd,dwStyle,nID);
if(bCreatedOK)
{
// 创建进度条
m_Progress.Create(WS_CHILD | WS_EX_STATICEDGE | PBS_SMOOTH,CRect(0,0,m_nProgressWidth,10),this,ID_STATUS_PROGRESS);
}
return bCreatedOK;
}
// 显示/隐藏进度条
//---------------------------------------------------------------------------
BOOL CStatusBarWithProgress::ShowProgressBar(BOOL bShow)
{
//存储原来是否可见状态
BOOL bOldVisible =m_bProgressVisible;
if((bOldVisible != bShow) && ::IsWindow(m_Progress.m_hWnd))
{
//显示/隐藏
m_Progress.ShowWindow(bShow ? SW_SHOWNA : SW_HIDE);
m_bProgressVisible =bShow;
//如果显示,要求进度条在合适位置
if(bShow)
{
AdjustProgressBarPosition();
RedrawWindow(NULL,NULL,RDW_INVALIDATE | RDW_UPDATENOW);
}
}
return bOldVisible;
}
//调整进度条位置,使其位于进度条最左边文字面板的右边
//---------------------------------------------------------------------------
void CStatusBarWithProgress::AdjustProgressBarPosition()
{
// 确信进度条已经创建
if(!::IsWindow(m_Progress.m_hWnd))
return;
//找出状态条最左边文字面板的大小
CString str_pane_text;
CRect progress_rect;
GetItemRect(0,progress_rect);
GetPaneText(0,str_pane_text);
CClientDC temp(this);
CFont *pOldFont =temp.SelectObject(GetFont());
progress_rect.left =temp.GetTextExtent(str_pane_text).cx + 10;
progress_rect.right =progress_rect.left + m_nProgressWidth;
progress_rect.InflateRect(0,-1);
temp.SelectObject(pOldFont);
//调整进度条控件大小
m_Progress.SetWindowPos(NULL,progress_rect.left,
progress_rect.top,
progress_rect.Width(),
progress_rect.Height(),SWP_NOZORDER | SWP_NOACTIVATE);
}
//当窗口变化时,动态调整进度条位置
//---------------------------------------------------------------------------
void CStatusBarWithProgress::OnSize(UINT nType, int cx, int cy)
{
// 默认大小
CStatusBar::OnSize(nType, cx, cy);
if(m_bProgressVisible)
AdjustProgressBarPosition();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -