|
| 1 | +#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] |
| 2 | + |
| 3 | +use triangle_from_scratch::win32::*; |
| 4 | + |
| 5 | +fn main() { |
| 6 | + let sample_window_class = "Sample Window Class"; |
| 7 | + let sample_window_class_wn = wide_null(sample_window_class); |
| 8 | + |
| 9 | + let mut wc = WNDCLASSW::default(); |
| 10 | + wc.lpfnWndProc = Some(window_procedure); |
| 11 | + wc.hInstance = get_process_handle(); |
| 12 | + wc.lpszClassName = sample_window_class_wn.as_ptr(); |
| 13 | + wc.hCursor = load_predefined_cursor(IDCursor::Arrow).unwrap(); |
| 14 | + |
| 15 | + let _atom = unsafe { register_class(&wc) }.unwrap(); |
| 16 | + |
| 17 | + let lparam: *mut i32 = Box::leak(Box::new(5_i32)); |
| 18 | + let hwnd = unsafe { |
| 19 | + create_app_window( |
| 20 | + sample_window_class, |
| 21 | + "Sample Window Name", |
| 22 | + None, |
| 23 | + [800, 600], |
| 24 | + lparam.cast(), |
| 25 | + ) |
| 26 | + } |
| 27 | + .unwrap(); |
| 28 | + let _previously_visible = unsafe { ShowWindow(hwnd, SW_SHOW) }; |
| 29 | + |
| 30 | + loop { |
| 31 | + match get_any_message() { |
| 32 | + Ok(msg) => { |
| 33 | + if msg.message == WM_QUIT { |
| 34 | + break; |
| 35 | + } |
| 36 | + translate_message(&msg); |
| 37 | + unsafe { |
| 38 | + DispatchMessageW(&msg); |
| 39 | + } |
| 40 | + } |
| 41 | + Err(e) => panic!("Error when getting from the message queue: {}", e), |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +pub unsafe extern "system" fn window_procedure( |
| 47 | + hwnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM, |
| 48 | +) -> LRESULT { |
| 49 | + match msg { |
| 50 | + WM_NCCREATE => { |
| 51 | + println!("NC Create"); |
| 52 | + let createstruct: *mut CREATESTRUCTW = lparam as *mut _; |
| 53 | + if createstruct.is_null() { |
| 54 | + return 0; |
| 55 | + } |
| 56 | + let boxed_i32_ptr = (*createstruct).lpCreateParams; |
| 57 | + SetWindowLongPtrW(hwnd, GWLP_USERDATA, boxed_i32_ptr as LONG_PTR); |
| 58 | + return 1; |
| 59 | + } |
| 60 | + WM_CREATE => println!("Create"), |
| 61 | + WM_CLOSE => { |
| 62 | + let text_null = wide_null("Really quit?"); |
| 63 | + let caption_null = wide_null("My Caption"); |
| 64 | + let mb_output = MessageBoxW( |
| 65 | + hwnd, |
| 66 | + text_null.as_ptr(), |
| 67 | + caption_null.as_ptr(), |
| 68 | + MB_OKCANCEL, |
| 69 | + ); |
| 70 | + if mb_output == IDOK { |
| 71 | + let _success = DestroyWindow(hwnd); |
| 72 | + } |
| 73 | + } |
| 74 | + WM_DESTROY => { |
| 75 | + let ptr = GetWindowLongPtrW(hwnd, GWLP_USERDATA) as *mut i32; |
| 76 | + Box::from_raw(ptr); |
| 77 | + println!("Cleaned up the box."); |
| 78 | + PostQuitMessage(0); |
| 79 | + } |
| 80 | + WM_PAINT => { |
| 81 | + let ptr = GetWindowLongPtrW(hwnd, GWLP_USERDATA) as *mut i32; |
| 82 | + println!("Current ptr: {}", *ptr); |
| 83 | + *ptr += 1; |
| 84 | + let mut ps = PAINTSTRUCT::default(); |
| 85 | + let hdc = BeginPaint(hwnd, &mut ps); |
| 86 | + let _success = FillRect(hdc, &ps.rcPaint, (COLOR_WINDOW + 1) as HBRUSH); |
| 87 | + EndPaint(hwnd, &ps); |
| 88 | + } |
| 89 | + _ => return DefWindowProcW(hwnd, msg, wparam, lparam), |
| 90 | + } |
| 91 | + 0 |
| 92 | +} |
0 commit comments