wxPanel colored borders and background

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!
Post Reply
lscpaiva
Knows some wx things
Knows some wx things
Posts: 41
Joined: Fri Apr 22, 2005 9:16 pm
Location: S

wxPanel colored borders and background

Post by lscpaiva » Sat Jul 02, 2005 12:09 am

I don't know if this is the right forum to post this message. If it don't be, pleasy sorry!

Frequently I am seeing people asking for some code to colouring the background and to put borders into panels in wxWidgets.

Below is shown how to do this.

BTW in the form shown, the only button is running obviously is the EXIT one.

Regards

Luiz Sergio

Code: Select all

//*****************************************************************************
//FILE: WXWIDGETSAPP.H
//*****************************************************************************

#ifndef __WXWIDGETSAPP_H
#define __WXWIDGETSAPP_H

#include <wx/wx.h>
// ----------------------------------------------------------------------------
class wxWidgetsApp : public wxApp
{
public:
    wxWidgetsApp();
    virtual ~wxWidgetsApp();
    virtual bool OnInit();
};
// ----------------------------------------------------------------------------
DECLARE_APP(wxWidgetsApp)
// ----------------------------------------------------------------------------
#endif //__WXWIDGETSAPP_H


//*****************************************************************************
//FILE: WXWIDGETSAPP.CPP
//*****************************************************************************

#include "wxWidgetsApp.h"
#include "wxWidgetsFrame.h"
// ----------------------------------------------------------------------------
IMPLEMENT_APP(wxWidgetsApp)
// ============================================================================
// implementation
// ============================================================================
wxWidgetsApp::wxWidgetsApp()
{
}
// ----------------------------------------------------------------------------
wxWidgetsApp::~wxWidgetsApp()
{
}
// ----------------------------------------------------------------------------
bool wxWidgetsApp::OnInit()
{
  wxWidgetsFrame *frame = new wxWidgetsFrame(NULL,
                                             -1,
                                             wxT(""),
                                             wxPoint(20,20),
                                             wxSize(499,340));
  frame->Show(true);
  frame->Centre();
  SetTopWindow(frame);
  frame->Rectangle();
  return true;
}
// ----------------------------------------------------------------------------


//*****************************************************************************
FILE: WXWIDGETSFRAME.H
//*****************************************************************************

#ifndef __WXWIDGETSFRAME_H
#define __WXWIDGETSFRAME_H
// ----------------------------------------------------------------------------
#include <wx/wx.h>
// ----------------------------------------------------------------------------
class Panel1 : public wxPanel 
{ 
public: 
  Panel1(wxWindow * parent,wxWindowID id) : wxPanel(parent, -1) 
  { 
    SetBackgroundColour(wxColour(200,255,200)); 
  }
  
  DECLARE_CLASS(Panel1) 
}; 
// ----------------------------------------------------------------------------
class Panel2 : public wxPanel 
{ 
public: 
  Panel2(wxWindow * parent,wxWindowID id) : wxPanel(parent, -1) 
  { 
    SetBackgroundColour(wxColour(200,150,200)); 
  } 
}; 
// ----------------------------------------------------------------------------
class Panel3 : public wxPanel 
{ 
public: 
  Panel3(wxWindow * parent,wxWindowID id) : wxPanel(parent, -1) 
  { 
    SetBackgroundColour(wxColour(200,255,200)); 
  } 
}; 
// ----------------------------------------------------------------------------
class wxWidgetsFrame : public wxFrame
{
public:
  wxWidgetsFrame(wxWindow *parent,
                 wxWindowID id,
                 const wxString &title,
                 const wxPoint& pos = wxDefaultPosition,
                 const wxSize& size = wxDefaultSize,
                 long style = wxNO_BORDER);
  virtual ~wxWidgetsFrame();
  void OnButton5( wxCommandEvent &event );	
  void Rectangle();

private:
  wxPanel* itemPanel1;
  wxPanel* itemPanel6;
  wxPanel* itemPanel14;
  wxPanel* itemPanel25;
        
  DECLARE_CLASS(wxWidgetsFrame)

  DECLARE_EVENT_TABLE()
};
// ----------------------------------------------------------------------------
#endif //__WXWIDGETSFRAME_H


//*****************************************************************************
FILE: WXWIDGETSFRAME.CPP
//*****************************************************************************


