From WxWiki
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
Official Classes SmallBlocks.png Archive Containers Controls Data Structures Database Date & Time Debug Device Contexts Dialogs Document & Views Drag & Drop Events Filesystem Frames Graphics Grid Cell Help HTML Logging Miscellaneous Networking Printing Sizers Streams Threading Windows

A wxDC is a "device context" onto which graphics and text can be drawn. It is intended to represent different output devices and offers a common abstract API for drawing on any of them.

wxWidgets offers an alternative drawing API based on the modern drawing backends GDI+, CoreGraphics and Cairo. See wxGraphicsContext, wxGraphicsRenderer and related classes. There is also a wxGCDC linking the APIs by offering the wxDC API on top of a wxGraphicsContext.

wxDC is an abstract base class and cannot be created directly. Use wxPaintDC, wxClientDC, wxWindowDC, wxScreenDC, wxMemoryDC or wxPrinterDC. Notice that device contexts which are associated with windows (i.e. wxClientDC, wxWindowDC and wxPaintDC) use the window font and colours by default (starting with wxWidgets 2.9.0) but the other device context classes use system-default values so you always must set the appropriate fonts and colours before using them.


The 'drawing' example that comes with wxWidgets should be worth checking out if you want to learn how to use wxDC.

  • wxPaintDC must be created on paint events if you catch them (even if you draw nothing yourself)
  • wxClientDC can be used outside paint events
  • wxBufferedDC would be used on top of your selected DC, for flickerless Drawing.
  • You can paint into postscript files with wxPostScriptDC
  • SVG files are supported using wxSVGFileDC (compilation instructions here), found in the contrib folder.

DrawRectangle can also be used with

void DrawRectangle(const wxPoint& pt, const wxSize& sz)
void DrawRectangle(const wxRect& rect)

Minimal drawing sample

#ifndef _drawPane_
#define _drawPane_

#include "wx/wx.h"
#include "wx/glcanvas.h"

class BasicDrawPane : public wxPanel
	BasicDrawPane(wxFrame* parent);
	void render(wxPaintEvent& evt);
	// events
	void mouseMoved(wxMouseEvent& event);
	void mouseDown(wxMouseEvent& event);
	void mouseWheelMoved(wxMouseEvent& event);
	void mouseReleased(wxMouseEvent& event);
	void rightClick(wxMouseEvent& event);
	void mouseLeftWindow(wxMouseEvent& event);
	void keyPressed(wxKeyEvent& event);
	void keyReleased(wxKeyEvent& event);


#include "wx/wx.h"
#include "wx/sizer.h"

class MyApp: public wxApp
    virtual bool OnInit();

    wxFrame *frame;
    BasicDrawPane * drawPane;


bool MyApp::OnInit()
    wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
    frame = new wxFrame((wxFrame *)NULL, -1,  wxT("Hello wxDC"), wxPoint(50,50), wxSize(400,200));
    drawPane = new BasicDrawPane( (wxFrame*) frame );
    sizer->Add(drawPane, 1, wxEXPAND);
    return true;

BEGIN_EVENT_TABLE(BasicDrawPane, wxPanel)

// some useful events to use
void BasicDrawPane::mouseMoved(wxMouseEvent& event) {}
void BasicDrawPane::mouseDown(wxMouseEvent& event) {}
void BasicDrawPane::mouseWheelMoved(wxMouseEvent& event) {}
void BasicDrawPane::mouseReleased(wxMouseEvent& event) {}
void BasicDrawPane::rightClick(wxMouseEvent& event) {}
void BasicDrawPane::mouseLeftWindow(wxMouseEvent& event) {}
void BasicDrawPane::keyPressed(wxKeyEvent& event) {}
void BasicDrawPane::keyReleased(wxKeyEvent& event) {}

BasicDrawPane::BasicDrawPane(wxFrame* parent) :

void BasicDrawPane::render( wxPaintEvent& evt )
    wxPaintDC dc(this); // only to be used in paint events. use wxClientDC to paint outside the paint event
	dc.DrawText(wxT("Testing"), 40, 60); 

ToolTips for objects drawn in an wxDC

Objects that can be draw within a wxDC can be provided with a ToolTip the following way:

  • call the wxDC's SetToolTip() when the mouse cursor is within the object;
  • call the wxDC's ClearToolTip() when the mouse cursor isn't within the object.
  • call wxToolTip::Enable(true);wxToolTip::Enable(false); if the cursor leaves the object: This resets the timer that delays the tooltip's appearance when the cursor enters the object.

See Also