mirror of
https://github.com/lifegpc/game-auto-sync.git
synced 2026-06-06 05:48:58 +08:00
Support hide console window when running games on Windows
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -28,6 +28,7 @@ dependencies = [
|
|||||||
"derive_more",
|
"derive_more",
|
||||||
"getopts",
|
"getopts",
|
||||||
"subprocess",
|
"subprocess",
|
||||||
|
"winapi",
|
||||||
"yaml-rust",
|
"yaml-rust",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|||||||
@@ -8,3 +8,6 @@ derive_more = "0.99.18"
|
|||||||
getopts = "0.2.21"
|
getopts = "0.2.21"
|
||||||
subprocess = "0.2.9"
|
subprocess = "0.2.9"
|
||||||
yaml-rust = "0.4.5"
|
yaml-rust = "0.4.5"
|
||||||
|
|
||||||
|
[target.'cfg(windows)'.dependencies]
|
||||||
|
winapi = { version = "0.3", features = ["wincon", "winuser"] }
|
||||||
|
|||||||
@@ -143,4 +143,11 @@ impl Config {
|
|||||||
pub fn rclone_flag(&self) -> Vec<String> {
|
pub fn rclone_flag(&self) -> Vec<String> {
|
||||||
self.get_str_vec("rclone_flag").unwrap_or(vec![])
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
18
src/main.rs
18
src/main.rs
@@ -1,5 +1,7 @@
|
|||||||
mod cfg;
|
mod cfg;
|
||||||
mod utils;
|
mod utils;
|
||||||
|
#[cfg(windows)]
|
||||||
|
mod windows;
|
||||||
|
|
||||||
use getopts::Options;
|
use getopts::Options;
|
||||||
use std::process::ExitCode;
|
use std::process::ExitCode;
|
||||||
@@ -187,7 +189,23 @@ impl Main {
|
|||||||
println!("Run command line: {:?}", cml);
|
println!("Run command line: {:?}", cml);
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} 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)?;
|
let e = Self::call(cml)?;
|
||||||
|
#[cfg(windows)]
|
||||||
|
if hide {
|
||||||
|
windows::show_window();
|
||||||
|
}
|
||||||
let ok = match &e {
|
let ok = match &e {
|
||||||
ExitStatus::Exited(c) => *c == 0,
|
ExitStatus::Exited(c) => *c == 0,
|
||||||
_ => false,
|
_ => false,
|
||||||
|
|||||||
19
src/windows.rs
Normal file
19
src/windows.rs
Normal 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)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user