51、wxWidgets绘画之自定义线条wxPENSTYLE_USER_DASH

通过设置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);
    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值