I have installed Code Blocks and wxWidgets and i got both working. That is I can make drag and drop a frame with some buttons and it will compile and run. Unfortunately I don't want to make a frame with some buttons that don't do anything, so I have been looking for tutorials and manuals and I found quite a number. Only problem is these are to specific for me.
I would like to make a frame and draw in it with wxClientDC. Now I found a manual with some sample codes, which are great, but when I try to compile those I get an error that says using "this" is a "invalid use of 'this' in non-member function", but every example I find uses "this".
So I don't think the problem is in the reference to "this", but in the basic setup.
Is there anyone that wants to take the time to explain me what I'm doing wrong? And preferably how to prevent this in the future. I'm sure it is not very difficult once you know it, but it took me both days of the weekend without result
Code: Select all
// Code from Main.h
#ifndef MAIN_H
#define MAIN_H
#include <wx/app.h>
class Main : public wxApp
{
public:
virtual bool OnInit();
};
#endif // MAIN_H
//Code from Main.cpp
#include "Main.h"
#include "Gui.h"
#include <wx/image.h>
IMPLEMENT_APP(Main); // start
bool Main::OnInit()
{
bool wxsOK = true;
wxInitAllImageHandlers();
if ( wxsOK )
{
Gui Frame(0);
SetTopWindow(&Frame);
wxsOK = false;
}
return wxsOK;
}
// code from Gui.h
#ifndef GUI_H
#define GUI_H
#include <wx/panel.h>
#ifndef WX_PRECOMP
#include <wx/frame.h>
#endif
class Gui: public wxFrame
{
public:
Gui(wxWindow* parent,wxWindowID id=wxID_ANY,const wxPoint& pos=wxDefaultPosition,const wxSize& size=wxDefaultSize);
virtual ~Gui();
void OnPaint(wxPaintEvent& event);
wxPanel* Panel1;
protected:
static const long ID_PANEL1;
private:
DECLARE_EVENT_TABLE()
};
#endif
// code from Gui.cpp
#include "wx_pch.h"
#include "Gui.h"
#include "wx/dcclient.h"
#ifndef WX_PRECOMP
#include <wx/intl.h>
#include <wx/string.h>
#endif
BEGIN_EVENT_TABLE(Gui,wxFrame)
EVT_PAINT(Gui::OnPaint)
END_EVENT_TABLE()
Gui::Gui(wxWindow* parent,wxWindowID id,const wxPoint& pos,const wxSize& size)
{
Create(parent, id, _("My frame title"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE|wxRESIZE_BORDER|wxMAXIMIZE_BOX|wxMINIMIZE_BOX|wxFULL_REPAINT_ON_RESIZE, _T("id"));
SetClientSize(wxSize(1440,900));
Move(wxDefaultPosition);
Center();
// Panel1 = new wxPanel(this, ID_PANEL1, wxPoint(0,0), wxSize(368,360), wxTAB_TRAVERSAL, _T("ID_PANEL1")); // an attempt, I thought maybe i needed a panel to refer to
}
void OnPaint(wxPaintEvent &event)
{
wxPaintDC dc (this);
dc.SetPen(*wxBLACK_PEN);
dc.SetBrush(*wxRED_BRUSH);
wxSize sz = GetClienSize();
wxCoord w = 100, h=50;
int x=wxMax(0,(sz.x-w)/2);
int y=wxMax(0,(sz.y-h)/2);
wxRect rectToDraw(x,y,w,h);
dc.Clear();
dc.DrawRectangle(rectToDraw);
}
In function 'void OnPaint(wxPaint(wxPaintEvent&)':
error: invalid use of 'this' in non-member function
error: 'GetClientSize' was not declared in this scope