进行一个win32的写

This commit is contained in:
shenjack 2024-11-06 23:32:37 +08:00
parent ea365bbe7d
commit c59582224a
Signed by: shenjack
GPG Key ID: 7B1134A979775551
4 changed files with 99 additions and 65 deletions

View File

@ -66,9 +66,9 @@ checksum = "fc7eb209b1518d6bb87b283c20095f5228ecda460da70b44f0802523dea6da04"
[[package]]
name = "anyhow"
version = "1.0.92"
version = "1.0.93"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "74f37166d7d48a0284b99dd824694c26119c700b53bf0d1540cdb147dbdaaf13"
checksum = "4c95c10ba0b00a02636238b814946408b1322d5ac4760326e6fb8ec956d85775"
[[package]]
name = "approx"
@ -182,9 +182,9 @@ dependencies = [
[[package]]
name = "cc"
version = "1.1.35"
version = "1.1.36"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f57c4b4da2a9d619dd035f27316d7a426305b75be93d09e92f2b9229c34feaf"
checksum = "baee610e9452a8f6f0a1b6194ec09ff9e2d85dea54432acdae41aa0761c95d70"
dependencies = [
"jobserver",
"libc",
@ -339,6 +339,7 @@ dependencies = [
"pyo3",
"quick-xml 0.37.0",
"rapier2d-f64",
"raw-window-handle",
"serde",
"windows-sys 0.59.0",
"winit",
@ -1087,9 +1088,9 @@ dependencies = [
[[package]]
name = "pyo3"
version = "0.22.5"
version = "0.22.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d922163ba1f79c04bc49073ba7b32fd5a8d3b76a87c955921234b8e77333c51"
checksum = "f402062616ab18202ae8319da13fa4279883a2b8a9d9f83f20dbade813ce1884"
dependencies = [
"cfg-if",
"indoc",
@ -1105,9 +1106,9 @@ dependencies = [
[[package]]
name = "pyo3-build-config"
version = "0.22.5"
version = "0.22.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc38c5feeb496c8321091edf3d63e9a6829eab4b863b4a6a65f26f3e9cc6b179"
checksum = "b14b5775b5ff446dd1056212d778012cbe8a0fbffd368029fd9e25b514479c38"
dependencies = [
"once_cell",
"target-lexicon",
@ -1115,9 +1116,9 @@ dependencies = [
[[package]]
name = "pyo3-ffi"
version = "0.22.5"
version = "0.22.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94845622d88ae274d2729fcefc850e63d7a3ddff5e3ce11bd88486db9f1d357d"
checksum = "9ab5bcf04a2cdcbb50c7d6105de943f543f9ed92af55818fd17b660390fc8636"
dependencies = [
"libc",
"pyo3-build-config",
@ -1125,9 +1126,9 @@ dependencies = [
[[package]]
name = "pyo3-macros"
version = "0.22.5"
version = "0.22.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e655aad15e09b94ffdb3ce3d217acf652e26bbc37697ef012f5e5e348c716e5e"
checksum = "0fd24d897903a9e6d80b968368a34e1525aeb719d568dba8b3d4bfa5dc67d453"
dependencies = [
"proc-macro2",
"pyo3-macros-backend",
@ -1137,9 +1138,9 @@ dependencies = [
[[package]]
name = "pyo3-macros-backend"
version = "0.22.5"
version = "0.22.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae1e3f09eecd94618f60a455a23def79f79eba4dc561a97324bf9ac8c6df30ce"
checksum = "36c011a03ba1e50152b4b394b479826cad97e7a21eb52df179cd91ac411cbfbe"
dependencies = [
"heck",
"proc-macro2",

View File

@ -31,9 +31,17 @@ pyo3 = { version = "0.22", features = [
winit = "0.30.5"
rapier2d-f64 = { version = "0.22", features = ["simd-stable"] }
# 虽然但是, raiper在这里!
raw-window-handle = "0.6.2"
[target.'cfg(windows)'.dependencies]
windows-sys = { version = "0.59.0", features = [
"Win32_Foundation",
"Win32_System_Threading",
"Win32_UI_WindowsAndMessaging",
] }
# 虽然但是, raiper在这里!
# windows = { version = "0.58" , features = [
# "UI_Core"
# ]}

View File

@ -1,57 +1,14 @@
use pyo3::pyfunction;
use winit::{application::ApplicationHandler, event_loop::EventLoop, window::Window};
#[cfg(windows)]
mod win;
use windows_sys::Win32::Foundation::HWND;
use windows_sys::Win32::System::Threading::GetCurrentProcessId;
use windows_sys::Win32::UI::WindowsAndMessaging::{EnumWindows, GetWindowThreadProcessId};
use pyo3::pyfunction;
#[pyfunction]
pub fn render_hack() {
println!("render_hacking_start");
render_main();
#[cfg(windows)]
win::render_main();
#[cfg(not(windows))]
println!("对不起不支持非 Windows 捏");
println!("render_hacking_end");
}
#[derive(Default)]
struct App {
window: Option<Window>,
}
impl ApplicationHandler for App {
fn resumed(&mut self, event_loop: &winit::event_loop::ActiveEventLoop) {
// 这里需要获取现有的窗口, 毕竟是运行在一个已有窗口的 Python 程序里
// self.window = Some(event_loop.ge)
}
fn window_event(
&mut self,
event_loop: &winit::event_loop::ActiveEventLoop,
window_id: winit::window::WindowId,
event: winit::event::WindowEvent,
) {
}
}
unsafe extern "system" fn enum_windows_proc(hwnd: HWND, lparam: isize) -> i32 {
let mut process_id = 0;
GetWindowThreadProcessId(hwnd, &mut process_id);
if process_id == GetCurrentProcessId() {
// 这里就是我们要找的窗口
println!("Find window: {:?}", hwnd);
return 0;
}
1
}
fn render_main() -> anyhow::Result<()> {
unsafe {
EnumWindows(Some(enum_windows_proc), 0);
}
// let event_loop = EventLoop::new()?;
// let mut app = App::default();
// event_loop.run_app(&mut app);
Ok(())
}

View File

@ -0,0 +1,68 @@
use std::num::NonZeroIsize;
use winit::window::WindowAttributes;
use winit::{application::ApplicationHandler, event_loop::EventLoop, window::Window};
use windows_sys::Win32::Foundation::HWND;
use windows_sys::Win32::System::Threading::GetCurrentProcessId;
use windows_sys::Win32::UI::WindowsAndMessaging::{EnumWindows, GetWindowThreadProcessId};
use raw_window_handle::{RawWindowHandle, Win32WindowHandle, WinRtWindowHandle};
struct App {
window: Option<Window>,
parent: RawWindowHandle,
}
impl ApplicationHandler for App {
fn resumed(&mut self, event_loop: &winit::event_loop::ActiveEventLoop) {
// 这里需要获取现有的窗口, 毕竟是运行在一个已有窗口的 Python 程序里
// self.window = Some(event_loop.ge)
let mut window_attribute = unsafe { WindowAttributes::default().with_parent_window(Some(self.parent.clone())) };
}
fn window_event(
&mut self,
event_loop: &winit::event_loop::ActiveEventLoop,
window_id: winit::window::WindowId,
event: winit::event::WindowEvent,
) {
}
}
impl App {
pub fn new(handle: RawWindowHandle) -> Self {
Self {
window: None,
parent: handle,
}
}
}
unsafe extern "system" fn enum_windows_proc(hwnd: HWND, lparam: isize) -> i32 {
let mut process_id = 0;
GetWindowThreadProcessId(hwnd, &mut process_id);
if process_id == GetCurrentProcessId() {
println!("找到当前的窗口: {:?}", hwnd);
*(lparam as *mut HWND) = hwnd;
return 0;
}
1
}
pub fn render_main() -> anyhow::Result<()> {
let mut window: HWND = std::ptr::null_mut();
let result = unsafe { EnumWindows(Some(enum_windows_proc), &mut window as *mut _ as isize) };
if result != 0 {
println!("Find window failed");
return Err(anyhow::anyhow!("Find window failed"));
}
println!("找到 pyglet 的窗口: {:?}", window);
let win32_handle = Win32WindowHandle::new(NonZeroIsize::new(window as isize).unwrap());
let raw_handle = RawWindowHandle::Win32(win32_handle);
let app = App::new(raw_handle);
Ok(())
}