yeeeeeeeee

This commit is contained in:
shenjack 2024-11-07 01:26:39 +08:00
parent c59582224a
commit 1164fe18c9
Signed by: shenjack
GPG Key ID: 7B1134A979775551
3 changed files with 69 additions and 9 deletions

View File

@ -1 +0,0 @@
nightly

View File

@ -0,0 +1,2 @@
[toolchain]
channel = "nightly"

View File

@ -1,5 +1,8 @@
use std::num::NonZeroIsize; use std::num::NonZeroIsize;
use winit::event::WindowEvent;
use winit::platform::run_on_demand::EventLoopExtRunOnDemand;
use winit::platform::windows::EventLoopBuilderExtWindows;
use winit::window::WindowAttributes; use winit::window::WindowAttributes;
use winit::{application::ApplicationHandler, event_loop::EventLoop, window::Window}; use winit::{application::ApplicationHandler, event_loop::EventLoop, window::Window};
@ -7,7 +10,7 @@ use windows_sys::Win32::Foundation::HWND;
use windows_sys::Win32::System::Threading::GetCurrentProcessId; use windows_sys::Win32::System::Threading::GetCurrentProcessId;
use windows_sys::Win32::UI::WindowsAndMessaging::{EnumWindows, GetWindowThreadProcessId}; use windows_sys::Win32::UI::WindowsAndMessaging::{EnumWindows, GetWindowThreadProcessId};
use raw_window_handle::{RawWindowHandle, Win32WindowHandle, WinRtWindowHandle}; use raw_window_handle::{RawWindowHandle, Win32WindowHandle};
struct App { struct App {
window: Option<Window>, window: Option<Window>,
@ -17,16 +20,42 @@ struct App {
impl ApplicationHandler for App { impl ApplicationHandler for App {
fn resumed(&mut self, event_loop: &winit::event_loop::ActiveEventLoop) { fn resumed(&mut self, event_loop: &winit::event_loop::ActiveEventLoop) {
// 这里需要获取现有的窗口, 毕竟是运行在一个已有窗口的 Python 程序里 // 这里需要获取现有的窗口, 毕竟是运行在一个已有窗口的 Python 程序里
// self.window = Some(event_loop.ge) if self.window.is_none() {
let mut window_attribute = unsafe { WindowAttributes::default().with_parent_window(Some(self.parent.clone())) }; println!("Create window");
let window_attribute = unsafe { WindowAttributes::default().with_parent_window(Some(self.parent.clone())) };
let window = event_loop.create_window(window_attribute).unwrap();
self.window = Some(window);
}
} }
fn window_event( fn window_event(
&mut self, &mut self,
event_loop: &winit::event_loop::ActiveEventLoop, event_loop: &winit::event_loop::ActiveEventLoop,
window_id: winit::window::WindowId, _window_id: winit::window::WindowId,
event: winit::event::WindowEvent, event: winit::event::WindowEvent,
) { ) {
match event {
WindowEvent::CloseRequested => {
println!("Close window");
event_loop.exit();
}
WindowEvent::Destroyed => {
println!("Window destroyed");
event_loop.exit();
}
WindowEvent::RedrawRequested => {
println!("Redraw window");
// self.ref_window().
event_loop.exit();
}
WindowEvent::MouseInput { button, .. } => {
println!("Mouse input: {:?}", button);
}
WindowEvent::Moved(pos) => {
println!("Window moved: {:?}", pos);
}
_ => {}
}
} }
} }
@ -37,6 +66,10 @@ impl App {
parent: handle, parent: handle,
} }
} }
pub fn ref_window(&self) -> &Window {
self.window.as_ref().unwrap()
}
} }
unsafe extern "system" fn enum_windows_proc(hwnd: HWND, lparam: isize) -> i32 { unsafe extern "system" fn enum_windows_proc(hwnd: HWND, lparam: isize) -> i32 {
@ -50,19 +83,45 @@ unsafe extern "system" fn enum_windows_proc(hwnd: HWND, lparam: isize) -> i32 {
1 1
} }
pub fn render_main() -> anyhow::Result<()> { fn render_thread(handler: isize) -> anyhow::Result<()> {
let window = handler as HWND;
let win32_handle = Win32WindowHandle::new(NonZeroIsize::new(window as isize).unwrap());
let raw_handle = RawWindowHandle::Win32(win32_handle);
let mut app = App::new(raw_handle);
let event_loop = EventLoop::builder().with_any_thread(true).build()?;
// let event_loop = EventLoop::new()?;
event_loop.run_app(&mut app)?;
Ok(())
}
pub fn render_main() {
let mut window: HWND = std::ptr::null_mut(); let mut window: HWND = std::ptr::null_mut();
let result = unsafe { EnumWindows(Some(enum_windows_proc), &mut window as *mut _ as isize) }; let result = unsafe { EnumWindows(Some(enum_windows_proc), &mut window as *mut _ as isize) };
if result != 0 { if result != 0 {
println!("Find window failed"); println!("Find window failed");
return Err(anyhow::anyhow!("Find window failed")); return;
} }
println!("找到 pyglet 的窗口: {:?}", window); println!("找到 pyglet 的窗口: {:?}", window);
// let window_ptr = window as isize;
let win32_handle = Win32WindowHandle::new(NonZeroIsize::new(window as isize).unwrap()); let win32_handle = Win32WindowHandle::new(NonZeroIsize::new(window as isize).unwrap());
let raw_handle = RawWindowHandle::Win32(win32_handle); let raw_handle = RawWindowHandle::Win32(win32_handle);
let app = App::new(raw_handle);
Ok(()) let mut app = App::new(raw_handle);
let mut event_loop = EventLoop::new().unwrap();
// let event_loop = EventLoop::new()?;
// event_loop.run_app(&mut app).unwrap();
let threaded_event_loop = event_loop.run_app_on_demand(&mut app).unwrap();
std::thread::spawn(move || {
// render_thread(window as isize).unwrap();
});
} }