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 12. The Auxiliary Classes

Small Windows includes a set of auxiliary classes, which are as follows:

  • Size, Point, Rect, Color, and Font: These wrap the Win32 API structures which are SIZE, POINT, RECT, COLORREF, and LOGFONT. They are equipped with methods to communicate with files, the clipboard, and the registry. The Registry is a database in the Windows system that we can use to store values between the executions of our applications.

  • Cursor: is a type representing the Windows cursor.

  • DynamicList: holds a list of dynamic size with a set of callback functions.

  • Tree: holds a recursive tree structure.

  • InfoList: holds a list of generic information that can be transformed to and from a memory buffer.

  • There is also a small set of string manipulation functions.

The Size class


The Size class is a small class holding the width and height:

Size.h

namespace SmallWindows { 

The ZeroSize object is an object with its width and height set to zero:

  class Size; 
  extern const Size ZeroSize;  
  class Size { 
    public: 

The default constructor initializes the width and height to zero. The size can be initialized by, and assigned to, another size. The Size class uses the assignment operator to assign a size to another size:

      Size(); 
      Size(int width, int height); 
      Size(const Size& size); 
      Size& operator=(const Size& size); 

A Size object can be initialized and assigned to a value of the Win32 API SIZE structure, and a Size object can be converted to a SIZE:

      Size(const SIZE& size); 
      Size& operator=(const SIZE& size); 
      operator SIZE() const; 

When comparing two sizes, the widths are compared first. If they are equal, the heights are then...

The Point class


The Point class is a small class holding the x and y position of a two-dimensional point:

Point.h

namespace SmallWindows { 
  class Point { 
    public: 

The default constructor initializes the x and y value to zero. The point can be initialized by, and assigned to, another point:

      Point(); 
      Point(int x, int y); 
      Point(const Point& point); 

Similar to the Size class mentioned earlier, Point uses the assignment operator:

      Point& operator=(const Point& point); 

Similar to SIZE in the preceding section, there is a POINT Win32 API structure. A Point object can be initialized by, and assigned to, a POINT structure, and a Point object can be converted to POINT:

      Point(const POINT& point); 
      Point& operator=(const POINT& point); 
      operator POINT() const; 

When comparing two points, the x values are first compared. If they are equal, the y values are then compared:

      bool...

The Rect class


The Rect class holds the four borders of a rectangle: left, top, right, and bottom.

Rect.h

namespace SmallWindows { 
  class Rect; 
  extern const Rect ZeroRect; 
 
  class Rect { 
    public: 

The default constructor sets all the four borders to zero. The rectangle can be initialized by, or assigned to, another rectangle. It is also possible to initialize the rectangle with the top-left and bottom-right corners, as well as the top-left corner and a size holding the width and height of the rectangle:

      Rect(); 
      Rect(int left, int top, int right, int bottom); 
      Rect(const Rect& rect); 
      Rect& operator=(const Rect& rect); 
      Rect(Point topLeft, Point bottomRight); 
      Rect(Point topLeft, Size size); 

Similar to SIZE and POINT in the previous sections, a rectangle can be initialized and assigned to a value of the Win32 API RECT structure. A Rect object can also be converted to a...

The Color class


The Color class is a wrapper class for the Win32 API COLORREF structure, which holds a color in accordance with the Red-Green-Blue (RGB) standard. Each component of the color is represented by a value between 0 and 255, inclusive, which gives a theoretical total number of 2563 = 16,777,216 different colors, among which Color defines 142 standard colors.

Color.h

namespace SmallWindows { 
  class Color; 
  extern const Color SystemColor; 

The default constructor initializes the color with zero for each of the red, green, and blue values, which corresponds to black. A color object can also be initialized by, and assigned to, another color:

  class Color { 
    public: 
      Color(); 
      Color(int red, int green, int blue); 
      Color(const Color& color); 
      Color& operator=(const Color& color); 

The equality operators compare the red, green, and blue values:

      bool operator==(const Color& color) const;...

The Font class


The Font class is a wrapper class for the Win32 API LOGFONT structure. The structure holds a large set of properties; however, we only take into consideration the fields for the font's name and size and whether the font is italic, bold, or underlined; the other fields are set to zero. The system font is the font where all fields in the LOGFONT structure are set to zero, which results in the standard font of the system. Finally, the Font class also includes a Color object.

Font.h

namespace SmallWindows { 
  class Font; 
  extern const Font SystemFont; 
 
  class Font { 
    public: 

The default constructor sets the name to the empty string and all other values to zero, resulting in the system font, usually 10 points Arial. The size of the font is given in typographic points (1 point = 1/72 of an inch = 1/72 * 25.4 mm ≈ 0.35 mm). A font can also be initialized by, or assigned to, another font:

