Untitled
unknown
c_cpp
3 years ago
2.9 kB
12
Indexable
// compile by:
// g++ ./wxtest.cpp $(wx-config --cppflags) $(wx-config --libs core,base) -o ./wxtestx
#include "wx/wxprec.h"
#ifndef WX_PRECOMP
# include "wx/wx.h"
#endif
#include <wx/wx.h>
wxDEFINE_EVENT(WX_CONSOLE_REFRESH, wxCommandEvent);
class HelloWorldApp : public wxApp
{
wxFrame *_frame;
public:
virtual bool OnInit();
};
wxIMPLEMENT_APP_NO_MAIN(HelloWorldApp);
int main(int argc, char **argv)
{
if (!wxInitialize())
return false;
wxEntry(argc, argv);
wxUninitialize();
}
class WinPortPanel: public wxPanel
{
unsigned char _loop = 0;
wxFrame *_frame = nullptr;
public:
WinPortPanel(wxFrame *frame, const wxPoint& pos, const wxSize& size);
void OnChar( wxKeyEvent& event );
void OnPaint( wxPaintEvent& event );
void OnEraseBackground( wxEraseEvent& event ) {}
private:
wxDECLARE_EVENT_TABLE();
};
wxBEGIN_EVENT_TABLE(WinPortPanel, wxPanel)
EVT_CHAR(WinPortPanel::OnChar)
EVT_PAINT(WinPortPanel::OnPaint)
EVT_ERASE_BACKGROUND(WinPortPanel::OnEraseBackground)
wxEND_EVENT_TABLE()
WinPortPanel::WinPortPanel(wxFrame *frame, const wxPoint& pos, const wxSize& size)
: wxPanel(frame, wxID_ANY, pos, size, wxWANTS_CHARS | wxNO_BORDER), _frame(frame)
{
}
void WinPortPanel::OnChar( wxKeyEvent& event )
{
++_loop;
std::wstring title;
for (int i = 30 + rand() % 8; i >= 0; --i) {
title+= (wchar_t)'A' + (rand() % 52);
}
wxGetApp().SetAppDisplayName(title.c_str());
_frame->SetTitle(title.c_str());
Refresh();
Update();
}
void WinPortPanel::OnPaint( wxPaintEvent& event )
{
wxRect box = GetClientRect();
wxPaintDC dc(this);
wxSize char_size = dc.GetTextExtent(L"W");
unsigned int cnt = 0;
const char *line = "WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW";
const char *clear = " ";
for (unsigned int y = 0; y < 80; ++y) {
if ((_loop & 1) == 0 || y == 0) {
dc.DrawText (line, 0, y * char_size.GetHeight());
} else {
dc.DrawText (clear, 0, y * char_size.GetHeight());
}
}
}
///////////////
class MainFrame: public wxFrame // MainFrame is the class for our window,
{
// It contains the window and all objects in it
WinPortPanel *_panel = nullptr;
public:
MainFrame( const wxString &title, const wxPoint &pos, const wxSize &size );
DECLARE_EVENT_TABLE()
};
BEGIN_EVENT_TABLE ( MainFrame, wxFrame )
END_EVENT_TABLE() // The button is pressed
MainFrame::MainFrame(const wxString &title, const wxPoint &pos, const wxSize
&size): wxFrame(NULL, wxID_ANY, title, pos, size)
{
_panel = new WinPortPanel(this, wxPoint(0, 0), GetClientSize());
}
// This is executed upon startup, like 'main()' in non-wxWidgets programs.
bool HelloWorldApp::OnInit()
{
_frame = new MainFrame(_("Hello World!"), wxDefaultPosition, wxSize(800, 600));
_frame->Show(true);
SetTopWindow(_frame);
return true;
}
Editor is loading...