From 041a1526d65c8887fb8aa859f1143129d0b188a5 Mon Sep 17 00:00:00 2001 From: Begild Date: Tue, 13 Aug 2024 08:50:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E5=B8=B8=E5=90=8C=E6=AD=A5=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=EF=BC=8C=E6=9C=AA=E5=8C=85=E5=90=AB=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E5=8F=AA=E6=9C=89=EF=BC=8C=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E4=B8=80=E4=BA=9Bwindows=E7=AA=97=E5=8F=A3=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E7=9A=84=E4=BB=A3=E7=A0=81=EF=BC=8C=E5=BE=85=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.toml | 5 +- src/sys_res/mod.rs | 111 +++++++++++++++++++++++++++++++++++++++++---- src/wclip/mod.rs | 8 +++- 3 files changed, 112 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ee8408f..d7025a6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,4 +16,7 @@ select = "0.5" scraper = "0.12" arboard = "3.4.0" -winapi = { version = "0.3.9", features = ["winnt"] } +winapi = { version = "0.3.9", features = ["winuser"] } + +[profile.test] +env = { "RUST_LOG" = "debug" } diff --git a/src/sys_res/mod.rs b/src/sys_res/mod.rs index f9ee372..c25fdbd 100755 --- a/src/sys_res/mod.rs +++ b/src/sys_res/mod.rs @@ -1,17 +1,110 @@ extern crate winapi; - -use winapi::um::winuser::{GetCursorPos, GetSystemMetrics, SM_CXSCREEN, SM_CYSCREEN}; -use winapi::shared::windef::POINT; +extern crate log; +use std::ptr::null_mut; +use winapi::um::winuser::{ + GetCursorPos, GetSystemMetrics, SM_CXSCREEN, SM_CYSCREEN, + FindWindowA, GetWindowRect +}; +use winapi::shared::windef::{ + POINT, + RECT, +}; +use winapi::um::winuser::{GetForegroundWindow, GetWindowTextW, GetClassNameW, GetWindowThreadProcessId}; +use std::ptr; +use std::ffi::{OsString, CString}; +use std::os::windows::ffi::OsStringExt; +use winapi::um::processthreadsapi::{OpenProcess, GetProcessImageFileNameA}; +use winapi::um::winnt::PROCESS_QUERY_INFORMATION; -pub fn get_screen_size() -> (i32, i32) { +pub fn screen_size() -> (i32, i32) { + unsafe { + let width = GetSystemMetrics(SM_CXSCREEN); + let height = GetSystemMetrics(SM_CYSCREEN); + (width, height) + } +} + +pub fn cursor_pos() -> (i32, i32) { let mut point = POINT { x: 0, y: 0 }; unsafe { GetCursorPos(&mut point); + let x = point.x; + let y = point.y; + (x, y) } - let x = point.x; - let y = point.y; - let width = GetSystemMetrics(SM_CXSCREEN); - let height = GetSystemMetrics(SM_CYSCREEN); - (x, y, width, height) +} + +pub fn get_window_rect() -> Option<(i32, i32, i32, i32)> { + unsafe { + let h = GetForegroundWindow(); + + let mut rect: RECT = std::mem::zeroed(); + if GetWindowRect(h, &mut rect) != 0 { + Some((rect.left, rect.top, rect.right, rect.bottom)) + } else { + None + } + } +} + + +pub fn get_foredround_window_name() { + unsafe { + let foreground_window = GetForegroundWindow(); + + // let mut process_id = 0; + // GetWindowThreadProcessId(foreground_window, &mut process_id); + + // let mut process_name: [u16; 1024] = [0; 1024]; + // GetWindowTextW(foreground_window, process_name.as_mut_ptr(), 1024); + + let process_handle = OpenProcess(PROCESS_QUERY_INFORMATION, 0, pid); + if process_handle.is_null() { + return None; + } + + let mut image_name = [0u8; 1024]; + let mut image_name_size = image_name.len() as u32; + if GetProcessImageFileNameA(process_handle, image_name.as_mut_ptr() as *mut _, &mut image_name_size) == 0 { + return None; + } + + let mut class_name: [u16; 256] = [0; 256]; + GetClassNameW(foreground_window, class_name.as_mut_ptr(), 256); + + let mut process_name_str = OsString::from_wide(&process_name); + let mut class_name_str = OsString::from_wide(&class_name); + + log::info!("Process Name: {}", process_name_str.to_string_lossy().to_string()); + log::info!("Class Name: {}", class_name_str.to_string_lossy().to_string()); + } +} + + +#[test] +fn test_screen_size() { + let (width, height) = screen_size(); + assert!(width > 0); + assert!(height > 0); +} +#[test] +fn test_get_window_rect() { + let (x, y, width, height) = get_window_rect().unwrap(); + assert!(x >= 0); + assert!(y >= 0); + assert!(width > 0); + assert!(height > 0); +} +#[test] +fn test_cursor_pos() { + let (x, y) = cursor_pos(); + assert!(x >= 0); + assert!(y >= 0); +} + +#[test] +fn test_get_foredround_window_name() { + env_logger::init(); + get_foredround_window_name(); } \ No newline at end of file diff --git a/src/wclip/mod.rs b/src/wclip/mod.rs index 40e34de..a62e2ff 100644 --- a/src/wclip/mod.rs +++ b/src/wclip/mod.rs @@ -15,7 +15,11 @@ impl Wclip { self.clipboard.set_text(text).unwrap(); } - pub fn get(&mut self) -> String { - self.clipboard.get_text().unwrap() + pub fn get(&mut self) -> Option { + if let Ok(text) = self.clipboard.get_text() { + Some(text) + } else { + None + } } }