#include "wxWidgetsApp.h"
#include "wxWidgetsFrame.h"
// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------
enum {
  ID_PANEL=1,
  ID_PANEL1,
  ID_PANEL2,
  ID_BUTTON,
  ID_BUTTON1,
  ID_BUTTON2,
  ID_BUTTON3,
  ID_BUTTON4,
  ID_BUTTON5
};
// ----------------------------------------------------------------------------
IMPLEMENT_CLASS(Panel1, wxPanel)

IMPLEMENT_CLASS(wxWidgetsFrame, wxFrame)
// ----------------------------------------------------------------------------
BEGIN_EVENT_TABLE(wxWidgetsFrame, wxFrame)
	EVT_BUTTON (ID_BUTTON5, wxWidgetsFrame::OnButton5)
END_EVENT_TABLE()
// ----------------------------------------------------------------------------
// main frame
// ----------------------------------------------------------------------------
wxWidgetsFrame::wxWidgetsFrame(wxWindow *parent,
                               wxWindowID id,
                               const wxString &title,
                               const wxPoint &position,
                               const wxSize& size,
                               long style)
              : wxFrame(parent, id, title, position, size, style)
{
  wxColour *m_colourBackground; 
  m_colourBackground = wxTheColourDatabase->FindColour("WHEAT");   
  SetBackgroundColour(*m_colourBackground); 

  itemPanel1 = new wxPanel( this, ID_PANEL);
  
  wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
  itemPanel1->SetSizer(itemBoxSizer2);

  itemBoxSizer2->Add(5, 26, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);

  wxBoxSizer* itemBoxSizer4 = new wxBoxSizer(wxHORIZONTAL);
  itemBoxSizer2->Add(itemBoxSizer4, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);

  itemBoxSizer4->Add(200, 5, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

  itemPanel6 = new wxPanel(this,  
                           ID_PANEL);
                          
  itemPanel6->SetBackgroundColour("WHITE");                         
  itemBoxSizer4->Add(itemPanel6, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

  wxBoxSizer* itemBoxSizer7 = new wxBoxSizer(wxVERTICAL);
  itemPanel6->SetSizer(itemBoxSizer7);

  wxStaticText* itemStaticText8 = new wxStaticText(itemPanel6,
                                                   wxID_STATIC, 
                                                   _("Static text"), 
                                                   wxDefaultPosition, 
                                                   wxDefaultSize, 
                                                   0);
                                                   
  itemBoxSizer7->Add(itemStaticText8,
                     0,
                     wxALIGN_CENTER_HORIZONTAL|wxALL|wxADJUST_MINSIZE,
                     5);

  wxStaticText* itemStaticText9 = new wxStaticText(itemPanel6,
                                                   wxID_STATIC,
                                                   _("Static text"),
                                                   wxDefaultPosition,
                                                   wxDefaultSize,
                                                   0);
                                                   
  itemBoxSizer7->Add(itemStaticText9,
                     0,
                     wxALIGN_CENTER_HORIZONTAL|wxALL|wxADJUST_MINSIZE,
                     5);

  itemBoxSizer4->Add(200, 5, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

  itemBoxSizer2->Add(5, 26, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);

  wxBoxSizer* itemBoxSizer12 = new wxBoxSizer(wxHORIZONTAL);
  itemBoxSizer2->Add(itemBoxSizer12, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);

  itemBoxSizer12->Add(5, 5, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

  itemPanel14 = new Panel2(itemPanel1,
                            ID_PANEL1);
                            
  itemPanel1->SetBackgroundColour("YELLOW");                          
  itemBoxSizer12->Add(itemPanel14, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

  wxGridSizer* itemGridSizer15 = new wxGridSizer(2, 2, 0, 0);
  itemPanel14->SetSizer(itemGridSizer15);

  wxButton* itemButton16 = new wxButton(itemPanel14,
                                        ID_BUTTON,
                                        _("Button"),
                                        wxDefaultPosition,
                                        wxDefaultSize,
                                        0);
                                        
  itemGridSizer15->Add(itemButton16,
                       0,
                       wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxALL,
                       5);

  wxButton* itemButton17 = new wxButton(itemPanel14,
                                        ID_BUTTON1,
                                        _("Button"),
                                        wxDefaultPosition,
                                        wxDefaultSize,
                                        0);
                                
  itemGridSizer15->Add(itemButton17,
                       0,
                       wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxALL,
                       5);

  wxButton* itemButton18 = new wxButton(itemPanel14,
                                        ID_BUTTON2,
                                        _("Button"),
                                        wxDefaultPosition,
                                        wxDefaultSize,
                                        0);
                                        
  itemGridSizer15->Add(itemButton18,
                       0,
                       wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxALL,
                       5);

  wxButton* itemButton19 = new wxButton(itemPanel14,
                                        ID_BUTTON3,
                                        _("Button"),
                                        wxDefaultPosition,
                                        wxDefaultSize,
                                        0);
                                        
  itemGridSizer15->Add(itemButton19,
                       0,
                       wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxALL,
                       5);

  itemBoxSizer12->Add(5, 5, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

  itemBoxSizer2->Add(5, 26, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);

  wxBoxSizer* itemBoxSizer22 = new wxBoxSizer(wxHORIZONTAL);
  itemBoxSizer2->Add(itemBoxSizer22, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);

  itemBoxSizer22->Add(5, 5, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

  wxBoxSizer* itemBoxSizer24 = new wxBoxSizer(wxHORIZONTAL);
  itemBoxSizer22->Add(itemBoxSizer24, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

  itemPanel25 = new Panel3(itemPanel1,
                            ID_PANEL2);
                            
  itemBoxSizer24->Add(itemPanel25, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

  wxBoxSizer* itemBoxSizer26 = new wxBoxSizer(wxHORIZONTAL);
  itemPanel25->SetSizer(itemBoxSizer26);

  wxButton* itemButton27 = new wxButton(itemPanel25,
                                        ID_BUTTON4,
                                        _("Button"),
                                        wxDefaultPosition,
                                        wxDefaultSize,
                                        0);
                                        
  itemBoxSizer26->Add(itemButton27, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

  wxButton* itemButton28 = new wxButton(itemPanel25,
                                        ID_BUTTON5,
                                        _("Exit"),
                                        wxDefaultPosition,
                                        wxDefaultSize,
                                        0);
                                        
  itemBoxSizer26->Add(itemButton28, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

  itemBoxSizer22->Add(5, 5, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

  itemBoxSizer2->Add(5, 5, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);
	
  itemPanel1->SetSizer(itemBoxSizer2);
  itemBoxSizer2->SetSizeHints(itemPanel1);
}
// ----------------------------------------------------------------------------
wxWidgetsFrame::~wxWidgetsFrame()
{
}
// ----------------------------------------------------------------------------
void wxWidgetsFrame::OnButton5( wxCommandEvent &event )
{
  Close(TRUE);
}
// ----------------------------------------------------------------------------
void wxWidgetsFrame::Rectangle()
{
  wxRect Rect;
  
  wxClientDC cdc(itemPanel1);
  PrepareDC(cdc);
  cdc.SetBrush(*wxTRANSPARENT_BRUSH);
  
  wxPen Pen("RED", 2, wxSOLID);
  cdc.SetPen(Pen);
  Rect = itemPanel6->GetRect();
  cdc.DrawRectangle(Rect.x,Rect.y,Rect.width,Rect.height);

  Pen.SetColour("FOREST GREEN");
  cdc.SetPen(Pen);
  Rect = itemPanel14->GetRect();
  cdc.DrawRectangle(Rect.x-1,Rect.y-1,Rect.width+3,Rect.height+3);
  
  Pen.SetColour("BLUE");
  cdc.SetPen(Pen);
  Rect = itemPanel25->GetRect();
  cdc.DrawRectangle(Rect.x-1,Rect.y-1,Rect.width+3,Rect.height+3);
}
//----------------------------------------------------------------------------

//*****************************************************************************

daddydave
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 214
Joined: Wed Jun 15, 2005 3:31 am
Location: United States
Contact:

Post by daddydave » Wed Jul 20, 2005 5:13 pm

A moderator will probably move it to The Code Dump forum. I'll probably need an example like this, so :D thanks!

NinjaNL
Moderator
Moderator
Posts: 899
Joined: Sun Oct 03, 2004 10:33 am
Location: Oosterwolde, Netherlands

Post by NinjaNL » Wed Jul 20, 2005 5:20 pm

Moved to the Code Dump
Follow the development of my screenplay authoring program at http://wxscreenplaywriter.blogspot.com/

dsk
Experienced Solver
Experienced Solver
Posts: 91
Joined: Sat Jun 25, 2005 12:10 pm
Location: Poland

Post by dsk » Wed Jul 20, 2005 5:57 pm

Lscpaiva please post screenshot, it will be very helpfull - not only for me ...

Have a nice day,
dsk

Post Reply