wxGraphicsContext是wxWidgets高級繪圖類。windows
wx的手冊上有個以下的Demo:this
void MyCanvas::OnPaint(wxPaintEvent &event) { // Create paint DC wxPaintDC dc(this); // Create graphics context from it wxGraphicsContext *gc = wxGraphicsContext::Create( dc ); if (gc) { // make a path that contains a circle and some lines gc->SetPen( *wxRED_PEN ); wxGraphicsPath path = gc->CreatePath(); path.AddCircle( 50.0, 50.0, 50.0 ); path.MoveToPoint(0.0, 50.0); path.AddLineToPoint(100.0, 50.0); path.MoveToPoint(50.0, 0.0); path.AddLineToPoint(50.0, 100.0 ); path.CloseSubpath(); path.AddRectangle(25.0, 25.0, 50.0, 50.0); gc->StrokePath(path); delete gc; } }
我通常在windows使用wx,可事實上,這段代碼沒效果。code
因此我就去看了一下sample(Drawer)ci
精簡了一下,使用方法以下:get
wxGCDC gdc; wxGraphicsRenderer* const renderer = wxGraphicsRenderer::GetDefaultRenderer(); wxGraphicsContext* gc; if ( wxPaintDC *paintdc = wxDynamicCast(&dc, wxPaintDC) ) { gc = renderer->CreateContext(*paintdc); } else if ( wxMemoryDC *memdc = wxDynamicCast(&dc, wxMemoryDC) ) { gc = renderer->CreateContext(*memdc); } if (gc) { gc->SetPen( *wxRED_PEN ); wxGraphicsPath path = gc->CreatePath(); //操做path ... //stroke是顯示路徑 gc->StrokePath(path); //另外fillPath是填充路徑,closePath是鏈接首尾點,會多一條線出來 }