gpt4 book ai didi

winapi - 在 Rust 中使用 winapi 从窗口获取位图

转载 作者:行者123 更新时间:2023-12-03 11:47:27 29 4
gpt4 key购买 nike

..嗨,我有这个代码:

#[cfg(windows)] extern crate winapi;

use winapi::um::winuser::{FindWindowW, GetClientRect, GetWindowDC, GetDC, ReleaseDC};
use winapi::um::wingdi::{CreateBitmap, CreateCompatibleBitmap, CreateCompatibleDC, SelectObject, GetObjectW, SaveDC, DeleteDC, DeleteObject};
use std::ptr::null_mut;
use std::mem::zeroed;
use std::ffi::OsStr;
use std::iter::once;
use std::os::windows::ffi::OsStrExt;

#[cfg(windows)]
fn find_window(name: &str) {
let window: Vec<u16> = OsStr::new(name).encode_wide().chain(once(0)).collect();

let hwnd = unsafe { FindWindowW(null_mut(), window.as_ptr()) };

if hwnd != null_mut() {
println!("Window found");

let mut my_rect = unsafe { zeroed::<winapi::shared::windef::RECT>() };
let _client_rect = unsafe { GetClientRect(hwnd, &mut my_rect) };
let w = my_rect.right - my_rect.left;
let h = my_rect.bottom - my_rect.top;

let hwnd_dc = unsafe { GetWindowDC(hwnd) };
let mem_dc = unsafe { CreateCompatibleDC(hwnd_dc) };
let bmp = unsafe { CreateCompatibleBitmap(mem_dc, w, h) };

//SelectObject(mem_dc, bmp); <== Problem is here

//DeleteObject(bmp); <== Same problem here
unsafe { DeleteDC(mem_dc) };
unsafe { ReleaseDC(hwnd, hwnd_dc) };
}
else {
println!("Window not found");
}
}

fn main() {
find_window("Firefox"); // just for test
}
如果我运行这个,我会得到:
SelectObject(mem_dc, bmp);
| ^^^ expected enum `std::ffi::c_void`, found enum `winapi::shared::windef::HBITMAP__`
所以我明白了,它不需要那个位图指针,但是在 win32 文档中,它说这个函数(SelectObject)接受一个设备上下文(mem_dc)和一个由 CreateCompatibleBitmap 等函数创建的对象。
https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-selectobject
Rust 中的绑定(bind)接受两个相同的参数,但它似乎想要 std::ffi::c_void 但查看 Rust 文档,我不明白它到底是什么。
你能帮我找出问题所在吗?谢谢。

最佳答案

添加:

use winapi::shared::windef::{HBITMAP, HBITMAP__, HGDIOBJ, HWND, POINT, RECT, SIZE};
...

unsafe {SelectObject(mem_dc, bmp as HGDIOBJ)};
unsafe {DeleteObject(bmp as HGDIOBJ)};

关于winapi - 在 Rust 中使用 winapi 从窗口获取位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64745252/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com