wxFontDialog

Introduction

Example

 File: Dialogs/WxFontDialog1/src/WxFontDialog1Frame.h
#ifndef _TUTORIALS_WXWIDGETS_WXFONTDIALOG1FRAME_H_
#define _TUTORIALS_WXWIDGETS_WXFONTDIALOG1FRAME_H_

#include <wx/frame.h>
#include <wx/fontdlg.h>

class WxFontDialog1Frame : public wxFrame
{
public:
    WxFontDialog1Frame(const wxString& title);

private:
    void OnPaint(wxPaintEvent& evt);
    void OnSelectFont(wxCommandEvent& evt);

private:
    wxFontData m_fontData;

    wxDECLARE_EVENT_TABLE();
};

#endif
 File: Dialogs/WxFontDialog1/src/WxFontDialog1Frame.cpp
#include "WxFontDialog1Frame.h"
#include "WindowIDs.h"
#include <wx/menu.h>
#include <wx/dcclient.h>

WxFontDialog1Frame::WxFontDialog1Frame(const wxString& title)
    : wxFrame(NULL, wxID_ANY, title)
{
    // The Open dialog is usually accessible from
    // the "File" menu so we create one.
    wxMenuBar* menuBar = new wxMenuBar;
    wxMenu* menuFile = new wxMenu;
    menuFile->Append(SelectFontMenuID, "Select Font...");
    menuBar->Append(menuFile, "&Options");
    SetMenuBar(menuBar);

    SetBackgroundColour(*wxWHITE);
}

// Event handler for the paint event
void WxFontDialog1Frame::OnPaint(wxPaintEvent& evt)
{
    wxPaintDC dc(this);

    // Set the font and text color based on the user
    // selection and draw some text
    dc.SetFont(m_fontData.GetChosenFont());
    dc.SetTextForeground(m_fontData.GetColour());
    dc.DrawText("Hello World!", 20, 20);
}

// Event handler for the Select Font menu item. It will
// display a wxFontDialog.
void WxFontDialog1Frame::OnSelectFont(wxCommandEvent& evt)
{
    // Create a new wxFontDialog dialog
    wxFontDialog* fontDialog = new wxFontDialog(this);

    // Display the dialog, save the select font
    // and color and refresh the window to
    // repaint it with the new font
    if (fontDialog->ShowModal() == wxID_OK)
    {
        m_fontData = fontDialog->GetFontData();
        Refresh();
    }

    fontDialog->Destroy();
}

// Add the event handler for the Select Font menu item
// to the event table and for the paint event
wxBEGIN_EVENT_TABLE(WxFontDialog1Frame, wxFrame)
    EVT_PAINT(WxFontDialog1Frame::OnPaint)
    EVT_MENU(SelectFontMenuID, WxFontDialog1Frame::OnSelectFont)
wxEND_EVENT_TABLE()
 File: Dialogs/WxFontDialog1/src/WindowIDs.h
#ifndef _TUTORIALS_WXWIDGETS_WINDOWIDS_H_
#define _TUTORIALS_WXWIDGETS_WINDOWIDS_H_

#include <wx/defs.h>

const wxWindowID SelectFontMenuID = wxID_HIGHEST + 1;

#endif
 File: Dialogs/WxFontDialog1/src/WxFontDialog1App.h
#ifndef _TUTORIALS_WXWIDGETS_WXFONTDIALOG1APP_H_
#define _TUTORIALS_WXWIDGETS_WXFONTDIALOG1APP_H_

#include <wx/app.h>

class WxFontDialog1App : public wxApp
{
public:
    virtual bool OnInit();
};

#endif
 File: Dialogs/WxFontDialog1/src/WxFontDialog1App.cpp
#include "WxFontDialog1App.h"
#include "WxFontDialog1Frame.h"

wxIMPLEMENT_APP(WxFontDialog1App);

bool WxFontDialog1App::OnInit()
{
    WxFontDialog1Frame* frame = new WxFontDialog1Frame("WxFontDialog1");
    frame->Show(true);
    return true;
}