日常同步代码,未包含新增功能只有,新增一些windows窗口相关的代码,待完善

This commit is contained in:
Ekko.bao 2024-08-13 08:50:51 +08:00
parent 5d99d5bf46
commit 041a1526d6
3 changed files with 112 additions and 12 deletions

View File

@ -16,4 +16,7 @@ select = "0.5"
scraper = "0.12" scraper = "0.12"
arboard = "3.4.0" arboard = "3.4.0"
winapi = { version = "0.3.9", features = ["winnt"] } winapi = { version = "0.3.9", features = ["winuser"] }
[profile.test]
env = { "RUST_LOG" = "debug" }

View File

@ -1,17 +1,110 @@
extern crate winapi; extern crate winapi;
extern crate log;
use winapi::um::winuser::{GetCursorPos, GetSystemMetrics, SM_CXSCREEN, SM_CYSCREEN}; use std::ptr::null_mut;
use winapi::shared::windef::POINT; 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 }; let mut point = POINT { x: 0, y: 0 };
unsafe { unsafe {
GetCursorPos(&mut point); GetCursorPos(&mut point);
}
let x = point.x; let x = point.x;
let y = point.y; let y = point.y;
let width = GetSystemMetrics(SM_CXSCREEN); (x, y)
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();
} }

View File

@ -15,7 +15,11 @@ impl Wclip {
self.clipboard.set_text(text).unwrap(); self.clipboard.set_text(text).unwrap();
} }
pub fn get(&mut self) -> String { pub fn get(&mut self) -> Option<String> {
self.clipboard.get_text().unwrap() if let Ok(text) = self.clipboard.get_text() {
Some(text)
} else {
None
}
} }
} }