Skip to content

Instantly share code, notes, and snippets.

@ClintLiddick
Last active October 15, 2022 12:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ClintLiddick/a094deaf58cf494cd1a4c8cd40115769 to your computer and use it in GitHub Desktop.
Save ClintLiddick/a094deaf58cf494cd1a4c8cd40115769 to your computer and use it in GitHub Desktop.
wxWidgets Hello World
cmake_minimum_required(VERSION 3.1)
project(wxApp CXX)
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
if(COMPILER_SUPPORTS_CXX11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
else()
message(ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()
set(wxWidgets_USE_LIBS)
find_package(wxWidgets)
include(${wxWidgets_USE_FILE})
include_directories(${wxWidgets_INCLUDE_DIRS})
add_executable(${PROJECT_NAME}
src/wxapp.cpp)
target_link_libraries(${PROJECT_NAME}
${wxWidgets_LIBRARIES})
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
class App : public wxApp
{
public:
bool OnInit();
};
class Frame : public wxFrame
{
public:
Frame(const wxString& title, const wxPoint& pos, const wxSize& size);
private:
void OnExit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
void OnHello(wxCommandEvent& event);
wxDECLARE_EVENT_TABLE();
};
enum { ID_Hello = 1 };
// clang-format off
wxBEGIN_EVENT_TABLE(Frame, wxFrame)
EVT_MENU(ID_Hello, Frame::OnHello)
EVT_MENU(wxID_EXIT, Frame::OnExit)
EVT_MENU(wxID_ABOUT, Frame::OnAbout)
wxEND_EVENT_TABLE()
wxIMPLEMENT_APP(App);
// clang-format on
bool App::OnInit()
{
Frame* frame = new Frame("Hello World", wxPoint(50, 50), wxSize(450, 340));
frame->Show(true);
return true;
}
Frame::Frame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame(NULL, wxID_ANY, title, pos, size)
{
wxMenu* menuFile = new wxMenu();
menuFile->Append(ID_Hello, "&Hello...\tCtrl-H",
"Help string show in status bar");
menuFile->AppendSeparator();
menuFile->Append(wxID_EXIT);
wxMenu* menuHelp = new wxMenu();
menuHelp->Append(wxID_ABOUT);
wxMenuBar* menuBar = new wxMenuBar();
menuBar->Append(menuFile, "&File");
menuBar->Append(menuHelp, "&Help");
SetMenuBar(menuBar);
CreateStatusBar();
SetStatusText("Welcome to wxWidgets!");
}
void Frame::OnExit(wxCommandEvent& event) { Close(true); }
void Frame::OnAbout(wxCommandEvent& event)
{
wxMessageBox("This is a wxWidgets' Hello world example", "About Hello World",
wxOK | wxICON_INFORMATION);
}
void Frame::OnHello(wxCommandEvent& event)
{
wxLogMessage("Hello World from wxWidgets!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment