代码简介或代码解析:
不用位图文件的电子钟表
实现步骤:
1 把CGCColorStatic.h/cpp和GCClock.h/cpp加入到工程中
2 在StaticDigitalClock2Dlg.h中添加
#include "GCClock.h"
3 添加如图所示的控件,并为第一个单选按钮IDC_RADIO_12HOURS添加一个变量
int m_iFormat;
4 在StaticDigitalClock2Dlg.h中添加
private:
CGCClock m_Clock;
CGCClock m_StyleExampleA;
CGCClock m_StyleExampleB;
CGCClock m_StyleExampleC;
CGCClock m_StyleExampleD;
5 在构造函数中加入
m_iFormat = 0;
在DoDataExchange(CDataExchange* pDX)的
//{{AFX_DATA_MAP(CStaticDigitalClock2Dlg)上面增加:
DDX_Control(pDX, IDC_STATIC_CLOCK, m_Clock);
DDX_Control(pDX, IDC_STATIC_STYLE_EXA, m_StyleExampleA);
DDX_Control(pDX, IDC_STATIC_STYLE_EXB, m_StyleExampleB);
DDX_Control(pDX, IDC_STATIC_STYLE_EXC, m_StyleExampleC);
DDX_Control(pDX, IDC_STATIC_STYLE_EXD, m_StyleExampleD);
6 在OnInitDialog()中加入:
// TODO: Add extra initialization here
m_StyleExampleA.SetOn(TRUE);
m_StyleExampleA.SetTextOffColor(RGB(0,180,255));
m_StyleExampleA.SetBackgroundOffColor(RGB(0,0,0));
m_StyleExampleA.SetBold();
m_StyleExampleA.SetSunken();
m_StyleExampleA.SetPointFont(12,"Comic Sans MS");
m_StyleExampleA.Start();
m_StyleExampleB.SetOn(FALSE);
m_StyleExampleB.SetTextOffColor(RGB(255,255,255));
m_StyleExampleB.SetPointFont(11,"Times New Roman");
// m_StyleExampleB.SetBold();
m_StyleExampleB.SetColorFrame(TRUE,2,RGB(190,0,0));
m_StyleExampleB.Start();
m_StyleExampleC.SetOn(FALSE);
m_StyleExampleC.SetTextOffColor(RGB(0,0,255));
m_StyleExampleC.SetBackgroundOffColor(RGB(0,0,0));
m_StyleExampleC.SetBold();
m_StyleExampleC.SetSunken();
m_StyleExampleC.SetPointFont(12,"幼圆");
m_StyleExampleC.Start();
m_StyleExampleD.SetOn(FALSE);
m_StyleExampleD.SetTextOffColor(RGB(0,255,0));
m_StyleExampleD.SetBackgroundOffColor(RGB(0,0,0));
m_StyleExampleD.SetBold();
m_StyleExampleD.SetSunken();
m_StyleExampleD.SetPointFont(12,"楷体GB2312");
m_StyleExampleD.Start();
m_Clock.SetOn(FALSE);
m_Clock.SetTextOffColor(RGB(0,180,255));
m_Clock.SetBackgroundOffColor(RGB(0,0,50));
m_Clock.SetBold();
m_Clock.SetPointFont(9,"Arial");
m_Clock.SetModalFrame();
m_Clock.Start();
7 写单选按钮响应函数
void CStaticDigitalClock2Dlg::OnRadio12hours()
...{
m_Clock.Format();
}
void CStaticDigitalClock2Dlg::OnRadio24hours()
...{
m_Clock.Format(TRUE);
}
8 附相关代码:
CGCClock::CGCClock() : CGCColorStatic()
...{
m_b24hr = FALSE;
m_uiHour = 0;
m_uiMinutes = 0;
m_bAlarmSet = FALSE;
m_bAlarmRunning = FALSE;
m_bAlarmActivated = FALSE;
}
CGCClock::~CGCClock()
...{
}
BEGIN_MESSAGE_MAP(CGCClock, CGCColorStatic)
ON_WM_TIMER()
END_MESSAGE_MAP()
void CGCClock::OnTimer(UINT nIDEvent)
...{
// The CGCClock class is derived from the CGCColorStatic class.
// Since the CGCColorStatic class provides the capability to flash the background
// or text using timer events, we must distinguish between CGCColorStatic timer events
// and the CGCClock's clock update timer event. Verify that the current timer
// event is the CGCClock class' update event. If it isn't route it on to the
// parent class' OnTimer() event handler because that may be one of the
// CGCColorStatic class' flashing timer events.
if (nIDEvent != CLOCK_UPDATE_TIMER_ID) CGCColorStatic::OnTimer(nIDEvent);
// If we are here then the current timer event is the clock's update event.
// Get the current time.
GetLocalTime(&m_CurrentDateTime);
// Format it for display.
FormatTime();
// Display the time.
SetWindowText(m_FormattedTime);
// If it is time for the alarm, initiate flashing of the background
// by calling the inherited FlashBackground() method.
if ( (!m_bAlarmRunning) && m_bAlarmSet && m_bAlarmActivated &&
( (m_CurrentDateTime.wHour >= m_uiHour)
&&
(m_CurrentDateTime.wMinute >= m_uiMinutes)
)
)
...{
FlashBackground(TRUE,FALSE,1);
m_bAlarmRunning = TRUE;
// Here is where you would add sound for the alarm.
}
}
void CGCClock::FormatTime()
...{
if (m_b24hr)
m_FormattedTime.Format("%02d:%02d:%02d",
m_CurrentDateTime.wHour,
m_CurrentDateTime.wMinute,
m_CurrentDateTime.wSecond);
else
...{
if (m_CurrentDateTime.wHour > 12)
...{
UINT th;
th = m_CurrentDateTime.wHour - 12;
m_FormattedTime.Format("%2d:%02d:%02d p.m.",
th,
m_CurrentDateTime.wMinute,
m_CurrentDateTime.wSecond);
}
else
m_FormattedTime.Format("%d:%02d:%02d p.m.",
m_CurrentDateTime.wHour,
m_CurrentDateTime.wMinute,
m_CurrentDateTime.wSecond);
}
}
BOOL CGCClock::Start()
...{
GetLocalTime(&m_CurrentDateTime);
FormatTime();
SetWindowText(m_FormattedTime);
UINT id = this->SetTimer(CLOCK_UPDATE_TIMER_ID,1000,NULL);
if (id == 0) return FALSE;
return TRUE;
}
void CGCClock::Format(const BOOL bTwentyFourHour)
...{
m_b24hr = bTwentyFourHour;
FormatTime();
SetWindowText(m_FormattedTime);
}
void CGCClock::GetAlarmTime(UINT &Hour, UINT &Minutes, BOOL& Active) const
...{
Hour = m_uiHour;
Minutes = m_uiMinutes;
Active = m_bAlarmRunning;
}
BOOL CGCClock::SetAlarm(const UINT Hour, const UINT Minutes)
...{
if ( Hour <= 0 ) return FALSE;
m_bAlarmSet = TRUE;
if (m_bAlarmRunning)
...{
// Since we are setting a new alarm time, kill the current alarm
// if it is active (going off).
FlashBackground(FALSE,FALSE);
m_bAlarmRunning = FALSE;
}
m_uiHour = Hour;
m_uiMinutes = Minutes;
return TRUE;
}
BOOL CGCClock::ActivateAlarm(const BOOL bOn)
...{
BOOL prev = m_bAlarmActivated;
m_bAlarmActivated = bOn;
return prev;
}
CGCColorStatic::CGCColorStatic(const BOOL bOn)
...{
m_bBackgroundOn = bOn; // Set initial bacground state.
m_bTextOn = bOn; // Set initial text state.
m_bColorFrameFlag = FALSE; // Is a color frame displayed?
m_ColorFrameWidth = 2; // At what width is the color frame to be displayed, if displayed?
// Initialize default on/off color variables.
m_CurrentBackgroundOnColor = DEFAULT_BACKGROUND_ON_COLOR;
m_CurrentBackgroundOffColor = DEFAULT_BACKGROUND_OFF_COLOR;
m_CurrentTextOnColor = DEFAULT_TEXT_ON_COLOR;
m_CurrentTextOffColor = DEFAULT_TEXT_OFF_COLOR;
m_ColorFrameColor = DEFAULT_BACKGROUND_OFF_COLOR;
// Create default background ON brush.
m_brOnBrush.CreateSolidBrush(m_CurrentBackgroundOnColor);
// Create default background OFF brush.
m_brOffBrush.CreateSolidBrush(m_CurrentBackgroundOffColor);
// m_brOffBrush.CreateSolidBrush(GetSysColor(COLOR_3DFACE));
m_FlashTypeFlag = NONE; // Default to not flashing.<
不用位图文件的电子钟表
实现步骤:
1 把CGCColorStatic.h/cpp和GCClock.h/cpp加入到工程中
2 在StaticDigitalClock2Dlg.h中添加
#include "GCClock.h"
3 添加如图所示的控件,并为第一个单选按钮IDC_RADIO_12HOURS添加一个变量
int m_iFormat;
4 在StaticDigitalClock2Dlg.h中添加
private:
CGCClock m_Clock;
CGCClock m_StyleExampleA;
CGCClock m_StyleExampleB;
CGCClock m_StyleExampleC;
CGCClock m_StyleExampleD;
5 在构造函数中加入
m_iFormat = 0;
在DoDataExchange(CDataExchange* pDX)的
//{{AFX_DATA_MAP(CStaticDigitalClock2Dlg)上面增加:
DDX_Control(pDX, IDC_STATIC_CLOCK, m_Clock);
DDX_Control(pDX, IDC_STATIC_STYLE_EXA, m_StyleExampleA);
DDX_Control(pDX, IDC_STATIC_STYLE_EXB, m_StyleExampleB);
DDX_Control(pDX, IDC_STATIC_STYLE_EXC, m_StyleExampleC);
DDX_Control(pDX, IDC_STATIC_STYLE_EXD, m_StyleExampleD);
6 在OnInitDialog()中加入:
// TODO: Add extra initialization here
m_StyleExampleA.SetOn(TRUE);
m_StyleExampleA.SetTextOffColor(RGB(0,180,255));
m_StyleExampleA.SetBackgroundOffColor(RGB(0,0,0));
m_StyleExampleA.SetBold();
m_StyleExampleA.SetSunken();
m_StyleExampleA.SetPointFont(12,"Comic Sans MS");
m_StyleExampleA.Start();
m_StyleExampleB.SetOn(FALSE);
m_StyleExampleB.SetTextOffColor(RGB(255,255,255));
m_StyleExampleB.SetPointFont(11,"Times New Roman");
// m_StyleExampleB.SetBold();
m_StyleExampleB.SetColorFrame(TRUE,2,RGB(190,0,0));
m_StyleExampleB.Start();
m_StyleExampleC.SetOn(FALSE);
m_StyleExampleC.SetTextOffColor(RGB(0,0,255));
m_StyleExampleC.SetBackgroundOffColor(RGB(0,0,0));
m_StyleExampleC.SetBold();
m_StyleExampleC.SetSunken();
m_StyleExampleC.SetPointFont(12,"幼圆");
m_StyleExampleC.Start();
m_StyleExampleD.SetOn(FALSE);
m_StyleExampleD.SetTextOffColor(RGB(0,255,0));
m_StyleExampleD.SetBackgroundOffColor(RGB(0,0,0));
m_StyleExampleD.SetBold();
m_StyleExampleD.SetSunken();
m_StyleExampleD.SetPointFont(12,"楷体GB2312");
m_StyleExampleD.Start();
m_Clock.SetOn(FALSE);
m_Clock.SetTextOffColor(RGB(0,180,255));
m_Clock.SetBackgroundOffColor(RGB(0,0,50));
m_Clock.SetBold();
m_Clock.SetPointFont(9,"Arial");
m_Clock.SetModalFrame();
m_Clock.Start();
7 写单选按钮响应函数
void CStaticDigitalClock2Dlg::OnRadio12hours()
...{
m_Clock.Format();
}
void CStaticDigitalClock2Dlg::OnRadio24hours()
...{
m_Clock.Format(TRUE);
}
8 附相关代码:
CGCClock::CGCClock() : CGCColorStatic()
...{
m_b24hr = FALSE;
m_uiHour = 0;
m_uiMinutes = 0;
m_bAlarmSet = FALSE;
m_bAlarmRunning = FALSE;
m_bAlarmActivated = FALSE;
}
CGCClock::~CGCClock()
...{
}
BEGIN_MESSAGE_MAP(CGCClock, CGCColorStatic)
ON_WM_TIMER()
END_MESSAGE_MAP()
void CGCClock::OnTimer(UINT nIDEvent)
...{
// The CGCClock class is derived from the CGCColorStatic class.
// Since the CGCColorStatic class provides the capability to flash the background
// or text using timer events, we must distinguish between CGCColorStatic timer events
// and the CGCClock's clock update timer event. Verify that the current timer
// event is the CGCClock class' update event. If it isn't route it on to the
// parent class' OnTimer() event handler because that may be one of the
// CGCColorStatic class' flashing timer events.
if (nIDEvent != CLOCK_UPDATE_TIMER_ID) CGCColorStatic::OnTimer(nIDEvent);
// If we are here then the current timer event is the clock's update event.
// Get the current time.
GetLocalTime(&m_CurrentDateTime);
// Format it for display.
FormatTime();
// Display the time.
SetWindowText(m_FormattedTime);
// If it is time for the alarm, initiate flashing of the background
// by calling the inherited FlashBackground() method.
if ( (!m_bAlarmRunning) && m_bAlarmSet && m_bAlarmActivated &&
( (m_CurrentDateTime.wHour >= m_uiHour)
&&
(m_CurrentDateTime.wMinute >= m_uiMinutes)
)
)
...{
FlashBackground(TRUE,FALSE,1);
m_bAlarmRunning = TRUE;
// Here is where you would add sound for the alarm.
}
}
void CGCClock::FormatTime()
...{
if (m_b24hr)
m_FormattedTime.Format("%02d:%02d:%02d",
m_CurrentDateTime.wHour,
m_CurrentDateTime.wMinute,
m_CurrentDateTime.wSecond);
else
...{
if (m_CurrentDateTime.wHour > 12)
...{
UINT th;
th = m_CurrentDateTime.wHour - 12;
m_FormattedTime.Format("%2d:%02d:%02d p.m.",
th,
m_CurrentDateTime.wMinute,
m_CurrentDateTime.wSecond);
}
else
m_FormattedTime.Format("%d:%02d:%02d p.m.",
m_CurrentDateTime.wHour,
m_CurrentDateTime.wMinute,
m_CurrentDateTime.wSecond);
}
}
BOOL CGCClock::Start()
...{
GetLocalTime(&m_CurrentDateTime);
FormatTime();
SetWindowText(m_FormattedTime);
UINT id = this->SetTimer(CLOCK_UPDATE_TIMER_ID,1000,NULL);
if (id == 0) return FALSE;
return TRUE;
}
void CGCClock::Format(const BOOL bTwentyFourHour)
...{
m_b24hr = bTwentyFourHour;
FormatTime();
SetWindowText(m_FormattedTime);
}
void CGCClock::GetAlarmTime(UINT &Hour, UINT &Minutes, BOOL& Active) const
...{
Hour = m_uiHour;
Minutes = m_uiMinutes;
Active = m_bAlarmRunning;
}
BOOL CGCClock::SetAlarm(const UINT Hour, const UINT Minutes)
...{
if ( Hour <= 0 ) return FALSE;
m_bAlarmSet = TRUE;
if (m_bAlarmRunning)
...{
// Since we are setting a new alarm time, kill the current alarm
// if it is active (going off).
FlashBackground(FALSE,FALSE);
m_bAlarmRunning = FALSE;
}
m_uiHour = Hour;
m_uiMinutes = Minutes;
return TRUE;
}
BOOL CGCClock::ActivateAlarm(const BOOL bOn)
...{
BOOL prev = m_bAlarmActivated;
m_bAlarmActivated = bOn;
return prev;
}
CGCColorStatic::CGCColorStatic(const BOOL bOn)
...{
m_bBackgroundOn = bOn; // Set initial bacground state.
m_bTextOn = bOn; // Set initial text state.
m_bColorFrameFlag = FALSE; // Is a color frame displayed?
m_ColorFrameWidth = 2; // At what width is the color frame to be displayed, if displayed?
// Initialize default on/off color variables.
m_CurrentBackgroundOnColor = DEFAULT_BACKGROUND_ON_COLOR;
m_CurrentBackgroundOffColor = DEFAULT_BACKGROUND_OFF_COLOR;
m_CurrentTextOnColor = DEFAULT_TEXT_ON_COLOR;
m_CurrentTextOffColor = DEFAULT_TEXT_OFF_COLOR;
m_ColorFrameColor = DEFAULT_BACKGROUND_OFF_COLOR;
// Create default background ON brush.
m_brOnBrush.CreateSolidBrush(m_CurrentBackgroundOnColor);
// Create default background OFF brush.
m_brOffBrush.CreateSolidBrush(m_CurrentBackgroundOffColor);
// m_brOffBrush.CreateSolidBrush(GetSysColor(COLOR_3DFACE));
m_FlashTypeFlag = NONE; // Default to not flashing.<