デバイスコンテキスト(Device context)とは、ディスプレイやプリンターなどのデバイス(Device、装置)に描画するためのGUIパーツです。wxwidgetsでは、DC(デバイスコンテキスト)に、いろいろなグラフィックスを描きます。
#include <wx/wx.h>
class Line : public wxFrame
{
public:
Line();
void OnPaint(wxPaintEvent & event);
};
class App : public wxApp
{
public:
virtual bool OnInit();
};
Line::Line() : wxFrame(NULL, -1, "Line", wxDefaultPosition, wxSize(400, 340))
{
Connect(wxEVT_PAINT, wxPaintEventHandler(Line::OnPaint));
}
void Line::OnPaint(wxPaintEvent & event)
{
wxPaintDC *dc = new wxPaintDC(this);
dc->SetPen(wxPen(wxColour("yellow"), 1, wxSOLID));
dc->DrawLine(20, 40, 380, 40);
dc->SetPen(wxPen(wxColour("magenta"), 10, wxDOT));
dc->DrawLine(20, 80, 380, 80);
dc->SetPen(wxPen(wxColour("cyan"), 1, wxLONG_DASH));
dc->DrawLine(20, 120, 380, 120);
dc->SetPen(wxPen(wxColour("red"), 10, wxSHORT_DASH));
dc->DrawLine(20, 160, 380, 160);
dc->SetPen(wxPen(wxColour("green"), 1, wxDOT_DASH));
dc->DrawLine(20, 200, 380, 200);
dc->SetPen(wxPen(wxColour("blue"), 10, wxTRANSPARENT));
dc->DrawLine(200, 240, 380, 240);
dc->SetPen(wxPen(wxColour("black")));
dc->DrawLine(20, 280, 380, 280);
}
IMPLEMENT_APP(App)
bool App::OnInit()
{
Line *line = new Line();
line->Show();
return true;
}
#include <wx/wx.h>
#include <time.h>
class Point : public wxFrame
{
public:
Point();
void OnPoint(wxPaintEvent & event);
};
class App : public wxApp
{
public:
virtual bool OnInit();
};
Point::Point() : wxFrame(NULL, -1, "Point")
{
Connect(wxEVT_PAINT, wxPaintEventHandler(Point::OnPoint));
srand(time(NULL));
}
void Point::OnPoint(wxPaintEvent & event)
{
wxPaintDC dc(this);
int x = 0, y = 0;
wxSize size = GetSize();
for (int i = 0; i < 1000; i++) {
x = rand() % size.x + 1;
y = rand() % size.y + 1;
dc.DrawPoint(x, y);
}
}
IMPLEMENT_APP(App)
bool App::OnInit()
{
Point *point = new Point();
point->Show();
return true;
}
#include <wx/wx.h>
class Color : public wxFrame
{
public:
Color();
void OnColor(wxPaintEvent & event);
};
class App : public wxApp
{
virtual bool OnInit();
};
Color::Color() : wxFrame(NULL, -1, "Color", wxDefaultPosition, wxSize(380, 235))
{
Connect(wxEVT_PAINT, wxPaintEventHandler(Color::OnColor));
}
void Color::OnColor(wxPaintEvent & event)
{
wxPaintDC dc(this);
dc.SetPen(wxColour("yellow"));
dc.SetBrush(wxColour("yellow"));
dc.DrawRectangle(20, 20, 100, 75);
dc.SetPen(wxColour("magenta"));
dc.SetBrush(wxColour("magenta"));
dc.DrawRectangle(140, 20, 100, 75);
dc.SetPen(wxColour("cyan"));
dc.SetBrush(wxColour("cyan"));
dc.DrawRectangle(260, 20, 100, 75);
dc.SetPen(wxColour("red"));
dc.SetBrush(wxColour("red"));
dc.DrawRectangle(20, 115, 100, 75);
dc.SetPen(wxColour("green"));
dc.SetBrush(wxColour("green"));
dc.DrawRectangle(140, 115, 100, 75);
dc.SetPen(wxColour("blue"));
dc.SetBrush(wxColour("blue"));
dc.DrawRectangle(260, 115, 100, 75);
}
IMPLEMENT_APP(App)
bool App::OnInit()
{
Color *color = new Color();
color->Show();
return true;
}
#include <wx/wx.h>
class Shape : public wxFrame
{
public:
Shape();
void OnShape(wxPaintEvent & event);
};
class App : public wxApp
{
public:
virtual bool OnInit();
};
Shape::Shape() : wxFrame(NULL, -1, "Shape", wxPoint(-1,-1), wxSize(390, 260))
{
Connect(wxEVT_PAINT, wxPaintEventHandler(Shape::OnShape));
}
void Shape::OnShape(wxPaintEvent & event)
{
wxPaintDC dc(this);
dc.SetPen(wxColour(255, 17, 17));
dc.SetBrush(wxColour(17, 255, 17));
//dc.SetBrush(wxColour("#777777"));
dc.DrawEllipse(20, 20, 100, 75);
dc.DrawRoundedRectangle(140, 20, 100, 75, 10);
dc.DrawArc(250, 57, 370, 57,310, 20);
dc.DrawCircle(70, 165,50);
dc.DrawRectangle(140, 115, 100, 100);
dc.DrawText("Hello wxWidgets!", 270, 160);
}
IMPLEMENT_APP(App)
bool App::OnInit()
{
Shape *shape = new Shape();
shape->Show();
return true;
}