wxWidgets: Transparent Static Text Controls

So all-in-all I’m really impressed with wxWidgets. Most of the time it is pretty straight forward and easy to figure out, but on Wednesday night through Thursday I was frustrated with the framework. The documentation made it sound easy, and in the end it was easy, but getting their wasn’t so easy.

I went through about 15 iterations before finding the right solution. My worst solution took 2 minutes to finish a re-paint, but it finally did paint everything the right way. During the 2 minutes though there was enough flicker it might have sent somebody into convulsions. I even attempted to use Google to see what others had done, most of the references were really old and stale, all of them didn’t seem to work anymore with 2.6. So I’m posting my solution for others to find and use.

Header File:

   1:  class CTransparentStaticText : public wxStaticText
   2:  { 
   3:      DECLARE_DYNAMIC_CLASS (CTransparentStaticText)
   4:
   5:  public:
   6:      CTransparentStaticText();
   7:      CTransparentStaticText(
   8:          wxWindow* parent,
   9:          wxWindowID id,
  10:          const wxString& label,
  11:          const wxPoint& pos = wxDefaultPosition,
  12:          const wxSize& size = wxDefaultSize,
  13:          long style = 0,
  14:          const wxString& name= wxStaticTextNameStr
  15:      );
  16:
  17:      bool Create(
  18:          wxWindow* parent,
  19:          wxWindowID id,
  20:          const wxString& label,
  21:          const wxPoint& pos = wxDefaultPosition,
  22:          const wxSize& size = wxDefaultSize,
  23:          long style = 0,
  24:          const wxString& name= wxStaticTextNameStr
  25:      );
  26:
  27:      virtual bool HasTransparentBackground() { return true; };
  28:
  29:      virtual void OnPaint(wxPaintEvent& event);
  30:
  31:      DECLARE_EVENT_TABLE()
  32:  };

Source File:

   1:  IMPLEMENT_DYNAMIC_CLASS (CTransparentStaticText, wxStaticText)
   2:
   3:  BEGIN_EVENT_TABLE(CTransparentStaticText, wxStaticText)
   4:      EVT_PAINT(CTransparentStaticText::OnPaint)
   5:  END_EVENT_TABLE()
   6:
   7:
   8:  CTransparentStaticText::CTransparentStaticText() {}
   9:
  10:  CTransparentStaticText::CTransparentStaticText(wxWindow* parent, wxWindowID id, const wxString& label,
         const wxPoint& pos, const wxSize& size, long style, const wxString& name ) {
  11:      Create(parent, id, label, pos, size, style, name);
  12:  }
  13:
  14:
  15:  bool CTransparentStaticText::Create(wxWindow* parent, wxWindowID id, const wxString& label,
         const wxPoint& pos, const wxSize& size, long style, const wxString& name ) {
  16:      bool bRetVal = wxStaticText::Create(parent, id, label, pos, size, style|wxTRANSPARENT_WINDOW, name);
  17:
  18:      SetBackgroundColour(parent->GetBackgroundColour());
  19:      SetBackgroundStyle(wxBG_STYLE_COLOUR);
  20:      SetForegroundColour(parent->GetForegroundColour());
  21:
  22:      return bRetVal;
  23:  }
  24:
  25:
  26:  void CTransparentStaticText::OnPaint(wxPaintEvent& /*event*/) {
  27:      wxPaintDC dc(this);
  28:      dc.SetFont(GetFont());
  29:      dc.DrawText(GetLabel(), 0, 0);
  30:  }

Thanks it. It took a while to find the fix and simple solution, I'm glad I finally figured it out.

My previous iterations were embarrassing.

----- Rom