fix: update console window handling

This commit is contained in:
Timofey Gelazoniya 2024-09-09 23:49:49 +03:00
parent 805ba16c2c
commit fc7dd809fe
Signed by: zeldon
GPG Key ID: 047886915281DD2A

View File

@ -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<u16> = 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");