Skip to content

Commit

Permalink
feat: 优化windows代码 (#183)
Browse files Browse the repository at this point in the history
* feat: windows 权限调整

* feat: 优化代码

* chore: 更新版本号

---------

Co-authored-by: nashaofu <[email protected]>
  • Loading branch information
nashaofu and nashaofu authored Jan 4, 2025
1 parent 46918cd commit 4c61e8d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "xcap"
version = "0.2.0"
version = "0.2.1"
edition = "2021"
description = "XCap is a cross-platform screen capture library written in Rust. It supports Linux (X11, Wayland), MacOS, and Windows. XCap supports screenshot and video recording (WIP)."
license = "Apache-2.0"
Expand Down
6 changes: 3 additions & 3 deletions src/windows/capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn to_rgba_image(

unsafe {
// 读取数据到 buffer 中
let is_success = GetDIBits(
let is_failed = GetDIBits(
*box_hdc_mem,
*box_h_bitmap,
0,
Expand All @@ -56,7 +56,7 @@ fn to_rgba_image(
DIB_RGB_COLORS,
) == 0;

if is_success {
if is_failed {
return Err(XCapError::new("Get RGBA data failed"));
}
};
Expand Down Expand Up @@ -152,7 +152,7 @@ pub fn capture_window(
}

if !is_success {
is_success = PrintWindow(hwnd, *box_hdc_mem, PRINT_WINDOW_FLAGS(3)).as_bool();
is_success = PrintWindow(hwnd, *box_hdc_mem, PRINT_WINDOW_FLAGS(4)).as_bool();
}

if !is_success {
Expand Down
22 changes: 13 additions & 9 deletions src/windows/impl_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ use windows::{
Storage::FileSystem::{GetFileVersionInfoSizeW, GetFileVersionInfoW, VerQueryValueW},
System::{
ProcessStatus::{GetModuleBaseNameW, GetModuleFileNameExW},
Threading::{GetCurrentProcess, GetCurrentProcessId, PROCESS_ALL_ACCESS},
Threading::{
GetCurrentProcess, GetCurrentProcessId, PROCESS_QUERY_LIMITED_INFORMATION,
},
},
UI::WindowsAndMessaging::{
EnumWindows, GetClassNameW, GetWindowInfo, GetWindowLongPtrW, GetWindowTextLengthW,
Expand Down Expand Up @@ -216,13 +218,14 @@ fn get_window_pid(hwnd: HWND) -> u32 {

fn get_app_name(pid: u32) -> XCapResult<String> {
unsafe {
let box_process_handle = match BoxProcessHandle::open(PROCESS_ALL_ACCESS, false, pid) {
Ok(box_handle) => box_handle,
Err(err) => {
log::error!("{}", err);
return Ok(String::new());
}
};
let box_process_handle =
match BoxProcessHandle::open(PROCESS_QUERY_LIMITED_INFORMATION, false, pid) {
Ok(box_handle) => box_handle,
Err(err) => {
log::error!("{}", err);
return Ok(String::new());
}
};

let mut filename = [0; MAX_PATH as usize];
GetModuleFileNameExW(*box_process_handle, None, &mut filename);
Expand Down Expand Up @@ -374,7 +377,8 @@ impl ImplWindow {
pub fn capture_image(&self) -> XCapResult<RgbaImage> {
// 在win10之后,不同窗口有不同的dpi,所以可能存在截图不全或者截图有较大空白,实际窗口没有填充满图片
// 如果窗口不感知dpi,那么就不需要缩放,如果当前进程感知dpi,那么也不需要缩放
let box_process_handle = BoxProcessHandle::open(PROCESS_ALL_ACCESS, false, self.pid)?;
let box_process_handle =
BoxProcessHandle::open(PROCESS_QUERY_LIMITED_INFORMATION, false, self.pid)?;
let window_is_dpi_awareness = get_process_is_dpi_awareness(*box_process_handle)?;
let current_process_is_dpi_awareness =
unsafe { get_process_is_dpi_awareness(GetCurrentProcess())? };
Expand Down

0 comments on commit 4c61e8d

Please sign in to comment.