11g.wxWidgets – Shapes

OVERVIEW

Apart from the basic rectangles and lines, there are more complex shapes which can be drawn: Ellipse, Rounded Rectangle, Arc, Circle, Splines and Polygons.

SAMPLE CODE

shapes.h
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
	#include <wx/wx.h>
#endif

class Shapes: public wxFrame {
	public:
		Shapes(const wxString &title);
		void onPaint(wxPaintEvent &event);
	
};

shapes.cpp
#include "shapes.h"

Shapes::Shapes(const wxString &title):
	wxFrame(NULL, -1, title, wxDefaultPosition, wxSize(600,500)) {
	srand(time(NULL));
	Bind(wxEVT_PAINT, &Shapes::onPaint, this);
	SetBackgroundColour(wxColour(255,255,255));
}

void Shapes::onPaint(wxPaintEvent &event) {
	wxPaintDC dc(this);

	dc.DrawEllipse(20, 20, 90, 60);
	dc.DrawRoundedRectangle(130, 20, 90, 60, 10);
	dc.DrawArc(240, 40, 340, 40, 290, 20);
  	dc.DrawRectangle(20, 120, 80, 50);
  	dc.DrawCircle(170, 230, 35);
	dc.DrawRectangle(250, 200, 60, 60);

	dc.SetPen(wxPen(wxColour(100,200,0), 1, wxPENSTYLE_SOLID));

	wxPoint lines [] = { wxPoint(10,160), wxPoint(120, 220),  wxPoint(50, 210), wxPoint(300, 210) };
	dc.DrawLines(4, lines);

	wxPoint splines[] = { wxPoint(240, 170), wxPoint(210, 70), wxPoint(185, 80), wxPoint(125, 110) };
	dc.DrawSpline(4, splines);

	wxPoint polygon[] = { wxPoint(130, 140), wxPoint(180, 170), wxPoint(180, 140), wxPoint(220, 110), wxPoint(140, 100) };
	dc.DrawPolygon(4, polygon);
}

main.h
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
	#include <wx/wx.h>
#endif

class Main: public wxApp {

	public:
		virtual bool OnInit();
};

main.cpp
#include "main.h"
#include "shapes.h"

IMPLEMENT_APP(Main)

bool Main::OnInit() {
	Shapes *app = new Shapes(wxT("Shapes"));
	app->Show(true);

}


The output is shown below

Be the first to comment

Leave a Reply

Your email address will not be published.


*