7c.wxWidgets – wxFontDialog

OVERVIEW

In the sample code below, we display a line of text and then use the wxFontDialog to change the font . We have added a certain size to the wxStaticText so that if the font size is very big , it can still be seen.

SAMPLE CODE

dialog-font.h
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
	#include <wx/wx.h>
#endif


class DialogFont: public wxFrame {
	public:
		DialogFont(const wxString& title);
		void OnClick(wxCommandEvent &event);
	private:
		wxStaticText *mLabel;
};

dialog-font.cpp
#include <wx/fontdlg.h>
#include "dialog-font.h"

DialogFont::DialogFont(const wxString& title):
	wxFrame(NULL, -1, title, wxDefaultPosition, wxSize(500,400)) {

	wxPanel *panelMain = new wxPanel(this, -1);
	wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
	panelMain->SetSizer(vbox);
	
	wxButton *btn1 = new wxButton(panelMain, -1, wxT("Change Font"));
	mLabel = new wxStaticText(panelMain, -1, wxT("This is a single line of text"), wxPoint(-1,-1), wxSize(200,100));

	vbox->Add(btn1, 0, wxALL | wxCENTER,5);
	vbox->Add(mLabel, 1, wxALL | wxEXPAND | wxCENTER, 5);
	
	btn1->Bind(wxEVT_BUTTON, &DialogFont::OnClick, this);
	Center();
}

	
void DialogFont::OnClick(wxCommandEvent &event) {

	wxFontDialog *dlg = new wxFontDialog(this);
	int retVal = dlg->ShowModal();	
	if (retVal == wxID_OK) {
		mLabel->SetFont(dlg->GetFontData().GetChosenFont());	
	}
}


main.h
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
	#include <wx/wx.h>
#endif

class Main: public wxApp {

	public:
		virtual bool OnInit();
};
main.cpp
#include "main.h"
#include "dialog-font.h"

IMPLEMENT_APP(Main)

bool Main::OnInit() {
	DialogFont *app = new DialogFont(wxT("Font Dialog "));
	app->Show(true);

}

The output is shown below

Be the first to comment

Leave a Reply

Your email address will not be published.


*