Support hide console window when running games on Windows

This commit is contained in:
2024-07-20 19:48:25 +08:00
parent 2f4ae442e1
commit fb662be70c
5 changed files with 48 additions and 0 deletions

1
Cargo.lock generated
View File

@@ -28,6 +28,7 @@ dependencies = [
"derive_more",
"getopts",
"subprocess",
"winapi",
"yaml-rust",
]

View File

@@ -8,3 +8,6 @@ derive_more = "0.99.18"
getopts = "0.2.21"
subprocess = "0.2.9"
yaml-rust = "0.4.5"
[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["wincon", "winuser"] }

View File

@@ -143,4 +143,11 @@ impl Config {
pub fn rclone_flag(&self) -> Vec<String> {
self.get_str_vec("rclone_flag").unwrap_or(vec![])
}
#[cfg(windows)]
pub fn hide_window_when_running_exe(&self) -> bool {
self.get_bool("hide_window_when_running_exe")
.map(|s| s.to_owned())
.unwrap_or(true)
}
}

View File

@@ -1,5 +1,7 @@
mod cfg;
mod utils;
#[cfg(windows)]
mod windows;
use getopts::Options;
use std::process::ExitCode;
@@ -187,7 +189,23 @@ impl Main {
println!("Run command line: {:?}", cml);
Ok(())
} else {
#[cfg(windows)]
let need_hide = self._cfg.hide_window_when_running_exe();
#[cfg(windows)]
let hide = if need_hide {
windows::hide_window()
} else {
false
};
#[cfg(windows)]
if need_hide && !hide {
println!("Failed to hide console window.");
}
let e = Self::call(cml)?;
#[cfg(windows)]
if hide {
windows::show_window();
}
let ok = match &e {
ExitStatus::Exited(c) => *c == 0,
_ => false,

19
src/windows.rs Normal file
View File

@@ -0,0 +1,19 @@
use winapi::um::wincon::GetConsoleWindow;
use winapi::um::winuser::{ShowWindow, SW_HIDE, SW_SHOW};
fn console_show_window(n_cmd_show: i32) -> bool {
let h_wnd = unsafe { GetConsoleWindow() };
if h_wnd.is_null() {
println!("Failed to get console window.");
return false;
}
unsafe { ShowWindow(h_wnd, n_cmd_show) != 0 }
}
pub fn show_window() -> bool {
console_show_window(SW_SHOW)
}
pub fn hide_window() -> bool {
console_show_window(SW_HIDE)
}