wxDC

From WxWiki
Jump to navigation Jump to search
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.

Features

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
{
    
public:
	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);
    
	DECLARE_EVENT_TABLE()
};

#endif 




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

class MyApp: public wxApp
{
    virtual bool OnInit();

    wxFrame *frame;
    BasicDrawPane * drawPane;
public:
      
};

IMPLEMENT_APP(MyApp)


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);
	
    frame->SetSizer(sizer);
    frame->SetAutoLayout(true);
	
    frame->Show();
    return true;
} 

BEGIN_EVENT_TABLE(BasicDrawPane, wxPanel)
EVT_MOTION(BasicDrawPane::mouseMoved)
EVT_LEFT_DOWN(BasicDrawPane::mouseDown)
EVT_LEFT_UP(BasicDrawPane::mouseReleased)
EVT_RIGHT_DOWN(BasicDrawPane::rightClick)
EVT_LEAVE_WINDOW(BasicDrawPane::mouseLeftWindow)
EVT_KEY_DOWN(BasicDrawPane::keyPressed)
EVT_KEY_UP(BasicDrawPane::keyReleased)
EVT_MOUSEWHEEL(BasicDrawPane::mouseWheelMoved)
EVT_PAINT(BasicDrawPane::render)
END_EVENT_TABLE()


// 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) :
wxPanel(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