Pass dc.DrawLine parameters in main.cpp file?

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
SciMath
In need of some credit
In need of some credit
Posts: 5
Joined: Wed May 05, 2021 7:14 am

Pass dc.DrawLine parameters in main.cpp file?

Post by SciMath »

In the example from https://zetcode.com/gui/wxwidgets/gdi/ (files line.cpp and main.cpp) parameters (x1, y1, x2, y2) for drawing a line are passed to the OnPaint function. How to pass these parameters in the main.cpp file?
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Pass dc.DrawLine parameters in main.cpp file?

Post by doublemax »

You can add parameters to the "Line" constructor and pass any values. If you store them in member variables of "Line", the paint event handler can access them.
Use the source, Luke!
SciMath
In need of some credit
In need of some credit
Posts: 5
Joined: Wed May 05, 2021 7:14 am

Re: Pass dc.DrawLine parameters in main.cpp file?

Post by SciMath »

Please give an example of how to fix the line.cpp and main.cpp files. In order to pass the same parameters x1, y1, x2, y2 in the main.cpp file.
Attachments
line.cpp
(397 Bytes) Downloaded 64 times
main.cpp
(178 Bytes) Downloaded 62 times
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Pass dc.DrawLine parameters in main.cpp file?

Post by doublemax »

This is very, very basic C++ knowledge. If you can't do this, you'll have a hard time with wxWidgets.

main.cpp

Code: Select all

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

IMPLEMENT_APP(MyApp)

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

    return true;
}
line.h

Code: Select all

#include <wx/wx.h>

class Line : public wxFrame
{
public:
    Line(const wxString& title, int x1, int y1, int x2, int y2);
    void OnPaint(wxPaintEvent& event);

protected:
   int m_x1, m_y1, m_x2, m_y2;
};
line.cpp

Code: Select all

#include "line.h"

Line::Line(const wxString& title, int x1, int y1, int x2, int y2)
       : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(280, 180))
{
  m_x1 = x1;
  m_y1 = y1;
  
  m_x2 = x2;
  m_y2 = y2;
  
  this->Connect(wxEVT_PAINT, wxPaintEventHandler(Line::OnPaint));
  this->Centre();
}

void Line::OnPaint(wxPaintEvent& event)
{
  wxPaintDC dc(this);
  
  dc.DrawLine(m_x1, m_y1, m_x2, m_y2);
}
Use the source, Luke!
SciMath
In need of some credit
In need of some credit
Posts: 5
Joined: Wed May 05, 2021 7:14 am

Re: Pass dc.DrawLine parameters in main.cpp file?

Post by SciMath »

Thanks for helping me. I would be grateful if you could tell me how, after creating the main window (i.e. after the line-> Show (true)), in the same additionally draw something.
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Pass dc.DrawLine parameters in main.cpp file?

Post by doublemax »

You can't just draw something on a window once and expect it to stay there. The way it works is that the paint event handler needs to redraw everything each time a paint event is received.

What you have to do is to add a method to the "Line" class, which receives all the information the paint event handler needs to draw its content. Then you call Refresh() to trigger the new redraw.

This is too abstract, please give an example of what you want to draw.
Use the source, Luke!
SciMath
In need of some credit
In need of some credit
Posts: 5
Joined: Wed May 05, 2021 7:14 am

Re: Pass dc.DrawLine parameters in main.cpp file?

Post by SciMath »

If you reuse the string:

Code: Select all

Line *line = new Line(wxT("Line"), 10, 10, 100, 100);
line->Show(true);
line = new Line(wxT("Line"), 100, 10, 100, 60);
line->Show(true);
two windows appear. How can I fix this so that both lines are drawn in the same window? It is important for me to pass parameters for drawing lines in the file main.cpp.
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Pass dc.DrawLine parameters in main.cpp file?

Post by doublemax »

Instead of the member variables m_x1 to m_y2, you need a container class that can store multiple point pairs.

Add a new method to Line(), e.g.
void AddLine( x1, y2, x2, y2 );
Append this point pair to the container.

Change the drawing code so that it iterates over all elements in the container and draws all lines
Use the source, Luke!
SciMath
In need of some credit
In need of some credit
Posts: 5
Joined: Wed May 05, 2021 7:14 am

Re: Pass dc.DrawLine parameters in main.cpp file?

Post by SciMath »

Thanks for the help, it worked for me!
jpo234
Experienced Solver
Experienced Solver
Posts: 70
Joined: Tue Feb 25, 2020 11:34 am

Re: Pass dc.DrawLine parameters in main.cpp file?

Post by jpo234 »

doublemax wrote: Wed May 05, 2021 5:17 pm

Code: Select all

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

IMPLEMENT_APP(MyApp)

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

    return true;
}
This always creeps me out: a naked new and a pointer that falls out of scope seemingly without anybody cleaning up. This is so not RAII...

I know it works, but I get all itchy. I so, so wish that wasn't just an April fools joke: C++ Will No Longer Have Pointers
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Pass dc.DrawLine parameters in main.cpp file?

Post by doublemax »

If you can't deal with pointers, there are other programming languages for you, like C# or Java.
Use the source, Luke!
jpo234
Experienced Solver
Experienced Solver
Posts: 70
Joined: Tue Feb 25, 2020 11:34 am

Re: Pass dc.DrawLine parameters in main.cpp file?

Post by jpo234 »

doublemax wrote: Tue May 11, 2021 3:45 pm If you can't deal with pointers, there are other programming languages for you, like C# or Java.
I have done C for 20 years. I can deal with pointers, which is why this "new" without balancing "delete" looks so creepy. It goes against every ingrained habit...
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7458
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Pass dc.DrawLine parameters in main.cpp file?

Post by ONEEYEMAN »

Hi,
You don't need to call "delete" - it will be called by the library. ;-)
Get used to it.

Thank you.
Post Reply