That seems to be a little complicated to me. (Also, unless I'm missing something you never seem to draw anything in your paint handler). I would just create a graphics context in the paint handler and draw to it there. Like so:
Code: Select all
// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
// for all others, include the necessary headers (this file is usually all you
// need because it includes almost all "standard" wxWidgets headers)
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
#include <wx/graphics.h>
#include <wx/dcbuffer.h>
class widget: public wxWindow
{
public:
widget(wxWindow *parent, wxWindowID id=wxID_ANY,
const wxPoint &pos=wxDefaultPosition,
const wxSize &size=wxDefaultSize,
long style=0, const wxString &name="widget");
private:
void OnPaint( wxPaintEvent& event );
wxColour m_WidgetBkColor,m_PlotBkColor,m_PlotLabelColor;
wxFont m_PlotLabelFont;
};
widget::widget(wxWindow *parent, wxWindowID id, const wxPoint &pos,
const wxSize &size, long style, const wxString &name)
:wxWindow(parent, id, pos, size, style|wxFULL_REPAINT_ON_RESIZE, name)
{
m_WidgetBkColor = wxColor( 200, 200, 200 );
m_PlotBkColor = wxColor( 255, 255, 255 );
m_PlotLabelColor = wxColor( 0, 0, 0 );
m_PlotLabelFont = wxFont( 18, wxSWISS, wxFONTSTYLE_NORMAL, wxNORMAL, false, _("Elephant") );
SetBackgroundStyle(wxBG_STYLE_PAINT);
Bind( wxEVT_PAINT, &widget::OnPaint, this );
}
void widget::OnPaint( wxPaintEvent& event )
{
wxAutoBufferedPaintDC dc(this);
wxGraphicsContext* gc = wxGraphicsContext::Create( dc );
wxRect rc = GetClientRect();
gc->SetBrush( wxBrush(m_WidgetBkColor) );
gc->SetPen( wxPen(m_WidgetBkColor, 1, wxSOLID) );
gc->DrawRectangle( rc.x, rc.y, rc.width, rc.height );
gc->SetFont( m_PlotLabelFont, m_PlotLabelColor );
gc->DrawText( _("SOME TEXT"), rc.width/2, rc.height/2 );
delete gc;
}
class MyFrame : public wxFrame
{
public:
MyFrame( wxWindow* parent, int id = wxID_ANY, wxString title = "Demo"
, wxPoint pos = wxDefaultPosition, wxSize size = wxDefaultSize
, int style = wxDEFAULT_FRAME_STYLE|wxTAB_TRAVERSAL );
};
MyFrame::MyFrame( wxWindow* parent, int id, wxString title, wxPoint pos
, wxSize size, int style )
:wxFrame( parent, id, title, pos, size, style )
{
new widget( this );
}
class MyApp : public wxApp
{
public:
virtual bool OnInit()
{
MyFrame* frame = new MyFrame(NULL);
frame->Show();
return true;
}
};
wxIMPLEMENT_APP(MyApp);
But I'm guessing the reason you're using a buffer is that the actual drawing will ultimately be much more complicated and time consuming so you want to cache a bitmap and only recompute it when needed. If that's the case, here's one possible way to do that:
Code: Select all
class widget: public wxWindow
{
public:
widget(wxWindow *parent, wxWindowID id=wxID_ANY,
const wxPoint &pos=wxDefaultPosition,
const wxSize &size=wxDefaultSize,
long style=0, const wxString &name="widget");
private:
void OnPaint( wxPaintEvent& event );
void OnSize(wxSizeEvent& event);
wxColour m_WidgetBkColor,m_PlotBkColor,m_PlotLabelColor;
wxFont m_PlotLabelFont;
wxBitmap m_Buffer;
bool m_dirty;
};
widget::widget(wxWindow *parent, wxWindowID id, const wxPoint &pos,
const wxSize &size, long style, const wxString &name)
:wxWindow(parent, id, pos, size, style|wxFULL_REPAINT_ON_RESIZE, name)
{
m_WidgetBkColor = wxColor( 200, 200, 200 );
m_PlotBkColor = wxColor( 255, 255, 255 );
m_PlotLabelColor = wxColor( 0, 0, 0 );
m_PlotLabelFont = wxFont( 18, wxSWISS, wxFONTSTYLE_NORMAL, wxNORMAL, false, _("Elephant") );
SetBackgroundStyle(wxBG_STYLE_PAINT);
Bind( wxEVT_PAINT, &widget::OnPaint, this );
Bind( wxEVT_SIZE, &widget::OnSize, this );
}
void widget::OnSize( wxSizeEvent& event )
{
m_dirty=true;
event.Skip();
}
void widget::OnPaint( wxPaintEvent& event )
{
if(m_dirty)
{
wxSize sz = GetClientSize();
m_Buffer = wxBitmap( sz.GetWidth(), sz.GetHeight() );
wxMemoryDC dc(m_Buffer);
wxGraphicsContext* gc = wxGraphicsContext::Create( dc );
wxRect rc = GetClientRect();
gc->SetBrush( wxBrush(m_WidgetBkColor) );
gc->SetPen( wxPen(m_WidgetBkColor, 1, wxSOLID) );
gc->DrawRectangle( rc.x, rc.y, rc.width, rc.height );
gc->SetFont( m_PlotLabelFont, m_PlotLabelColor );
gc->DrawText( _("SOME TEXT"), rc.width/2, rc.height/2 );
delete gc;
m_dirty=false;
}
wxPaintDC dc(this);
dc.DrawBitmap(m_Buffer,0,0);
}