50、wxWidgets之自定义wxGrideCellEnumRenderer

表格中的某一列的值为0 、1、2。如果想显示为 等待、生产、完成等字符串,使用官方的wxGrideCellEnumRenderer有点小bug,需要自定义一个render来实现。


class  MyGridCellEnumRenderer : public wxGridCellChoiceRenderer
{
public:
    MyGridCellEnumRenderer( const wxString& choices = wxEmptyString );

    // draw the string right aligned
    virtual void Draw(wxGrid& grid,
                      wxGridCellAttr& attr,
                      wxDC& dc,
                      const wxRect& rect,
                      int row, int col,
                      bool isSelected) wxOVERRIDE;

    virtual wxSize GetBestSize(wxGrid& grid,
                               wxGridCellAttr& attr,
                               wxDC& dc,
                               int row, int col) wxOVERRIDE;

    virtual wxGridCellRenderer *Clone() const wxOVERRIDE;

protected:
    wxString GetString(const wxGrid& grid, int row, int col);
};


MyGridCellEnumRenderer::MyGridCellEnumRenderer(const wxString& choices)
{
    if (!choices.empty())
        SetParameters(choices);
}

wxGridCellRenderer *MyGridCellEnumRenderer::Clone() const
{
    MyGridCellEnumRenderer *renderer = new MyGridCellEnumRenderer;
    renderer->m_choices = m_choices;
    return renderer;
}

wxString MyGridCellEnumRenderer::GetString(const wxGrid& grid, int row, int col)
{

    wxGridTableBase *table = grid.GetTable();
    wxString text;
    long choiceno;
 

    text = table->GetValue(row, col).Trim();

    if(text.IsEmpty() | text.IsNull() | !text.IsNumber()){
        text="";
    }else{
        if(text.Length()==0){
            text ="";
        }else{
            choiceno = wxAtol(text.Trim());
            text.Printf(wxT("%s"), m_choices[ choiceno ].c_str() );
        }
    }
    //If we faild to parse string just show what we where given?
    return text;
}

void MyGridCellEnumRenderer::Draw(wxGrid& grid,
                                   wxGridCellAttr& attr,
                                   wxDC& dc,
                                   const wxRect& rectCell,
                                   int row, int col,
                                   bool isSelected)
{
    wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected);

    SetTextColoursAndFont(grid, attr, dc, isSelected);

    wxRect rect = rectCell;
    rect.Inflate(-1);

    // draw the text right aligned by default
    grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, attr,
                           wxALIGN_RIGHT);
}

wxSize MyGridCellEnumRenderer::GetBestSize(wxGrid& grid,
                                            wxGridCellAttr& attr,
                                            wxDC& dc,
                                            int row, int col)
{
    return DoGetBestSize(attr, dc, GetString(grid, row, col));
}
 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值