Skip to content

Commit b5df005

Browse files
committed
user feedback (mag_linear)
1 parent 074e948 commit b5df005

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

book_src/opening_a_window/win32.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,24 @@ extern "system" {
10801080
}
10811081
```
10821082

1083+
Okay so then we put those into our `loop`, if there's no problem with getting the message:
1084+
```rust
1085+
loop {
1086+
let message_return = unsafe { GetMessageW(&mut msg, null_mut(), 0, 0) };
1087+
if message_return == 0 {
1088+
break;
1089+
} else if message_return == -1 {
1090+
let last_error = unsafe { GetLastError() };
1091+
panic!("Error with `GetMessageW`, error code: {}", last_error);
1092+
} else {
1093+
unsafe {
1094+
TranslateMessage(&msg);
1095+
DispatchMessageW(&msg);
1096+
}
1097+
}
1098+
}
1099+
```
1100+
10831101
There's a lot of good info on the page about window messages,
10841102
but that's all we have to do here in terms of our code.
10851103

@@ -1099,11 +1117,16 @@ However, a few event types can't just be ignored.
10991117
One of them is that window closing situation.
11001118
Another is that thing with the mouse cursor.
11011119

1120+
First let's do the window closing and cleanup.
11021121
If we look at MSDN page for the [WM_CLOSE](https://docs.microsoft.com/en-us/windows/win32/winmsg/wm-close)
11031122
message, we can see that we'll need to be able to use [DestroyWindow](https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-destroywindow)
11041123
and [PostQuitMessage](https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-postquitmessage).
1124+
We also need to respond to [WM_DESTROY](https://docs.microsoft.com/en-us/windows/win32/winmsg/wm-destroy) as well.
11051125

11061126
```rust
1127+
pub const WM_CLOSE: u32 = 0x0010;
1128+
pub const WM_DESTROY: u32 = 0x0002;
1129+
11071130
#[link(name = "User32")]
11081131
extern "system" {
11091132
/// [`DestroyWindow`](https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-destroywindow)

0 commit comments

Comments
 (0)