11f.wxWidgets – Gradient

OVERVIEW

A Gradient is a gradual transition in a color from one shade to another. This is useful for complex coloring and effects. wxWidgets provides two kinds of gradients:

  • GradientFillLinear
  • GradientFillConcentric

GradientFillLinear implements the gradient in a straight linear line from one side of the container to another. GradientFillConcentric implements the gradient in a circular concentric path starting from the innermost circle to the outermost circle

The sample code below shows both the gradient types

SAMPLE CODE

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

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

gradients.cpp
#include "gradients.h"

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

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

	wxColour *color1 = new wxColour(40,50,100);
	wxColour *color2 = new wxColour(90,150,200);
	
	wxColour *color3 = new wxColour(190,150,200);
	wxColour *color4 = new wxColour(4,0,160);

	dc.GradientFillLinear(wxRect(10, 10, 180, 100), *color1, *color2, wxNORTH);
	dc.GradientFillLinear(wxRect(110, 10, 180, 100), *color1, *color2, wxSOUTH);
	dc.GradientFillLinear(wxRect(210, 120, 280, 100), *color1, *color2, wxEAST);
	dc.GradientFillLinear(wxRect(110, 220, 180, 400), *color1, *color2, wxWEST);

	dc.GradientFillLinear(wxRect(10, 310, 180, 300), *color3, *color4, wxNORTH);
	dc.GradientFillLinear(wxRect(110, 410, 180, 400), *color3, *color4, wxSOUTH);
	dc.GradientFillLinear(wxRect(210, 520, 280, 500), *color3, *color4, wxEAST);
	dc.GradientFillLinear(wxRect(110, 620, 180, 600), *color3, *color4, wxWEST);


}

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 "gradients.h"

IMPLEMENT_APP(Main)

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

}

The output is shown below

Be the first to comment

Leave a Reply

Your email address will not be published.


*