Reader small image

You're reading from  C++ Windows Programming

Product typeBook
Published inSep 2016
Reading LevelIntermediate
PublisherPackt
ISBN-139781786464224
Edition1st Edition
Languages
Right arrow
Author (1)
Stefan Björnander
Stefan Björnander
author image
Stefan Björnander

Stefan Björnander is the author of the books Microsoft Windows C++ and C++ Windows Programming. He holds a Master of Engineering and a Licentiate in Computer Science. He has worked as a software developer and as a teacher in computer science and mathematics for many years.
Read more about Stefan Björnander

Right arrow

Chapter 14. Dialogs, Controls, and Page Setup

In this chapter, we look into the implementation of the following:

  • Custom dialogs: The Dialog class is intended to be inherited by subclasses and equipped with controls.

  • Controls: The Control class and its subclasses. There are controls for edit fields, check boxes, radio buttons, list boxes, and combo boxes.

  • Converters: Between strings and other values. For instance, when the user inputs text that represents a numerical value, it is possible to add a converter that converts the text to a value, or gives an error message if the text does not hold a valid value.

  • Page Setup: Where we extend the Dialog class. The dialog is used when setting page settings for a document of the StandardDocument class. It handles information for headers, footers, and margins.

Custom dialogs


The Dialog class handles a set of controls, which are added to the dialog by the AddControl method. For a subclass of the Dialog class, refer to PageSetupDialog in the last section of this chapter. The Dialog class provides a modal dialog, which means that all other windows in the application become disabled until the dialog is closed.

The user may navigate between controls with the Tab key and between radio buttons in the same group with the arrow keys. They can also use mnemonics to access controls.

Dialog.h

namespace SmallWindows { 

The dialogMap field is used by DialogProc to look up the dialog receiving the messages:

  extern map<HWND,Dialog*> dialogMap; 
  extern Font DialogFont; 

The Dialog class is a subclass of Window even though it calls the default Window constructor, which does not call the Win32 API function CreateWindowEx. Instead, DoModal collects information about the dialog and its controls and calls the Win32 API function DialogBoxIndirectParam...

Controls


Here is the Small Windows control hierarchy:

Control.h

namespace SmallWindows { 
  class Dialog; 

The constructor sends the parent window pointer to the Window constructer and stores the other values until it is added to the dialog information list by AddControlInfo:

  class Control : public Window { 
    public: 
      Control(Dialog* parentPtr, Point topLeft, Size controlSize, 
              String className, String text, int style); 
      void AddControlInfo(InfoList& infoList) const; 
 
      Point TopLeft() const {return topLeft;} 
      Size GetSize() const {return controlSize;} 

The following methods are intended to be overridden by subclasses and are by default empty:

      virtual void OnControlInit(Dialog* dialogPtr) {/* Empty. */} 
      virtual void OnGainFocus(Dialog* dialogPtr) {/* Empty. */} 
      virtual void OnLoseFocus(Dialog* dialogPtr) {/* Empty. */} 
      virtual void OnChange(Dialog* dialogPtr...

Converters


The Converter class is a template class intended to be specialized by type. Its task is to convert values between the template type and the String objects. The Check variable takes a string and returns true if it holds a valid value, TextToValue converts a text to a value, and ValueToText converts a value to a text.

Converter.h

namespace SmallWindows { 
  template <class Type> 
  class Converter { 
    public: 
      static bool Check(String& text, int base); 
      static Type TextToValue(String& text, int base); 
      static String ValueToText(Type& value, int base); 
  }; 

Signed integers

Small Windows comes equipped with a set of predefined converters, which are specializations of Converter. One of these handles signed integer values of the type int.

Converter.h

  template <> 
  class Converter<int> { 
    public: 
      static bool Check(String& text, int base); 
      static int...

Page setup


The final section describes page setup functionality, divided into the PageSetupInfo class, which handles page setup information, the PageSetupDialog, which is a subclass of Dialog displayed for the user to input page setup information, and the Template function, which translates code input by the user in the Page Setup dialog to actual values.

Page setup information

The PageSetupInfo class holds information about the page: portrait or landscape orientation, the margins, the text and font of the header and footer, whether the header and footer will be present on the first page, and whether the pages will be enclosed by a frame.

PageSetupInfo.h

namespace SmallWindows { 
  enum Orientation {Portrait, Landscape}; 
 
  class PageSetupInfo { 
    public: 
      PageSetupInfo(); 
      PageSetupInfo(const PageSetupInfo& pageSetupInfo); 
      bool operator==(const PageSetupInfo& pageSetupInfo); 
      bool operator!=(const PageSetupInfo...

Summary


In this chapter, we looked into custom dialogs, controls, converters, and the Page Setup dialog. The only remaining part of the book is the implementation of the rational and complex classes.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
C++ Windows Programming
Published in: Sep 2016Publisher: PacktISBN-13: 9781786464224
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Stefan Björnander

Stefan Björnander is the author of the books Microsoft Windows C++ and C++ Windows Programming. He holds a Master of Engineering and a Licentiate in Computer Science. He has worked as a software developer and as a teacher in computer science and mathematics for many years.
Read more about Stefan Björnander