- C++ Windows Programming
- Stefan Bj?rnander
- 689字
- 2021-07-14 10:03:17
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 toMainWindow
, 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'sSetHeader
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); }
- .NET之美:.NET關鍵技術深入解析
- Mastering Entity Framework Core 2.0
- ASP.NET Web API:Build RESTful web applications and services on the .NET framework
- Visual Basic程序開發(學習筆記)
- Debian 7:System Administration Best Practices
- 微服務設計原理與架構
- 精通API架構:設計、運維與演進
- 利用Python進行數據分析(原書第3版)
- Julia高性能科學計算(第2版)
- Java Web開發就該這樣學
- 小程序,巧應用:微信小程序開發實戰(第2版)
- 物聯網系統架構設計與邊緣計算(原書第2版)
- C++ Data Structures and Algorithm Design Principles
- Python Natural Language Processing
- Scratch編程入門與算法進階(第2版)