- 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); }
- Mastering RabbitMQ
- Arduino by Example
- Mastering Apache Spark 2.x(Second Edition)
- 微信小程序項目開發實戰
- 數據結構習題解析與實驗指導
- 微服務從小白到專家:Spring Cloud和Kubernetes實戰
- Web前端應用開發技術
- Python 3 數據分析與機器學習實戰
- Mastering Elixir
- C++從入門到精通(第6版)
- SSH框架企業級應用實戰
- WordPress Search Engine Optimization(Second Edition)
- MATLAB 2020 GUI程序設計從入門到精通
- Java 7 Concurrency Cookbook
- H5頁面設計與制作(全彩慕課版·第2版)