以下是如何将HICON
转换为PNG
图标的base64
表示形式:
use base64::{engine::general_purpose, Engine as _};
use image::RgbaImage;
use std::{mem::{self, MaybeUninit},ptr::addr_of_mut};
use windows::Win32::{Foundation::HWND, Graphics::Gdi::{DeleteObject, GetDC, GetDIBits, GetObjectW, ReleaseDC, BITMAP, BITMAPINFOHEADER, BI_RGB,DIB_RGB_COLORS, HDC}, UI::WindowsAndMessaging::{GetIconInfo, HICON}};
unsafe fn icon_to_rgba_image(icon: HICON) -> String {
let bitmap_size = i32::try_from(mem::size_of::<BITMAP>()).unwrap();
let biheader_size = u32::try_from(mem::size_of::<BITMAPINFOHEADER>()).unwrap();
let mut info = MaybeUninit::uninit();
GetIconInfo(icon, info.as_mut_ptr()).unwrap();
let info = info.assume_init_ref();
DeleteObject(info.hbmMask).unwrap();
let mut bitmap: MaybeUninit<BITMAP> = MaybeUninit::uninit();
let result = GetObjectW(
info.hbmColor,
bitmap_size,
Some(bitmap.as_mut_ptr().cast()),
);
assert!(result == bitmap_size);
let bitmap = bitmap.assume_init_ref();
let width = u32::try_from(bitmap.bmWidth).unwrap();
let height = u32::try_from(bitmap.bmHeight).unwrap();
let w = usize::try_from(bitmap.bmWidth).unwrap();
let h = usize::try_from(bitmap.bmHeight).unwrap();
let buf_size = w
.checked_mul(h)
.and_then(|size| size.checked_mul(4))
.unwrap();
let mut buf: Vec<u8> = Vec::with_capacity(buf_size);
let dc = GetDC(HWND(0));
assert!(dc != HDC(0));
let mut bitmap_info = BITMAPINFOHEADER {
biSize: biheader_size,
biWidth: bitmap.bmWidth,
biHeight: -bitmap.bmHeight,
biPlanes: 1,
biBitCount: 32,
biCompression: BI_RGB,
biSizeImage: 0,
biXPelsPerMeter: 0,
biYPelsPerMeter: 0,
biClrUsed: 0,
biClrImportant: 0,
};
let result = GetDIBits(
dc,
info.hbmColor,
0,
height,
Some(buf.as_mut_ptr().cast()),
addr_of_mut!(bitmap_info).cast(),
DIB_RGB_COLORS,
);
buf.set_len(buf.capacity());
let result = ReleaseDC(HWND(0), dc);
assert!(result == 1);
DeleteObject(info.hbmColor).unwrap();
for chunk in buf.chunks_exact_mut(4) {
let [b, _, r, _] = chunk else { unreachable!() };
mem::swap(b, r);
}
let img = RgbaImage::from_vec(width, height, buf).unwrap();
let mut data: Vec<u8> = Vec::new();
img.write_to(&mut Cursor::new(&mut data), image::ImageOutputFormat::Png)?;
general_purpose::STANDARD.encode(&data);
}
=
所以Tauri是建立在TAO的基础上的,这是winit的Forking ,他们为那里的每个平台实现窗口管理.
具体来说是windows/window.rs个,所以在您的情况下,我会克隆TAO存储库并在其中添加所需的更改,然后克隆patch个依赖项以使其指向您的TAO版本.
现在,以下是与TAO implements hicon
相关的类:
let class = WNDCLASSEXW {
cbSize: mem::size_of::<WNDCLASSEXW>() as u32,
style: CS_HREDRAW | CS_VREDRAW | CS_OWNDC,
lpfnWndProc: Some(window_proc),
cbClsExtra: 0,
cbWndExtra: 0,
hInstance: HINSTANCE(GetModuleHandleW(PCWSTR::null()).unwrap_or_default().0),
hIcon: HICON::default(),
hCursor: HCURSOR::default(), // must be null in order for cursor state to work properly
hbrBackground: HBRUSH::default(),
lpszMenuName: PCWSTR::null(),
lpszClassName: PCWSTR::from_raw(class_name.as_ptr()),
hIconSm: HICON::default(),
};
哦,值得一提的是,Tauri使用的是windows箱,而不是winapi
箱.它的下载量较少,但我认为微软的支持更好.从本质上讲,这与apple-sys为Mac所做的非常相似.
正如乔纳森·波特所指出的,你需要用GetIconInfo来获取他的信息:
pub struct ICONINFO {
pub fIcon: BOOL,
pub xHotspot: u32,
pub yHotspot: u32,
pub hbmMask: HBITMAP,
pub hbmColor: HBITMAP,
}
所以回答你最初的问题,不,你不能打印hicon
,因为它只是a handle:
pub type HICON = isize;
附注:这是如何在Windows上TAO check visibility的一个窗口.
附注:Same Place有~7 ways个选项来获取HWND
和其他格式的Windows硬件ID.