官术网_书友最值得收藏!

Hello, Small Windows!

In The C Programming Language by Brian Kernighan and Dennis Richie, the hello-world example was introduced. It was a small program that wrote "hello, world" on the screen. In this section, we shall write a similar program for Small Windows.

In regular C++, the execution of the application starts with the main function. In Small Windows, however, main is hidden in the framework and has been replaced by MainWindow, whose task is to define the application name and create the main window object. The following argumentList parameter corresponds to argc and argv in main. The commandShow parameter forwards the system's request regarding the window's appearance:

MainWindow.cpp

#include "..\\SmallWindows\\SmallWindows.h" 
#include "HelloWindow.h" 
 
void MainWindow(vector<String> /* argumentList */, WindowShow windowShow) { 
  Application::ApplicationName() = TEXT("Hello"); 
  Application::MainWindowPtr() = 
    new HelloWindow(windowShow); 
} 

In C++, there are to two character types, char and wchar_t, where char holds a regular character of 1 byte and wchar_t holds a wide character of larger size, usually 2 bytes. There is also the string class, which holds a string of char values, and the wstring class, which holds a string of wchar_t values.

However, in Windows, there is also the generic character type TCHAR, which is char or wchar_t, depending on system settings. There is also the String class, which holds a string of TCHAR values. Moreover, TEXT is a macro that translates a character value to TCHAR and a text value to an array of TCHAR values.

To sum it up, the following table shows character types and string classes:

In the applications of this book, we always use the TCHAR type, the String class, and the TEXT macro. The only exception to that rule is clipboard handling in Chapter 13, The Registry, Clipboard, Standard Dialogs, and Print Preview.

Our version of the hello-world program writes "Hello, Small Windows!" in the center of the client area. The client area of the window is that part of the window where it is possible to draw graphical objects. In the following window, the client area is the white area:

The HelloWindow class extends the Small Windows Window class. It holds a constructor and the Draw method. The constructor calls the Window constructor with suitable information regarding the appearance of the window. The Draw method is called every time the client area of the window needs to be redrawn:

HelloWindow.h

class HelloWindow : public Window { 
  public: 
    HelloWindow(WindowShow windowShow); 
    void OnDraw(Graphics& graphics, DrawMode drawMode) const; 
}; 

The constructor of HelloWindow calls the constructor of Window with the following parameters:

  • The first parameter of the HelloWindow constructor is the coordinate system. LogicalWithScroll indicates that each logical unit is one hundredth of a millimeter, regardless of the physical resolution of the screen. The current scroll bar settings are taken into consideration.
  • The second parameter of the Window constructor is the preferred size of the window. It indicates that a default size should be used.
  • The third parameter is a pointer to the parent window. It is null since the window has no parent window.
  • The fourth and fifth parameters set the window's style, in this case overlapped windows.
  • The last parameter is windowShow, given by the surrounding system to MainWindow, which decides the window's initial appearance (minimized, normal, or maximized).
  • Finally, the constructor sets the header of the window by calling the Window class's SetHeader method.

HelloWindow.cpp

#include "..\\SmallWindows\\SmallWindows.h" 
#include "HelloWindow.h" 
 
HelloWindow::HelloWindow(WindowShow windowShow) 
 :Window(LogicalWithScroll, ZeroSize, nullptr, 
         OverlappedWindow, NoStyle, windowShow) { 
  SetHeader(TEXT("Hello Window")); 
} 

The OnDraw method is called every time the client area of the window needs to be redrawn. It obtains the size of the client area and draws the text in its center with black text on a white background. The SystemFont parameter will make the text appear in the default system font.

The Small Windows Color class holds the constants Black and White. The Point class holds a two-dimensional point. The Size class holds width and height. The Rect class holds a rectangle; more specifically, it holds the four corners of a rectangle:

void HelloWindow::OnDraw(Graphics& graphics, 
                         DrawMode /* drawMode */) const { 
  Size clientSize = GetClientSize(); 
  Rect clientRect(Point(0, 0), clientSize);
 graphics.DrawText(clientRect, TEXT("Hello, Small Windows!"),
                    SystemFont, Black, White);
} 
主站蜘蛛池模板: 盘锦市| 九江县| 垫江县| 伊川县| 北宁市| 包头市| 苏尼特左旗| 长丰县| 海城市| 通道| 谢通门县| 泌阳县| 宣城市| 仁布县| 和平县| 马鞍山市| 康保县| 佛教| 资源县| 开原市| 高陵县| 汽车| 白银市| 长葛市| 斗六市| 和顺县| 彝良县| 隆林| 平潭县| 来宾市| 叶城县| 黔东| 宁乡县| 通海县| 凉城县| 云和县| 余干县| 磐安县| 胶州市| 东至县| 鸡西市|