Skip to content

Commit 8efeea4

Browse files
Update chap04/chap04-windows.md
1 parent 9b56175 commit 8efeea4

File tree

1 file changed

+77
-4
lines changed

1 file changed

+77
-4
lines changed

chap04/chap04-windows.md

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,67 @@ Because we're going to be writing this from scratch, we're going to have to inte
1212

1313
If you're targeting Linux as well, you should surround both by the `#if defined(_WIN32)` directives.
1414

15-
### Allocating a Console
15+
### Setting Up a Console Window
1616

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.
40+
41+
```cpp
42+
FILE * freopen (
43+
const char * filename,
44+
const char * mode,
45+
FILE * stream );
46+
```
47+
48+
- [Documentation](http://www.cplusplus.com/reference/cstdio/freopen/)
49+
- `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:
1876

1977
```cpp
2078
AllocConsole();
@@ -24,7 +82,22 @@ freopen("CON", "w", stderr);
2482
SetConsoleTitle(TEXT(applicationName));
2583
```
2684
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.
28101

29102
```cpp
30103
MessageBox(NULL, msg, applicationName, MB_ICONERROR);
@@ -45,7 +118,7 @@ HWND window;
45118
In this section we'll be writing the body this method:
46119

47120
```cpp
48-
void VulkanExample::createWindow(HINSTANCE hInstance) {}
121+
void createWindow(HINSTANCE hInstance) {}
49122
```
50123
51124
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

Comments
 (0)