dc.Clear() in a wxScrolledWindow

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
PJSJP
Earned a small fee
Earned a small fee
Posts: 13
Joined: Mon Nov 21, 2011 4:33 pm

dc.Clear() in a wxScrolledWindow

Post by PJSJP » Mon Nov 21, 2011 5:42 pm

I'm just trying out wxWidgets, so apologies if this is a beginners question. I'm using dc.Clear() to change the background colour, but when I scroll it has only changed the background colour in the part that was visible. The line which I drew does continue into the area which was initially hidden. Here's the code which I think is relevant
PictureWindow::PictureWindow(wxWindow* parent)
: wxScrolledWindow(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
wxHSCROLL | wxVSCROLL | wxNO_FULL_REPAINT_ON_RESIZE)
{
SetScrollbars(10,10,50,50);
}
void PictureWindow::OnDraw(wxDC& dc)
{
dc.SetBackground(*wxWHITE_BRUSH);
dc.Clear();
dc.SetPen(*wxBLACK_PEN);
dc.DrawLine(0, 0, 100, 200);
}

User avatar
doublemax
Moderator
Moderator
Posts: 14617
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: dc.Clear() in a wxScrolledWindow

Post by doublemax » Mon Nov 21, 2011 6:48 pm

but when I scroll it has only changed the background colour in the part that was visible.
That's normal. If you just want to change the default background color of a window, use wxWindow::SetBackgroundColour().
http://docs.wxwidgets.org/stable/wx_wxw ... oundcolour

Apart from that you need to catch the paint event of the window and always (re-)draw what you want to display in the window. Anything that you draw using wxClientDC will be overdrawn by the next paint event, it doesn't just stay there forever.
Use the source, Luke!

PJSJP
Earned a small fee
Earned a small fee
Posts: 13
Joined: Mon Nov 21, 2011 4:33 pm

Re: dc.Clear() in a wxScrolledWindow

Post by PJSJP » Wed Nov 23, 2011 9:30 am

Thanks for your help. Why does DrawLine manage to draw outside the displayed area when Clear doesn't?

User avatar
doublemax
Moderator
Moderator
Posts: 14617
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: dc.Clear() in a wxScrolledWindow

Post by doublemax » Wed Nov 23, 2011 4:53 pm

Why does DrawLine manage to draw outside the displayed area when Clear doesn't?
It doesn't. What makes you think so?
Use the source, Luke!

PJSJP
Earned a small fee
Earned a small fee
Posts: 13
Joined: Mon Nov 21, 2011 4:33 pm

Re: dc.Clear() in a wxScrolledWindow

Post by PJSJP » Wed Nov 23, 2011 5:03 pm

When I scroll, the line carries on into the newly visible area, whereas the background reverts to a default grey.

User avatar
doublemax
Moderator
Moderator
Posts: 14617
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: dc.Clear() in a wxScrolledWindow

Post by doublemax » Wed Nov 23, 2011 5:14 pm

Can you please post the complete code for this class?
Use the source, Luke!

PJSJP
Earned a small fee
Earned a small fee
Posts: 13
Joined: Mon Nov 21, 2011 4:33 pm

Re: dc.Clear() in a wxScrolledWindow

Post by PJSJP » Thu Nov 24, 2011 12:26 pm

I've tried to take out everything which is not relevant and here is the entire source.

Code: Select all

#pragma once

#include "wx/wxprec.h"

class wxTestApp : public wxApp
{
public:
    virtual bool OnInit();
};

class PictureWindow : public wxScrolledWindow
{
public:
	PictureWindow(wxWindow* parent);
	~PictureWindow(void);

	void OnDraw(wxDC& dc);
};

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

	PictureWindow *m_pPictureWindow;
};

IMPLEMENT_APP(wxTestApp)

bool wxTestApp::OnInit()
{
    if ( !wxApp::OnInit() )
        return false;

	wxTestFrame *frame = new wxTestFrame( _T("Test"));
    frame->Show(true);
    SetTopWindow(frame);

    return true;
} 

wxTestFrame::wxTestFrame(const wxString& title) : wxFrame( NULL, wxID_ANY, title)
{
	m_pPictureWindow = new PictureWindow(this);

	Centre(); 
}

wxTestFrame::~wxTestFrame(void)
{
}

PictureWindow::PictureWindow(wxWindow* parent)
        : wxScrolledWindow(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
                           wxHSCROLL | wxVSCROLL | wxNO_FULL_REPAINT_ON_RESIZE)
{
	SetScrollbars(10,10,50,50);
}

PictureWindow::~PictureWindow(void)
{
}

void PictureWindow::OnDraw(wxDC& dc)
{
	dc.SetBackground(*wxWHITE_BRUSH);
	dc.Clear();
	dc.SetPen(*wxBLACK_PEN);
	dc.DrawLine(0, 0, 100, 300);
}
When it starts the window only shows part of the line. If I scroll down the rest of the line is visible, but the background is no longer white. (I appreciate I should have used SetBackgroundColour) So I am just curious as to why DrawLine appears to have drawn outside the currently visible area while Clear hasn't. Thanks for looking at this for me.
Last edited by PJSJP on Mon Dec 05, 2011 8:18 pm, edited 1 time in total.

User avatar
doublemax
Moderator
Moderator
Posts: 14617
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: dc.Clear() in a wxScrolledWindow

Post by doublemax » Thu Nov 24, 2011 1:03 pm

Where is the event table or Connect call? How does OnDraw() get called?

And please use code tags (in the editor, select the code and click on the "Code" button above)
Use the source, Luke!

PJSJP
Earned a small fee
Earned a small fee
Posts: 13
Joined: Mon Nov 21, 2011 4:33 pm

Re: dc.Clear() in a wxScrolledWindow

Post by PJSJP » Mon Dec 05, 2011 8:23 pm

I assume OnDraw is being called by the framework. I have just overridden the function in my class which is derived from wxScrolledWindow.

Post Reply