Skip to content

Commit 4c61e8d

Browse files
authored
feat: 优化windows代码 (#183)
* feat: windows 权限调整 * feat: 优化代码 * chore: 更新版本号 --------- Co-authored-by: nashaofu <[email protected]>
1 parent 46918cd commit 4c61e8d

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "xcap"
3-
version = "0.2.0"
3+
version = "0.2.1"
44
edition = "2021"
55
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)."
66
license = "Apache-2.0"

src/windows/capture.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ fn to_rgba_image(
4646

4747
unsafe {
4848
// 读取数据到 buffer 中
49-
let is_success = GetDIBits(
49+
let is_failed = GetDIBits(
5050
*box_hdc_mem,
5151
*box_h_bitmap,
5252
0,
@@ -56,7 +56,7 @@ fn to_rgba_image(
5656
DIB_RGB_COLORS,
5757
) == 0;
5858

59-
if is_success {
59+
if is_failed {
6060
return Err(XCapError::new("Get RGBA data failed"));
6161
}
6262
};
@@ -152,7 +152,7 @@ pub fn capture_window(
152152
}
153153

154154
if !is_success {
155-
is_success = PrintWindow(hwnd, *box_hdc_mem, PRINT_WINDOW_FLAGS(3)).as_bool();
155+
is_success = PrintWindow(hwnd, *box_hdc_mem, PRINT_WINDOW_FLAGS(4)).as_bool();
156156
}
157157

158158
if !is_success {

src/windows/impl_window.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ use windows::{
1212
Storage::FileSystem::{GetFileVersionInfoSizeW, GetFileVersionInfoW, VerQueryValueW},
1313
System::{
1414
ProcessStatus::{GetModuleBaseNameW, GetModuleFileNameExW},
15-
Threading::{GetCurrentProcess, GetCurrentProcessId, PROCESS_ALL_ACCESS},
15+
Threading::{
16+
GetCurrentProcess, GetCurrentProcessId, PROCESS_QUERY_LIMITED_INFORMATION,
17+
},
1618
},
1719
UI::WindowsAndMessaging::{
1820
EnumWindows, GetClassNameW, GetWindowInfo, GetWindowLongPtrW, GetWindowTextLengthW,
@@ -216,13 +218,14 @@ fn get_window_pid(hwnd: HWND) -> u32 {
216218

217219
fn get_app_name(pid: u32) -> XCapResult<String> {
218220
unsafe {
219-
let box_process_handle = match BoxProcessHandle::open(PROCESS_ALL_ACCESS, false, pid) {
220-
Ok(box_handle) => box_handle,
221-
Err(err) => {
222-
log::error!("{}", err);
223-
return Ok(String::new());
224-
}
225-
};
221+
let box_process_handle =
222+
match BoxProcessHandle::open(PROCESS_QUERY_LIMITED_INFORMATION, false, pid) {
223+
Ok(box_handle) => box_handle,
224+
Err(err) => {
225+
log::error!("{}", err);
226+
return Ok(String::new());
227+
}
228+
};
226229

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

0 commit comments

Comments
 (0)