挂钟程序
程序运行截图:

clockDlg.h
//---------------------------------------------------------------------------
//
// Name: ClockDlg.h
// Author: Jos de Jong
// Created: 29-12-2007 11:42:36
// Description: ClockDlg class declaration
//
//---------------------------------------------------------------------------
#ifndef __CLOCKDLG_h__
#define __CLOCKDLG_h__
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include <wx/wx.h>
#include <wx/dialog.h>
#else
#include <wx/wxprec.h>
#endif
//Do not add custom headers between
//Header Include Start and Header Include End.
//wxDev-C++ designer will remove them. Add custom headers after the block.
////Header Include Start
#include <wx/timer.h>
////Header Include End
#include <wx/dcbuffer.h>
#include <cmath>
////Dialog Style Start
#undef ClockDlg_STYLE
#define ClockDlg_STYLE wxSUNKEN_BORDER | wxCAPTION | wxRESIZE_BORDER | wxSYSTEM_MENU | wxDIALOG_NO_PARENT | wxMINIMIZE_BOX | wxMAXIMIZE_BOX | wxCLOSE_BOX
////Dialog Style End
class ClockDlg : public wxDialog
{
private:
DECLARE_EVENT_TABLE();
public:
ClockDlg(wxWindow *parent, wxWindowID id = 1, const wxString &title = wxT("Clock"), const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = ClockDlg_STYLE);
virtual ~ClockDlg();
void ClockDlgPaint(wxPaintEvent& event);
void ClockDlgSize(wxSizeEvent& event);
void WxTimer1Timer(wxTimerEvent& event);
private:
//Do not add custom control declarations between
//GUI Control Declaration Start and GUI Control Declaration End.
//wxDev-C++ will remove them. Add custom code after the block.
////GUI Control Declaration Start
wxTimer *WxTimer1;
////GUI Control Declaration End
private:
//Note: if you receive any error with these enum IDs, then you need to
//change your old form code that are based on the #define control IDs.
//#defines may replace a numeric value for the enum names.
//Try copy and pasting the below block in your old form header files.
enum
{
////GUI Enum Control ID Start
ID_WXTIMER1 = 1001,
////GUI Enum Control ID End
ID_DUMMY_VALUE_ //don't remove this value unless you have other enum values
};
private:
void OnClose(wxCloseEvent& event);
void CreateGUIControls();
};
#endif
clockapp.cpp
//---------------------------------------------------------------------------
//
// Name: ClockApp.cpp
// Author: Jos de Jong
// Created: 29-12-2007 11:42:36
// Description:
//
//---------------------------------------------------------------------------
#include "ClockApp.h"
#include "ClockDlg.h"
IMPLEMENT_APP(ClockDlgApp)
bool ClockDlgApp::OnInit()
{
ClockDlg* dialog = new ClockDlg(NULL);
SetTopWindow(dialog);
dialog->Show(true);
return true;
}
int ClockDlgApp::OnExit()
{
return 0;
}
clockApp.h
//---------------------------------------------------------------------------
//
// Name: ClockApp.h
// Author: Jos de Jong
// Created: 29-12-2007 11:42:36
// Description:
//
//---------------------------------------------------------------------------
#ifndef __CLOCKDLGApp_h__
#define __CLOCKDLGApp_h__
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include <wx/wx.h>
#else
#include <wx/wxprec.h>
#endif
class ClockDlgApp : public wxApp
{
public:
bool OnInit();
int OnExit();
};
#endif
clockDlg.cpp
//---------------------------------------------------------------------------
//
// Name: ClockDlg.cpp
// Author: Jos de Jong
// Created: 29-12-2007 11:42:36
// Description: ClockDlg class implementation
//
//---------------------------------------------------------------------------
#include "ClockDlg.h"
//Do not add custom headers
//wxDev-C++ designer will remove them
////Header Include Start
////Header Include End
//----------------------------------------------------------------------------
// ClockDlg
//----------------------------------------------------------------------------
//Add Custom Events only in the appropriate block.
//Code added in other places will be removed by wxDev-C++
////Event Table Start
BEGIN_EVENT_TABLE(ClockDlg,wxDialog)
////Manual Code Start
////Manual Code End
EVT_CLOSE(ClockDlg::OnClose)
EVT_SIZE(ClockDlg::ClockDlgSize)
EVT_PAINT(ClockDlg::ClockDlgPaint)
EVT_TIMER(ID_WXTIMER1,ClockDlg::WxTimer1Timer)
END_EVENT_TABLE()
////Event Table End
ClockDlg::ClockDlg(wxWindow *parent, wxWindowID id, const wxString &title, const wxPoint &position, const wxSize& size, long style)
: wxDialog(parent, id, title, position, size, style)
{
CreateGUIControls();
}
ClockDlg::~ClockDlg()
{
}
void ClockDlg::CreateGUIControls()
{
//Do not add custom code between
//GUI Items Creation Start and GUI Items Creation End.
//wxDev-C++ designer will remove them.
//Add the custom code before or after the blocks
////GUI Items Creation Start
SetTitle(wxT("Clock"));
SetIcon(wxNullIcon);
SetSize(8,8,509,412);
Center();
WxTimer1 = new wxTimer();
WxTimer1->SetOwner(this, ID_WXTIMER1);
WxTimer1->Start(200);
////GUI Items Creation End
}
void ClockDlg::OnClose(wxCloseEvent& /*event*/)
{
Destroy();
}
/*
* Convert a float value to int
*/
int toInt(float value)
{
return static_cast<int>(value);
}
/*
* ClockDlgPaint
*/
void ClockDlg::ClockDlgPaint(wxPaintEvent& event)
{
SetBackgroundStyle(wxBG_STYLE_CUSTOM);
//wxPaintDC dc(this);
wxBufferedPaintDC dc(this);
// Get window dimensions
wxSize sz = GetClientSize();
wxPoint center = wxPoint(sz.x / 2, sz.y / 2);
wxCoord radius = wxMin(sz.x / 2 - 20, sz.y / 2 - 20);
radius = wxMax(radius, 50);
float factor = wxMax(radius/60.0, 1.0);
// create a few colors
wxColour lightblue = wxColour(220, 245, 255);
wxColour darkblue = wxColour(0, 0, 120);
// draw lightblue background
dc.SetPen(lightblue);
dc.SetBrush(lightblue);
dc.DrawRectangle(0, 0, sz.x, sz.y);
// draw clock border
dc.SetPen(*wxBLACK_PEN);
dc.SetBrush(*wxBLACK_BRUSH);
dc.DrawCircle(center, radius);
dc.SetPen(*wxBLACK_PEN);
dc.SetBrush(*wxWHITE_BRUSH);
dc.DrawCircle(center, toInt(radius - 3.0 * factor));
// paint lines for minutes
float pi = 3.141592654;
float r_outer = radius - 5 * factor;
float r_inner_min = radius - 7 * factor;
float r_inner_hour = radius - 10 * factor;
float r_inner_text = radius - 15 * factor;
int linewidth = 1;
for (int r = 1; r <= 60; r ++)
{
float s = sin(r/60.0*2.0*pi);
float c = -cos(r/60.0*2.0*pi);
linewidth = wxMax(toInt(factor / 2), 1);
dc.SetPen(wxPen(*wxBLACK, linewidth));
wxPoint outer = wxPoint(toInt(r_outer * s), toInt(r_outer * c));
wxPoint inner = wxPoint(toInt(r_inner_min * s), toInt(r_inner_min * c));
dc.DrawLine(center + inner, center + outer);
if (r % 5 == 0)
{
linewidth = wxMax(toInt(factor), 1);
dc.SetPen(wxPen(*wxBLACK, linewidth));
// paint larger lines for the hours
outer = wxPoint(toInt(r_outer * s), toInt(r_outer * c));
inner = wxPoint(toInt(r_inner_hour * s), toInt(r_inner_hour * c));
dc.DrawLine(center + inner, center + outer);
// paint value of the hour
wxString hour;
hour.Printf("%i", r / 5);
dc.SetFont(wxFont(toInt(5 * factor), wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD, false, "tahoma", wxFONTENCODING_DEFAULT));
// calculate the size of the text and paint it in the window
wxCoord w = 0, h = 0;
dc.GetTextExtent(hour, &w, &h);
wxPoint text_pos = wxPoint(toInt(r_inner_text * s - w / 2), toInt(r_inner_text * c - h / 2));
dc.DrawText(hour, center + text_pos);
//dc.DrawRotatedText(hour, center.x + text_pos.x, center.y + text_pos.y, (60-r)/60.0*360); // cool :)
}
}
// draw hour, minute and second hand of the clock
wxDateTime time = wxDateTime::Now();
int h = time.GetHour();
int m = time.GetMinute();
int s = time.GetSecond();
int r = 1;
/* int h_deg = toInt((h % 12) / 12.0 * 360);
int m_deg = toInt(m / 60.0 * 360.0);
int s_deg = toInt(s / 60.0 * 360.0);*/
float h_rad = (h % 12 + m / 60.0 + s / 3600.0) / 12.0 * 2.0 * pi;
float m_rad = (m / 60.0 + s / 3600.0) * 2.0 * pi;
float s_rad = s / 60.0 * 2.0 * pi;
// show digital time in caption
wxString title;
title.Printf("Clock %i:%.2i:%.2i", h, m, s);
SetTitle(title);
r = toInt(factor * 20);
linewidth = wxMax(toInt(factor * 5), 1);
dc.SetPen(wxPen(*wxBLACK, linewidth));
dc.DrawLine(center, center + wxPoint(toInt(r * sin(h_rad)), toInt(r * -cos(h_rad))));
r = toInt(factor * 40);
linewidth = wxMax(toInt(factor * 2), 1);
dc.SetPen(wxPen(*wxBLACK, linewidth));
dc.DrawLine(center, center + wxPoint(toInt(r * sin(m_rad)), toInt(r * -cos(m_rad))));
r = toInt(factor * 50);
linewidth = wxMax(toInt(factor), 1);
dc.SetPen(wxPen(*wxRED, linewidth));
dc.DrawLine(center, center + wxPoint(toInt(r * sin(s_rad)), toInt(r * -cos(s_rad))));
// credits for wxWidgets
dc.SetTextForeground(darkblue);
dc.SetFont(wxFont(8, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD, false, "arial", wxFONTENCODING_DEFAULT));
dc.DrawRotatedText(wxT("用wxWidgets2.9.4创建"), toInt(sz.x - 5 - 10), toInt(sz.y - 15), 90);
}
/*
* ClockDlgSize
*/
void ClockDlg::ClockDlgSize(wxSizeEvent& event)
{
Refresh();
}
/*
* WxTimer1Timer
*/
void ClockDlg::WxTimer1Timer(wxTimerEvent& event)
{
Refresh();
}

浙公网安备 33010602011771号