Pass dc.DrawLine parameters in main.cpp file?
Pass dc.DrawLine parameters in main.cpp file?
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?
Re: Pass dc.DrawLine parameters in main.cpp file?
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!
Re: Pass dc.DrawLine parameters in main.cpp file?
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.
Re: Pass dc.DrawLine parameters in main.cpp file?
This is very, very basic C++ knowledge. If you can't do this, you'll have a hard time with wxWidgets.
main.cpp
line.h
line.cpp
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;
}
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;
};
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!
Re: Pass dc.DrawLine parameters in main.cpp file?
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.
Re: Pass dc.DrawLine parameters in main.cpp file?
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.
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!
Re: Pass dc.DrawLine parameters in main.cpp file?
If you reuse the string:
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.
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);
Re: Pass dc.DrawLine parameters in main.cpp file?
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
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!
Re: Pass dc.DrawLine parameters in main.cpp file?
Thanks for the help, it worked for me!
Re: Pass dc.DrawLine parameters in main.cpp file?
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...doublemax wrote: ↑Wed May 05, 2021 5:17 pmCode: 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; }
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
Re: Pass dc.DrawLine parameters in main.cpp file?
If you can't deal with pointers, there are other programming languages for you, like C# or Java.
Use the source, Luke!
Re: Pass dc.DrawLine parameters in main.cpp file?
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...
Re: Pass dc.DrawLine parameters in main.cpp file?
Hi,
You don't need to call "delete" - it will be called by the library.
Get used to it.
Thank you.
You don't need to call "delete" - it will be called by the library.
Get used to it.
Thank you.