This is part of a more general work dedicated to gtkmm accessible here. This example shows how to create a drawing area with Cairo.
Download

gtkmm - example 14 1.71 KB
Source code
main.cpp
#include
#include
int main(int argc, char* argv[])
{
// Initialize gtkmm and create the main window
Glib::RefPtr app = Gtk::Application::create(argc, argv, "www.lucidarme.me");
Gtk::Window window;
// Create the drawing
Drawing Dwg;
// Insert the drawing in the window
window.add(Dwg);
// Show the drawing
Dwg.show();
// Start main loop
return app->run(window);
}
drawing.h
#ifndef DRAWING_H
#define DRAWING_H
#include
#include
class Drawing : public Gtk::DrawingArea
{
public:
Drawing();
protected:
//Override default signal handler:
virtual bool on_draw(const Cairo::RefPtr& cr);
};
#endif // DRAWING_H
drawing.cpp
#include "drawing.h"
Drawing::Drawing()
{
}
bool Drawing::on_draw(const Cairo::RefPtr& cr)
{
// Set color
cr->set_source_rgb(0, 0.0, 1.0);
// Set line width
cr->set_line_width(15.0);
// Set line cap
cr->set_line_cap(Cairo::LINE_CAP_ROUND);
// Draw the lines
cr->move_to(50, 50);
cr->line_to(150, 150);
cr->move_to(150,50);
cr->line_to(50, 150);
// Apply drawing
cr->stroke();
return true;
}