Using GetSizer and Size Event Handler to resize a drawing

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
crownz
In need of some credit
In need of some credit
Posts: 2
Joined: Fri Oct 11, 2019 2:24 pm

Using GetSizer and Size Event Handler to resize a drawing

Post by crownz »

I am trying to learn WxWidgets with C++ (I am very new at this), and I have created a window with a black background color and has a big red "X" on it. I have to edit the code so that the "X" changes its size with the window as I am resizing the window. How can I properly get the size of the window, and assign it to "dc.DrawLine(x1,y1,x2,y2)", so that when I resize the window, the "X" automatically resizes itself, so it maintains its form like on the screenshot?

Here's a screenshot of what my code produces: https://imgur.com/a/0I8EG5y

Here's what I have so far:

Code: Select all

#include <wx/wx.h>
#include <wx/dcbuffer.h>

class MyCanvas : public wxWindow
{
public:
    MyCanvas(wxWindow* parent)
        : wxWindow(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)

    {
        SetBackgroundStyle(wxBG_STYLE_PAINT);
        Bind(wxEVT_PAINT, &MyCanvas::OnPaint, this);
    }
private:
    void OnPaint(wxPaintEvent&)
    {
        wxAutoBufferedPaintDC dc(this);

        dc.SetPen(*wxRED_PEN);
        dc.SetBrush(*wxBLACK_BRUSH);

        dc.DrawLine(0,0,485,485);
        dc.DrawLine(0, 485, 485, 0);

    }
 };


class MyFrame : public wxFrame
{
public:
    MyFrame()
        : wxFrame(NULL, wxID_ANY, _("Resizable X"), wxDefaultPosition, wxSize(500, 525))
    {
        wxBoxSizer* bSizer = new wxBoxSizer(wxVERTICAL);

        bSizer->Add(new MyCanvas(this), 1, wxEXPAND);
        SetSizer(bSizer);
    }

};

/**** MyApp ****/
class MyApp : public wxApp
{
public:
    virtual bool OnInit()
   {
        MyFrame* frame = new MyFrame();
        frame->Show();

        return true;
    }
};


IMPLEMENT_APP(MyApp)
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: Using GetSizer and Size Event Handler to resize a drawing

Post by PB »

In the paint handler, use GetClientRect() to get the client window rectangle (for the panel it is the same as the window size, for frame it would exclude e.g. its caption bar and frame around it) as wxRect, use wxRect's methods to obtain the coordinates of its corner and draw the two lines using those.

I am assuming this is a homework and it should be easy to figure out just by checking wxPanel documentation:
https://docs.wxwidgets.org/trunk/classwx_panel.html
crownz
In need of some credit
In need of some credit
Posts: 2
Joined: Fri Oct 11, 2019 2:24 pm

Re: Using GetSizer and Size Event Handler to resize a drawing

Post by crownz »

So GetClientRect() can be used to create different shapes? It's not only for creating a rectangle shape?
Manolo
Can't get richer than this
Can't get richer than this
Posts: 828
Joined: Mon Apr 30, 2012 11:07 pm

Re: Using GetSizer and Size Event Handler to resize a drawing

Post by Manolo »

Any GetXXXX function pass some value to your variable; the other direction is SetXXXX.

To draw something that fits into a rectangle you first need to know the dimensions of that rectangle. The dimensions (width, height) of the "available" part of a window (the client area, without toolbars, menus, caption, nor status bar) can be asked by GetClientSize(), which is implemented for any wxWindow-derived class (most of GUI classes).

About different sizes read https://docs.wxwidgets.org/trunk/overvi ... izing.html

So, in your OnPaint handler:

Code: Select all

wxSize cliSize = GetClientSize();
//some margings are defined before
dc.DrawLine(margLeft, margTop, cliSize.GetWidth()-margRight, cliSize.GetHeight()-margBottom);
dc.DrawLine(cliSize.GetWidth()-margRight, margTop, margLeft, cliSize.GetHeight()-margBottom);
Of course, as PB told, instead of GetClientSize() you can use GetClientRect() and work with coordinates instead of sizes.
Post Reply