      Font(); 
      Font(String name, int size...

The Cursor class


There is a set of cursors available in the Win32 API, all with names starting with IDC_. In Small Windows, they have been given other names, which are hopefully easier to understand. Unlike other cases, we cannot use an enumeration for the cursors, since they are actually zero-terminated C++ strings (character pointers). Instead, every cursor is a pointer to a zero-terminated string. LPCTSTR stands for Long Pointer to Constant TChar String.

The reason the cursor has its own class, while the caret has a method in the Document class is that the caret does need a window handle to be set, while the cursor does not.

Cursor.h

namespace SmallWindows { 
  typedef LPCTSTR CursorType; 
 
  class Cursor { 
    public: 
      static const CursorType Normal; 
      static const CursorType Arrow; 
      static const CursorType ArrowHourGlass; 
      static const CursorType Crosshair; 
      static const CursorType Hand; 
      static const...

The DynamicList class


The DynamicList class can be regarded as a more advanced version of the C++ standard classes list and vector. It varies its size dynamically:

DynamicList.h

namespace SmallWindows { 
  template <class Type> 
  class DynamicList { 
    public: 

The IfFuncPtr pointer is a function prototype that is used when testing (without changing) a value in the list. It takes a constant value and a void pointer and returns a Boolean value. DoFuncPtr is used when changing a value in the list and takes a (non-constant) value and a void pointer. The void pointers are sent by the calling methods; they hold additional information:

      typedef bool (*IfFuncPtr)(const Type& value, void* voidPtr); 
      typedef void (*DoFuncPtr)(Type& value, void* voidPtr); 

The list can be initialized by, and assigned to, another list. The default constructor creates an empty list, and the destructor deallocates the memory from the list:

      DynamicList(); ...

The Tree class


The C++ standard library hold a set of container classes for arrays, lists, vectors, sets, and maps. However, there is no class for a tree structure. Therefore, the Tree class has been added to Small Windows. A tree is made up of a set of nodes, among which, one is the root node. Each node holds a (possibly empty) list of child nodes:

Tree.h

namespace SmallWindows { 
  template <class NodeType> 
  class Tree { 
    public: 
      Tree(); 
      Tree(NodeType nodeValue, 
           initializer_list<Tree<NodeType>*> childList = {}); 
      Tree(const Tree& tree); 
      Tree& operator=(const Tree& tree); 
      void Init(const Tree& tree); 
      ~Tree(); 

The tree can be written to, and read from, a file stream or the clipboard:

      bool WriteTreeToStream(ostream& outStream) const; 
      bool ReadTreeFromStream(istream& inStream); 
      void WriteTreeToClipboard(InfoList...

The InfoList class


The InfoList class is an auxiliary class with template methods that stores information in a character list; information can be added and extracted; or written to, or read from, a buffer.

InfoList .h

namespace SmallWindows { 
  class InfoList { 
    public: 
      template <class AlignType> void Align(); 
      template <class ListType> 
        void AddValue(const ListType value); 
      template <class ListType> 
        void PeekValue(ListType& value, int index); 
      template <class ListType> void GetValue(ListType& value); 
      template <class CharType> 
        void AddString(basic_string<CharType> text); 
      template <class CharType> 
        basic_string<CharType> GetString(); 
      void FromBuffer(const void* voidBuffer, int size); 
      void ToBuffer(void* voidBuffer); 
      int Size() const {return list.Size();} 
 ...

Strings


There are a small set of string functions:

  • CharPtrToGenericString: This takes text as a char character pointer and returns the same text as a generic String object. Remember that the String class holds values of the TCHAR type, of which many are char or wchar_t depending on system settings.

  • Split: This takes a string and returns a list of strings holding the space-separated words of the text.

  • IsNumeric: This returnstrue if the text holds a numeric value.

  • Trim: This removes spaces at the beginning and at the end of the text.

  • ReplaceAll: This replaces one string with another string.

  • WriteStringToStream and ReadStringFromStream: These write and read a string to and from a stream.

  • StartsWith and EndsWith: These returntrue if the text starts or ends with the subtext.

String.h

namespace SmallWindows { 
  extern String CharPtrToGenericString(char* text); 
  extern vector<String> Split(String text, TCHAR c = TEXT(' ')); 
  extern bool IsNumeric(String text); 
 ...

Summary


In this chapter, we studied the auxiliary classes used by Small Windows. In Chapter 13, The Clipboard, Standard Dialogs, and Print Preview, we will look into the registry, the clipboard, standard dialogs, and print preview.

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