📄 statusbardlg.cpp
字号:
// StatusBarDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Controls.h"
#include "StatusBarDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define PANEOFFSET 6
/////////////////////////////////////////////////////////////////////////////
// CStatusBarDlg property page
IMPLEMENT_DYNCREATE(CStatusBarDlg, CPropertyPage)
static UINT indicators[] =
{
ID_SEPARATOR, // status line indicator
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
ID_INDICATOR_TIME
};
CStatusBarDlg::CStatusBarDlg() : CPropertyPage(CStatusBarDlg::IDD)
{
//{{AFX_DATA_INIT(CStatusBarDlg)
m_nStatusBarPos = 1;
m_bColor = FALSE;
m_bNoDivider = TRUE;
//}}AFX_DATA_INIT
m_psp.dwFlags &= ~PSP_HASHELP;
// m_psp.pszTitle = "Status Bar";
}
CStatusBarDlg::~CStatusBarDlg()
{
}
void CStatusBarDlg::DoDataExchange(CDataExchange* pDX)
{
CPropertyPage::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CStatusBarDlg)
DDX_Radio(pDX, IDC_STATUSBAR_TOP, m_nStatusBarPos);
DDX_Check(pDX, IDC_STATUSBAR_COLOR, m_bColor);
DDX_Check(pDX, IDC_STATUSBAR_NODIVIDER, m_bNoDivider);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CStatusBarDlg, CPropertyPage)
//{{AFX_MSG_MAP(CStatusBarDlg)
ON_BN_CLICKED(IDC_STATUSBAR_TOP, OnStatusbarPosition)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_STATUSBAR_COLOR, OnStatusbarColor)
ON_BN_CLICKED(IDC_STATUSBAR_NODIVIDER, OnStatusbarNodivider)
ON_BN_CLICKED(IDC_STATUSBAR_BOTTOM, OnStatusbarPosition)
ON_BN_CLICKED(IDC_STATUSBAR_INSERTPANE, OnStatusbarInsertpane)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CStatusBarDlg message handlers
BOOL CStatusBarDlg::OnInitDialog()
{
CPropertyPage::OnInitDialog();
/*
CCS_BOTTOM
CCS_ADJUSTABLE
CCS_NODIVIDER Prevents a two-pixel highlight from being drawn at the top of the control.
CCS_NOHILITE Prevents a one-pixel highlight from being drawn at the top of the control.
CCS_NOMOVEY Causes the control to resize
and move itself horizontally,
but not vertically, in response
to a WM_SIZE message.
If the CCS_NORESIZE style is
used, this style does not
apply.
CCS_NOPARENTALIGN Prevents the control from automatically
moving to the top or bottom of the
parent window. Instead, the control
keeps its position within the parent
window despite changes to the size
of the parent window. If the
CCS_TOP or CCS_BOTTOM style is also
used, the height is adjusted to the
default, but the position and
width remain unchanged.
CCS_NORESIZE Prevents the control from using
the default width and height
when setting its initial size
or a new size. Instead, the control
uses the width and height specified
in the request for creation
or sizing.
CCS_TOP Causes the control to position
itself at the top of the parent
window抯 client area and sets
the width to be the same as the
parent window抯 width.
*/
CRect rc;
GetWindowRect (rc);
ScreenToClient (rc);
m_SampleBar.Create (CCS_NODIVIDER |// CCS_NOHILITE |
WS_CHILD |
WS_VISIBLE |
CCS_BOTTOM |
CCS_ADJUSTABLE,
CRect (0, 0, 0, 0), this, 0);
CFont *font = GetFont ();
m_SampleBar.SetFont (font);
SetBarParts (indicators, sizeof(indicators)/sizeof(UINT));
m_SampleBar.GetRect (1, rc);
rc.bottom += 4 * (rc.bottom - rc.top);
SetTimer (42, 5000, NULL);
return TRUE;
}
void CStatusBarDlg::OnStatusbarPosition()
{
UpdateData (TRUE);
switch (m_nStatusBarPos)
{
case 0: // Status bar on Top
m_SampleBar.ModifyStyle (CCS_BOTTOM, CCS_TOP, TRUE);
break;
case 1: // Status bar on Bottom
m_SampleBar.ModifyStyle (CCS_TOP, CCS_BOTTOM, TRUE);
break;
default:
return;
}
m_SampleBar.SetWindowPos (NULL, 0, 0, 0, 0,
SWP_NOZORDER|SWP_NOMOVE|SWP_NOACTIVATE|SWP_SHOWWINDOW);
}
void CStatusBarDlg::SetBarParts(UINT *pInd, int nCount)
{
CRect rc;
// m_SampleBar.GetWindowRect (rc);
GetWindowRect (rc);
ScreenToClient (rc);
int nWidth = rc.right - rc.left;
int *Indicators = new int [nCount];
CWindowDC dc(&m_SampleBar);
CSize sz;
CFont *font = GetFont ();
dc.SelectObject (font);
memset (Indicators, '\0', sizeof (int) * nCount);
CTime Now = CTime::GetCurrentTime ();
CString strTime = Now.Format ("%A, %B %d, %Y %H:%M");
for (int pos = nCount - 1; pos >= 0; --pos)
{
switch (pInd[pos])
{
case ID_SEPARATOR: // status line indicator
Indicators[pos] = nWidth;
break;
case ID_INDICATOR_CAPS:
Indicators[pos] = nWidth;
sz = dc.GetTextExtent ("CAPS", 4);
nWidth -= sz.cx + PANEOFFSET;
break;
case ID_INDICATOR_NUM:
Indicators[pos] = nWidth;
sz = dc.GetTextExtent ("NUM", 3);
nWidth -= sz.cx + PANEOFFSET;
break;
case ID_INDICATOR_SCRL:
Indicators[pos] = nWidth;
sz = dc.GetTextExtent ("SCRL", 4);
nWidth -= sz.cx + PANEOFFSET;
break;
case ID_INDICATOR_TIME:
Indicators[pos] = nWidth;
sz = dc.GetTextExtent (strTime);
nWidth -= sz.cx + PANEOFFSET;
break;
default:
break;
}
}
m_SampleBar.SetParts (nCount, Indicators);
m_SampleBar.GetParts (nCount, Indicators);
m_SampleBar.SetParts (nCount, Indicators);
for (pos = nCount - 1; pos > 0; --pos)
{
switch (pInd[pos])
{
case ID_SEPARATOR: // status line indicator
m_SampleBar.SetText (_T("Stuff"), pos, 0);
break;
case ID_INDICATOR_CAPS:
m_SampleBar.SetText (_T("CAPS"), pos, 0);
break;
case ID_INDICATOR_NUM:
m_SampleBar.SetText (_T("NUM"), pos, 0);
break;
case ID_INDICATOR_SCRL:
m_SampleBar.SetText (_T("SCRL"), pos, 0);
break;
case ID_INDICATOR_TIME:
m_SampleBar.SetText ((LPCSTR)strTime, pos, 0);
break;
default:
break;
}
}
delete [] Indicators;
}
void CStatusBarDlg::OnTimer(UINT nIDEvent)
{
static int count = 0;
switch (nIDEvent)
{
case 42:
{
int nCount = m_SampleBar.GetParts (0, NULL);
CWindowDC dc(this);
CFont *font = GetFont ();
dc.SelectObject (font);
CTime Now = CTime::GetCurrentTime() + count++ * 86400;
CString strTime;
strTime = m_SampleBar.GetText (nCount - 1);
CSize szOld = dc.GetTextExtent (strTime);
strTime = Now.Format ("%A, %B %d, %Y %H:%M");
CSize sz = dc.GetTextExtent (strTime);
if (sz.cx != szOld.cx)
{
int *Indicators = new int [nCount];
CString *strText = new CString [nCount];
for (int i = 0; i < nCount; ++i)
{
TCHAR szText[40];
m_SampleBar.GetText (szText, i);
strText[i] = szText;
}
strText[nCount-1] = (LPCSTR) strTime;
m_SampleBar.GetParts (nCount, Indicators);
for (i = (nCount - 1); i > 0; --i)
{
sz = dc.GetTextExtent (strText[i]);
Indicators[i-1] = Indicators[i] - sz.cx - PANEOFFSET;
}
m_SampleBar.SetParts (nCount, Indicators);
for (i = 0; i < nCount; ++i)
m_SampleBar.SetText (strText[i], i, 0);
delete [] Indicators;
delete [] strText;
}
else
m_SampleBar.SetText (strTime, nCount - 1, 0);
return;
}
default:
break;
}
CPropertyPage::OnTimer(nIDEvent);
}
void CStatusBarDlg::OnStatusbarColor()
{
UpdateData (TRUE);
if (m_bColor)
m_SampleBar.SetBkColor (RGB(0x00, 0xff, 0xff));
else
m_SampleBar.SetBkColor (GetSysColor (COLOR_BTNFACE));
}
void CStatusBarDlg::OnStatusbarNodivider()
{
UpdateData (TRUE);
DWORD dwStyle;
dwStyle = GetWindowLong (m_SampleBar.m_hWnd, GWL_STYLE);
if (m_bNoDivider)
dwStyle |= CCS_NODIVIDER;
else
dwStyle &= ~CCS_NODIVIDER;
SetWindowLong (m_SampleBar.m_hWnd, GWL_STYLE, dwStyle);
CRect rc;
m_SampleBar.GetWindowRect (rc);
m_SampleBar.SetWindowPos(NULL, 0, 0, rc.Width(), rc.Height(),
SWP_NOZORDER|SWP_NOMOVE|SWP_NOACTIVATE|SWP_SHOWWINDOW);
}
void CStatusBarDlg::OnStatusbarInsertpane()
{
int nInsertAt = 4;
int nCount = m_SampleBar.GetParts (0, NULL);
if (nCount == 6)
{
DeletePane (nInsertAt);
return;
}
int *Indicators = new int [nCount + 1];
CString *strText = new CString [nCount + 1];
m_SampleBar.GetParts (nCount, Indicators);
for (int i = 0; i < nCount; ++i)
{
TCHAR szText[40];
m_SampleBar.GetText (szText, i);
strText[i] = szText;
}
for (i = nCount; i > nInsertAt; --i)
{
Indicators [i] = Indicators[i - 1];
strText[i] = (LPCSTR)strText [i - 1];
}
strText [i] = _T("INS");
CWindowDC dc(&m_SampleBar);
CFont *font = GetFont ();
dc.SelectObject (font);
CSize sz = dc.GetTextExtent (strText[i]);
for (i = nInsertAt + 1; i > 0; --i)
{
sz = dc.GetTextExtent (strText[i]);
Indicators [i-1] = Indicators[i] - sz.cx - PANEOFFSET;
}
--i;
m_SampleBar.SetParts (nCount + 1, Indicators);
for (i = 0; i < (nCount + 1); ++i)
m_SampleBar.SetText ((LPCSTR)strText[i], i, 0);
delete [] Indicators;
delete [] strText;
CWnd *button = GetDlgItem (IDC_STATUSBAR_INSERTPANE);
button->SetWindowText (_T("Delete Pane"));
}
void CStatusBarDlg::DeletePane(int nPane)
{
int nCount = m_SampleBar.GetParts (0, NULL);
int *Indicators = new int [nCount + 1];
CString *strText = new CString [nCount + 1];
m_SampleBar.GetParts (nCount, Indicators);
for (int i = 0; i < nCount; ++i)
{
TCHAR szText[40];
m_SampleBar.GetText (szText, i);
strText[i] = szText;
}
//
// Remove the text from item 3;
//
for (i = nPane; i < (nCount - 1); ++i)
{
strText[i] = strText[i + 1];
}
//
// Put the time indicator width in the next
// lower
Indicators[nCount-2] = Indicators[nCount-1];
CWindowDC dc(&m_SampleBar);
CFont *font = GetFont ();
dc.SelectObject (font);
CSize sz;
for (i = (nCount-2); i > 0; --i)
{
sz = dc.GetTextExtent (strText[i]);
Indicators[i-1] = Indicators [i] - sz.cx - PANEOFFSET;
}
m_SampleBar.SetParts (nCount - 1, Indicators);
for (i = 0; i < (nCount - 1); ++i)
m_SampleBar.SetText ((LPCSTR)strText[i], i, 0);
CWnd *button = GetDlgItem (IDC_STATUSBAR_INSERTPANE);
button->SetWindowText (_T("Insert Pane"));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -