I'm trying to write a simple graphics app and I have a problem with drawing to a wxBitmap with wxBufferedDC and alpha channel.
Here's some code:
Code: Select all
//canvas.cpp
#include "canvas.h"
BEGIN_EVENT_TABLE(qCanvas, wxPanel)
EVT_PAINT(qCanvas::onPaint)
EVT_LEFT_DOWN(qCanvas::onLeftDown)
EVT_LEFT_UP(qCanvas::onLeftUp)
EVT_MOTION(qCanvas::onMotion)
EVT_LEAVE_WINDOW(qCanvas::onLeave)
EVT_ERASE_BACKGROUND(qCanvas::eraseBackground)
END_EVENT_TABLE()
qCanvas::qCanvas(wxFrame* parent)
: wxPanel(parent, wxID_ANY, wxPoint(0, 0), parent->GetSize() ),
initialPoint( 0, 0 )
{
wxSystemOptions::SetOption("msw.window.no-clip-children", 1);
drawBuffer = new wxBitmap( wxSize( 3000, 2000 ), 32 );
drawBuffer->UseAlpha();
drawing = new wxBitmap( wxSize( 3000, 2000 ), 32 );
drawing->UseAlpha();
wxMemoryDC dc( *drawBuffer );
dc.SetBackground( *wxWHITE_BRUSH );
dc.Clear();
dc.SelectObject( *drawing );
dc.Clear();
dc.SelectObject( wxNullBitmap );
this->SetBackgroundStyle( wxBG_STYLE_CUSTOM );
wxPanel::SetBackgroundColour( wxColor(255, 255, 255) );
}
qCanvas::~qCanvas()
{
delete drawBuffer;
delete drawing;
}
void qCanvas::onPaint(wxPaintEvent &event)
{
wxPaintDC dc( this );
render( dc );
}
void qCanvas::onLeftDown(wxMouseEvent &event)
{
initialPoint = event.GetPosition();
wxClientDC dc( this );
render( dc );
}
void qCanvas::onLeftUp(wxMouseEvent &event)
{
wxClientDC dc( this );
dc.DrawBitmap( drawing, 0, 0, true );
}
void qCanvas::onMotion(wxMouseEvent &event)
{
wxClientDC dc( this );
if( event.LeftIsDown() )
{
wxBufferedDC* bdc = new wxBufferedDC( &dc, *drawBuffer );
bdc->SetBackground( *wxTRANSPARENT_BRUSH );
bdc->Clear();
bdc->SetPen( wxPen( wxColor(0, 255, 0, 255), 4, wxPENSTYLE_SOLID ) );
bdc->DrawLine( initialPoint, event.GetPosition() );
delete bdc;
render( dc );
}
wxBufferedDC ldc( &dc, drawing );
ldc.SetBackground( *wxTRANSPARENT_BRUSH );
wxPoint pt = event.GetPosition();
wxSize sz = this->GetSize();
ldc.Clear();
ldc.SetPen( wxPen( wxColor( 0, 255, 0, 255 ), 1, wxPENSTYLE_SHORT_DASH ) );
ldc.DrawLine( wxPoint( 0, pt.y ), wxPoint( sz.GetWidth(), pt.y ) );
ldc.DrawLine( wxPoint( pt.x, 0 ), wxPoint( pt.x, sz.GetHeight() ) );
render( dc );
}
void qCanvas::onLeave(wxMouseEvent &event)
{
}
void qCanvas::render( wxDC& dc )
{
dc.DrawBitmap( *drawBuffer, 0, 0, true );
dc.DrawBitmap( drawing, 0, 0, true );
}
void qCanvas::eraseBackground(wxEraseEvent &event) { ; }
void qCanvas::onCaptureLost(wxMouseCaptureLostEvent &event) { ; }
Code: Select all
//canvas.h
#ifndef CANVAS_H_INCLUDED
#define CANVAS_H_INCLUDED
#include <wx/wx.h>
#include <wx/dcbuffer.h>
#include <wx/graphics.h>
#include <wx/dcgraph.h>
#include <wx/sysopt.h>
class qCanvas : public wxPanel
{
private:
wxBitmap* drawing;
wxBitmap* drawBuffer;
wxPoint initialPoint;
void render( wxDC& dc );
public:
qCanvas(wxFrame* parent);
~qCanvas();
//Event Handlers:
void onPaint(wxPaintEvent &event);
void onLeftDown(wxMouseEvent &event);
void onLeftUp(wxMouseEvent &event);
void onRightDown(wxMouseEvent &event);
void onRightUp(wxMouseEvent &event);
void onMotion(wxMouseEvent &event);
void onLeave(wxMouseEvent &event);
void eraseBackground(wxEraseEvent &event);
void onCaptureLost(wxMouseCaptureLostEvent &event);
DECLARE_EVENT_TABLE();
};
#endif // CANVAS_H_INCLUDED
What I'm trying to achieve for this example is to draw lines on mouse position and on LeftDown event start to draw line from initialPoint to current mouse position. But when I try to do so the screen stays blank.
What may the reason of this?
PS. When I drawed a line with wxColor( 0, 255, 0, 255 ) which should be green, the line was yellow.