Page 1 of 1

dc tutorial clipping when resizing

Posted: Tue Jun 09, 2009 1:13 pm
by mael15
hi,

i wanted to understand how redrawing and resizing work by customizing the very simple tutorial http://zetcode.com/tutorials/wxwidgetstutorial/gdi/

the problem is that the line is cut off when i maximaize the window. the direction is correct, but the line is no longer than the size of the device context BEFORE maximizing.

here are the files:

line.h

Code: Select all

#include <wx/wx.h>

class Line : public wxFrame
{
public:
    Line(const wxString& title);

    void OnSize(wxSizeEvent& event);
	void OnMax(wxMaximizeEvent& event);
	void drawStuff();
};
line.cpp

Code: Select all

#include "line.h"

Line::Line(const wxString& title) : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(400, 380))
{
  this->Connect(wxEVT_SIZE, wxSizeEventHandler(Line::OnSize));
  this->Connect(wxEVT_MAXIMIZE, wxMaximizeEventHandler(Line::OnMax));
  this->Centre();
}

void Line::OnSize(wxSizeEvent& event)
{
	if(!this->IsMaximized())
		drawStuff();
}

void Line::OnMax(wxMaximizeEvent& event){
	drawStuff();
}

void Line::drawStuff(){
	wxClientDC dc(this);
	dc.Clear();

	wxSize size = this->GetSize();

	wxCoord x1 = 10, y1 = 60;
	wxCoord x2 = size.x-10, y2 = size.y-30;

	this->SetFocus();
	dc.DrawLine(x1, y1, x2, y2);
}
main.h

Code: Select all

#include <wx/wx.h>

class MyApp : public wxApp
{
  public:
    virtual bool OnInit();
};
main.cpp

Code: Select all

#include "main.h"
#include "line.h"

IMPLEMENT_APP(MyApp)

bool MyApp::OnInit()
{
    Line *line = new Line(wxT("Line"));
    line->Show(true);

    return true;
}
what am i missing?

Posted: Tue Jun 09, 2009 2:28 pm
by Auria
Check the DC examples on the wiki; wxClientDC can be useful, but using it alone generally does not make it. Look into using paint events and using wxPaintDC. Then, you don't even need to catch the ONSize event, as a new paint event will be fired after the window is resized