diff --git a/src/windows/boxed.rs b/src/windows/boxed.rs index dcaeeaf..c72431c 100644 --- a/src/windows/boxed.rs +++ b/src/windows/boxed.rs @@ -3,30 +3,39 @@ use windows::{ core::PCWSTR, Win32::{ Foundation::HWND, - Graphics::Gdi::{CreateDCW, DeleteDC, DeleteObject, GetWindowDC, HBITMAP, HDC}, + Graphics::Gdi::{CreateDCW, DeleteDC, DeleteObject, GetWindowDC, ReleaseDC, HBITMAP, HDC}, }, }; -pub(super) struct BoxHDC(HDC); +pub(super) struct BoxHDC { + hdc: HDC, + hwnd: Option, +} impl Deref for BoxHDC { type Target = HDC; fn deref(&self) -> &Self::Target { - &self.0 + &self.hdc } } impl Drop for BoxHDC { fn drop(&mut self) { + // ReleaseDC 与 DeleteDC 的区别 + // https://learn.microsoft.com/zh-cn/windows/win32/api/winuser/nf-winuser-releasedc unsafe { - DeleteDC(self.0); + if let Some(hwnd) = self.hwnd { + ReleaseDC(hwnd, self.hdc); + } else { + DeleteDC(self.hdc); + } }; } } impl BoxHDC { - pub fn new(hdc: HDC) -> Self { - BoxHDC(hdc) + pub fn new(hdc: HDC, hwnd: Option) -> Self { + BoxHDC { hdc, hwnd } } } @@ -43,15 +52,17 @@ impl From<&[u16; 32]> for BoxHDC { ) }; - BoxHDC::new(hdc) + BoxHDC::new(hdc, None) } } impl From for BoxHDC { fn from(hwnd: HWND) -> Self { + // GetWindowDC vs GetDC, GetDC 不会绘制窗口边框 + // https://learn.microsoft.com/zh-cn/windows/win32/api/winuser/nf-winuser-getwindowdc let hdc = unsafe { GetWindowDC(hwnd) }; - BoxHDC::new(hdc) + BoxHDC::new(hdc, Some(hwnd)) } } diff --git a/src/windows/capture.rs b/src/windows/capture.rs index 1f42430..ab28d31 100644 --- a/src/windows/capture.rs +++ b/src/windows/capture.rs @@ -70,7 +70,7 @@ pub fn capture_monitor(x: i32, y: i32, width: i32, height: i32) -> XCapResult XCapResult