From fc7dd809fe625ab14f9efe822f9523ebcbfb3a39 Mon Sep 17 00:00:00 2001 From: xzeldon Date: Mon, 9 Sep 2024 23:49:49 +0300 Subject: [PATCH] fix: update console window handling --- src/main.rs | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4b65c9d..74fdccc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,8 @@ #![windows_subsystem = "windows"] +use std::{ffi::OsStr, os::windows::ffi::OsStrExt}; + use tray::TrayApp; -use winapi::um::{self, wincon, winuser}; mod controller; mod devices; @@ -11,21 +12,32 @@ mod tray; fn main() { unsafe { // Allocate new console for the process - um::consoleapi::AllocConsole(); + winapi::um::consoleapi::AllocConsole(); - // Modify the console window's style to remove the system menu (close, minimize, etc.). - winuser::SetWindowLongPtrW( - wincon::GetConsoleWindow(), - winuser::GWL_STYLE, - #[allow(clippy::cast_possible_wrap)] - { - winuser::GetWindowLongPtrW(wincon::GetConsoleWindow(), winuser::GWL_STYLE) - & !winuser::WS_SYSMENU as isize - }, - ); + let title: Vec = OsStr::new("Razer Battery Report Debug Console") + .encode_wide() + .chain(std::iter::once(0)) + .collect(); + winapi::um::wincon::SetConsoleTitleW(title.as_ptr()); + + let hwnd = winapi::um::wincon::GetConsoleWindow(); + + // Disable close command in the sys.menu of the new console, otherwise the whole process will quit: https://stackoverflow.com/a/12015131/126995 + if !hwnd.is_null() { + let hmenu = winapi::um::winuser::GetSystemMenu(hwnd, 0); + if !hmenu.is_null() { + winapi::um::winuser::DeleteMenu( + hmenu, + winapi::um::winuser::SC_CLOSE as u32, + winapi::um::winuser::MF_BYCOMMAND, + ); + } + } // Hide the console window - winuser::ShowWindow(wincon::GetConsoleWindow(), winuser::SW_HIDE); + if !hwnd.is_null() { + winapi::um::winuser::ShowWindow(hwnd, winapi::um::winuser::SW_HIDE); + } } std::env::set_var("RUST_LOG", "trace");