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(900,600)) {
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(0,0,160);
wxColour *color5 = new wxColour(114,50,100);
wxColour *color6 = new wxColour(50,30,200);
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.GradientFillConcentric(wxRect(290, 210, 280, 300), *color3, *color4);
dc.GradientFillConcentric(wxRect(430, 210, 480, 400), *color5, *color6);
}
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

Leave a Reply