WxClientDC and wxBitmap problem

Volker Bartheld dr_versaeg at freenet.de
Wed Aug 9 11:14:01 PDT 2006


Hi!

On Wed, 9 Aug 2006 19:49:43 +0200 (CEST), jgomez at net2u.es wrote :
>I'm developing a class that extends from wxWindow class.I want to drawan
>image  to the window client area (i want to develop something like a
>clickable wxBitmap).

wxBitmapButton exists. If you remove the wxBU_AUTODRAW style, you can
specify your state images yourself and probably get the "flat" look
youre looking for.

If you want to do it entirely yourself, I suggest basing on wxPanel and
not using wxDC::Blit() for the bitmap but wxDC::DrawBitmap() instead. If
you need fancy stuff with transparent backgrounds, have a look at
EVT_ERASE_BACKGROUND and
wxWindow::GetBackgroundStyle(wxBG_STYLE_CUSTOM).

Here's a little codesnippet that also supports indicating that the
bitmap is focused and sends wxEVT_COMMAND_BUTTON_CLICKED to the parent
if you click it (optional).

HTH,
Volker 

 class wxDynBitmap : public wxPanel
 {
   DECLARE_DYNAMIC_CLASS(wxDynBitmap)
   DECLARE_EVENT_TABLE()
 public:
   wxDynBitmap::wxDynBitmap(wxWindow *parent, const wxBitmap& Bitmap, wxWindowID winid=wxID_ANY, const wxPoint& pos=wxDefaultPosition, const wxSize& size=wxDefaultSize, long style=wxTAB_TRAVERSAL|wxNO_BORDER, bool AcceptFocus=false, const wxString& name=wxT("wxDynBitmap"))
     : wxPanel(parent, winid, pos, size, style, name), m_pBitmap(NULL), m_bAcceptsFocus(AcceptFocus), m_bHasFocus(false)
   {
     SetBitmap(Bitmap);
     if(m_bAcceptsFocus=!!(style&wxTAB_TRAVERSAL)) SetCursor(wxCURSOR_HAND);
   }
   void wxDynBitmap::SetBitmap(const wxBitmap& bitmap)
   {
     if(m_pBitmap) delete m_pBitmap;
     m_pBitmap=new wxBitmap(bitmap);
     SetBestSize(wxSize(bitmap.GetWidth(), bitmap.GetHeight()));
   } // void wxDynBitmap::SetBitmap(const wxBitmap& bitmap)
   wxSize wxDynBitmap::DoGetBestSize() { return wxSize(m_pBitmap->GetWidth(), m_pBitmap->GetHeight()); }
   void wxDynBitmap::OnLeftUp(wxMouseEvent& WXUNUSED(event))
   {
     if(!m_bAcceptsFocus) return;
     wxCommandEvent evt(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
     AddPendingEvent(evt);
   } // void wxDynBitmap::OnLeftUp(wxMouseEvent& WXUNUSED(event))
   void OnKeyUp(wxKeyEvent& event)
   {
     if(WXK_SPACE==event.GetKeyCode()) { wxMouseEvent evt; OnLeftUp(evt); }
     else event.Skip(true);
   } // void OnKeyUp(wxKeyEvent& event)
   bool wxDynBitmap::AcceptsFocus() const { return m_bAcceptsFocus; }
   void wxDynBitmap::OnPaint(wxPaintEvent& WXUNUSED(event))
   {
     if(!m_pBitmap->Ok()) return;
     wxPaintDC dc(this);
     dc.DrawBitmap(*m_pBitmap, 0, 0, true);
     if(m_bHasFocus)
     {
       dc.SetPen(wxPen(wxColour(0xC0, 0xC0, 0xC0), 1, wxSOLID));
       dc.SetBrush(*wxTRANSPARENT_BRUSH);
       dc.DrawRectangle(wxRect(0, 0, m_pBitmap->GetWidth(), m_pBitmap->GetHeight()));
     } // if(m_bHasFocus)
   } // void wxDynBitmap::OnPaint(wxPaintEvent& WXUNUSED(event))
   wxDynBitmap::~wxDynBitmap() { delete m_pBitmap; }
 protected:
   void OnSetFocus(wxFocusEvent& event)  { m_bHasFocus=true; Refresh(); }
   void OnKillFocus(wxFocusEvent& event) { m_bHasFocus=false; Refresh(); }
 private:
   wxDynBitmap::wxDynBitmap() {}
   wxBitmap *m_pBitmap;
   bool m_bAcceptsFocus, m_bHasFocus;
 };
 IMPLEMENT_DYNAMIC_CLASS(wxDynBitmap, wxPanel)
 BEGIN_EVENT_TABLE(wxDynBitmap, wxPanel)
 EVT_PAINT(wxDynBitmap::OnPaint)
 EVT_LEFT_UP (wxDynBitmap::OnLeftUp)
 EVT_KEY_UP(wxDynBitmap::OnKeyUp)
 EVT_SET_FOCUS(wxDynBitmap::OnSetFocus)
 EVT_KILL_FOCUS(wxDynBitmap::OnKillFocus)
 END_EVENT_TABLE()

__
Mail replies to/an V B A R T H E L D at G M X dot D E






More information about the wx-users mailing list