Transparent Static Text Control

If you have a cool piece of software to share, but you are not hosting it officially yet, please dump it in here. If you have code snippets that are useful, please donate!
pidel
Experienced Solver
Experienced Solver
Posts: 89
Joined: Fri Sep 29, 2006 11:04 pm
Location: near Hamburg, Germany

Transparent Static Text Control

Postby pidel » Sat Dec 30, 2006 7:40 pm

I needed a StaticText Control with transparent background, but my search on this forum and on other sites didn
Systems: Mac OSX 10.4.8, OpenSuSE 10.2, WinXP SP2
wxWidgets: 2.8.0
IDE: KDevelop 3.3.4, Xcode 2.4, wxDev-C++ 6.10

pidel
Experienced Solver
Experienced Solver
Posts: 89
Joined: Fri Sep 29, 2006 11:04 pm
Location: near Hamburg, Germany

Postby pidel » Sun Dec 31, 2006 12:14 am

I just noticed some weird behaviour when using more than one instance (two in my case), the OnPaint event draws on both instances when one of them is changed with ctrl->SetLabel(). Using a wxPaintDC instead of the wxAutoBufferedDC makes the background transparent but lets the desktop shine through instead of the panels background. I
Systems: Mac OSX 10.4.8, OpenSuSE 10.2, WinXP SP2

wxWidgets: 2.8.0

IDE: KDevelop 3.3.4, Xcode 2.4, wxDev-C++ 6.10

wxBen
Knows some wx things
Knows some wx things
Posts: 46
Joined: Wed Jun 06, 2012 4:44 pm
Location: Calgary, Canada

Re: Transparent Static Text Control

Postby wxBen » Fri Jul 06, 2012 11:00 pm

I needed the same and found the following somewhere while Googling. It works well.

Any thoughts on whether I should submit a patch around this for consideration? Or is an entire Windows specific class a little specialized?

Code: Select all

class TransparentStaticText : public wxStaticText
   {
   DECLARE_DYNAMIC_CLASS (TransparentStaticText)

public:
   TransparentStaticText()
      {
      }
   TransparentStaticText(wxWindow* parent,
                         wxWindowID id,
                         const wxString& label,
                         const wxPoint& pos = wxDefaultPosition,
                         const wxSize& size = wxDefaultSize,
                         long style = 0,
                         const wxString& name= wxStaticTextNameStr)
      {
      Create(parent, id, label, pos, size, style, name);
      }

   bool Create(wxWindow* parent,
               wxWindowID id,
               const wxString& label,
               const wxPoint& pos = wxDefaultPosition,
               const wxSize& size = wxDefaultSize,
               long style = 0,
               const wxString& name= wxStaticTextNameStr)
      {
      bool ret = wxStaticText::Create(parent, id, label, pos, size, style|wxTRANSPARENT_WINDOW, name);
      return ret;
      }
   virtual void OnPaint(wxPaintEvent& event)
      {
      wxPaintDC dc(this);
      dc.SetFont(GetFont());
      dc.SetTextForeground(GetForegroundColour());
      dc.DrawText(GetLabel(), 0, 0); // simple, no aligment check
      }
   DECLARE_EVENT_TABLE()
   };

IMPLEMENT_DYNAMIC_CLASS (TransparentStaticText, wxStaticText)

BEGIN_EVENT_TABLE(TransparentStaticText, wxStaticText)
   EVT_PAINT(TransparentStaticText::OnPaint)
END_EVENT_TABLE()

PB
Part Of The Furniture
Part Of The Furniture
Posts: 1688
Joined: Sun Jan 03, 2010 5:45 pm

Re: Transparent Static Text Control

Postby PB » Sat Jul 07, 2012 12:33 pm

wxBen wrote:Any thoughts on whether I should submit a patch around this for consideration?

My 2 cents: No.

As I see it, that custom "static text" doesn't seem to have much in common with a real wxStaticText. Does it honour text alignment? Does it support text markup? Does it support ellipsization and wrapping? It looks like it fails even at basic keyboard short cut drawing (a character following an ampersand should be drawn underlined) and as its side effect it also reports wrong control size (as if ampersand was not drawn, resulting in the control size being too small).

Also
wxWidgets reference manual wrote:Paint event is sent when a window's contents needs to be repainted.
Please notice that in general it is impossible to change the drawing of a standard control (such as wxButton) and so you shouldn't attempt to handle paint events for them as even if it might work on some platforms, this is inherently not portable and won't work everywhere.

wxBen
Knows some wx things
Knows some wx things
Posts: 46
Joined: Wed Jun 06, 2012 4:44 pm
Location: Calgary, Canada

Re: Transparent Static Text Control

Postby wxBen » Wed Jul 18, 2012 10:39 pm

Rather than the TransparentStaticText class, which should really not be necessary yet there is no workaround, I believe I have fixed the root cause of these issues, as explained here:

http://trac.wxwidgets.org/ticket/14492

The proposed code change allows you to have a truly transparent static text over something like a ribbon bar which has a gradient colour background using code like this:

wxStaticText* text = new wxStaticText(panel, wxID_ANY, wxT("StaticText"), wxDefaultPosition, wxDefaultSize, wxTRANSPARENT_WINDOW);

This, and transparent checkboxes, is also demonstrated in the ribbon bar demo sample, proposed version that is.


Return to “The Code Dump”

Who is online

Users browsing this forum: No registered users and 1 guest