通过设置wxDash的变化,,来模拟一个简单的动画
#include <wx/wx.h>
#include <wx/dcbuffer.h>
#include <wx/graphics.h>
class Simple:public wxFrame
{
public:
Simple(const wxString& title);
protected:
void OnPaint(wxPaintEvent& event);
void OnTimer(wxCommandEvent& event);
wxTimer* timer;
const static int dashCount =8;
wxDash dash[dashCount],dash1[dashCount],dash2[dashCount];
int dashIndex =0;
};
Simple::Simple(const wxString& title)
:wxFrame(NULL,-1,title,wxPoint(-1,-1),wxSize(680,580))
{
SetBackgroundStyle(wxBG_STYLE_PAINT);
srand( time(NULL));
timer = new wxTimer(this,wxID_ANY);
dash1[0] = 3; // Long dash <---------+
dash1[1] = 4; // Short gap |
dash1[2] = 3; // Short dash |
dash1[3] = 4; // Short gap |
dash1[4] = 3; // Short dash |
dash1[5] = 4; // Short gap and repeat +
dash2[0] = 4; // Long dash <---------+
dash2[1] = 6; // Short gap |
dash2[2] = 4; // Short dash |
dash2[3] = 6; // Short gap |
dash2[4] = 4; // Short dash |
dash2[5] = 6; // Short gap and repeat +
for(int i=0;i<dashCount;i++) dash[i]=dash1[i];
Bind(wxEVT_TIMER,wxCommandEventHandler(Simple::OnTimer),this);
Bind(wxEVT_PAINT,wxPaintEventHandler(Simple::OnPaint),this);
timer->Start(300);
Centre();
}
void Simple::OnTimer(wxCommandEvent& event)
{
if(dashIndex ==1 ){
for(int i=0;i<dashCount;i++) dash[i]=dash2[i];
dashIndex =0;
}
else
{
for(int i=0;i<dashCount;i++) dash[i]=dash1[i];
dashIndex =1;
}
Refresh();
}
void Simple::OnPaint(wxPaintEvent& event)
{
wxSize sizer = GetClientSize();
wxBufferedPaintDC dc(this);
dc.Clear();
wxPen pen(*wxRED,3,wxPENSTYLE_USER_DASH);
pen.SetDashes(dashCount,dash);
dc.SetPen(pen);
dc.DrawLine(20,20,480,20);
dc.DrawLine(0,30,sizer.GetWidth(),30);
dc.DrawRectangle(20,50,270,30);
dc.DrawCircle(400,100,50);
pen.SetDashes(dashCount,dash1);
dc.SetPen(pen);
if(dashIndex ==1 ){
dc.DrawLine(0,10,sizer.GetWidth(),10);
}
else
{
dc.DrawLine(5,10,sizer.GetWidth(),10);
}
}