You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: chap04/chap04-windows.md
+77-4Lines changed: 77 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,9 +12,67 @@ Because we're going to be writing this from scratch, we're going to have to inte
12
12
13
13
If you're targeting Linux as well, you should surround both by the `#if defined(_WIN32)` directives.
14
14
15
-
### Allocating a Console
15
+
### Setting Up a Console Window
16
16
17
-
Because we're now switching from a **Windows Console Application** to a **Windows Application**, we'll need to make sure we have a console to view the output of `stdout` and `stderr`. In addition, because we're exiting right after we encounter an error, we should show a message box, wait for input, then close after the user has acknowledged the error. So, before we call any functions in our constructor, let's add the following code:
17
+
Because we're now switching from a **Windows Console Application** to a **Windows Application**, we'll need to make sure we have a console to view the output of `stdout` and `stderr`. In addition, because we're exiting right after we encounter an error, we should:
18
+
19
+
- Show a message box
20
+
- Wait for user input (keypress)
21
+
- Close after the user has acknowledged the error
22
+
23
+
We'll be using four methods to do this work:
24
+
25
+
```cpp
26
+
BOOL WINAPI AllocConsole(void);
27
+
```
28
+
29
+
- [Documentation](https://goo.gl/8k36tq)
30
+
- This function takes no arguments
31
+
32
+
```cpp
33
+
BOOL WINAPI AttachConsole(
34
+
_In_ DWORD dwProcessId
35
+
);
36
+
```
37
+
38
+
-[Documentation](https://goo.gl/EeSrhh)
39
+
-`dwProcessId` is the identifier of the process whose console is to be used.
- `fileName` is a C string containing the name of the file to be opened.
50
+
- `mode` is a C string containing a file access mode. It can be:
51
+
- `"r"`
52
+
- `"w"`
53
+
- `"a"`
54
+
- `"r+"`
55
+
- `"w+"`
56
+
- `"a+"`
57
+
- `stream` is a pointer to a `FILE` object that identifies the stream to be reopened.
58
+
59
+
```cpp
60
+
BOOL WINAPI SetConsoleTitle(
61
+
_In_ LPCTSTR lpConsoleTitle
62
+
);
63
+
```
64
+
65
+
-[Documentation](https://goo.gl/HAIfMd)
66
+
-`lpConsoleTitle` is the string to be displayed in the title bar of the console window. The total size must be less than 64K.
67
+
68
+
If you put these methods together you can:
69
+
70
+
- Allocate a console
71
+
- Attach the console to the current process
72
+
- Redirect `stdout` and `stderr` to said console
73
+
- Set the title of the console window
74
+
75
+
Let's look at the code:
18
76
19
77
```cpp
20
78
AllocConsole();
@@ -24,7 +82,22 @@ freopen("CON", "w", stderr);
24
82
SetConsoleTitle(TEXT(applicationName));
25
83
```
26
84
27
-
Now, let's modify our `exitOnError` method to show a error message box:
85
+
Now, let's modify our `exitOnError` method to show a error message box. We'll need to use the `MessageBox` method:
86
+
87
+
```cpp
88
+
int WINAPI MessageBox(
89
+
_In_opt_ HWND hWnd,
90
+
_In_opt_ LPCTSTR lpText,
91
+
_In_opt_ LPCTSTR lpCaption,
92
+
_In_ UINT uType
93
+
);
94
+
```
95
+
96
+
-[Documentation](https://goo.gl/7tAVnv)
97
+
-`hWnd` is a handle to the owner window of the message box to be created. If this parameter is `NULL`, the message box has no owner window.
98
+
-`lpText` is the message to be displayed. If the string consists of more than one line, you can separate the lines using a carriage return and/or linefeed character between each line.
99
+
-`lpCaption` is the dialog box title. If this parameter is `NULL`, the default title is "Error".
100
+
-`uType` is the contents and behavior of the dialog box. This parameter can be a combination of flags.
Don't worry about `hInstance` for now. It is simply passed from the `WinMain` method we'll write later on. To setup our window, we'll need to register it with Windows. You can find the documentation for the `RegisterClassEx` method [here](https://goo.gl/m3WViB). The definition looks like this:
0 commit comments