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

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);
} 
主站蜘蛛池模板: 高安市| 东阿县| 盐山县| 西吉县| 淮阳县| 瑞丽市| 乌审旗| 芮城县| 静安区| 大连市| 砀山县| 长垣县| 新化县| 会昌县| 汉沽区| 林周县| 安徽省| 泗洪县| 清河县| 大新县| 枣阳市| 绥棱县| 乐平市| 工布江达县| 嘉黎县| 呼伦贝尔市| 西青区| 芜湖县| 利川市| 仲巴县| 黑山县| 南京市| 沈丘县| 中山市| 佛坪县| 台中县| 安义县| 呼图壁县| 阿拉善盟| 隆子县| 太原市|