Skip to content

Commit 962c20e

Browse files
committed
wip
1 parent a084fc1 commit 962c20e

3 files changed

Lines changed: 62 additions & 3 deletions

File tree

src/wrappers/win32/window.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ mod handle;
33
mod proc;
44
mod window_class;
55

6+
#[cfg(feature = "opengl")]
7+
mod dc;
8+
9+
#[cfg(feature = "opengl")]
10+
pub use dc::*;
11+
612
use data::WindowData;
713
use dpi::PhysicalSize;
814
pub use handle::HWnd;
@@ -16,7 +22,7 @@ use crate::wrappers::win32::h_instance::HInstance;
1622
use crate::wrappers::win32::style::WindowStyle;
1723
use crate::wrappers::win32::DpiAwarenessContext;
1824
use windows_sys::Win32::Foundation::{HWND, LPARAM, LRESULT, WPARAM};
19-
use windows_sys::Win32::UI::WindowsAndMessaging::CreateWindowExW;
25+
use windows_sys::Win32::UI::WindowsAndMessaging::{CreateWindowExW, CW_USEDEFAULT};
2026

2127
pub trait WindowImpl: 'static {
2228
/// Called during the processing of the WM_CREATE message, but after this type was properly
@@ -82,3 +88,30 @@ pub fn create_window<W: WindowImpl>(
8288

8389
Ok(hwnd)
8490
}
91+
92+
pub fn with_dummy_window<T>(handler: impl FnOnce(HWnd) -> Result<T>) -> Result<T> {
93+
let instance = HInstance::get_from_dll();
94+
let window_class = RegisteredClass::register_new(instance, None)?;
95+
let hwnd = unsafe {
96+
CreateWindowExW(
97+
0,
98+
window_class.as_atom_ptr(),
99+
null_mut(),
100+
0,
101+
CW_USEDEFAULT,
102+
CW_USEDEFAULT,
103+
CW_USEDEFAULT,
104+
CW_USEDEFAULT,
105+
null_mut(),
106+
null_mut(),
107+
instance.as_raw(),
108+
null_mut(),
109+
)
110+
};
111+
112+
if hwnd.is_null() {
113+
return Err(Error::from_thread());
114+
}
115+
116+
handler(hwnd)
117+
}

src/wrappers/win32/window/dc.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use crate::wrappers::win32::window::HWnd;
2+
use std::ffi::c_void;
3+
use std::ptr::NonNull;
4+
use windows_core::{Error, Result};
5+
use windows_sys::Win32::Graphics::Gdi::{GetDC, ReleaseDC};
6+
7+
pub struct DeviceContext {
8+
window: HWnd,
9+
inner: NonNull<c_void>,
10+
}
11+
12+
impl DeviceContext {
13+
pub(super) fn from_window(window: HWnd) -> Result<Self> {
14+
let dc = unsafe { GetDC(window.as_raw()) };
15+
16+
let dc = NonNull::new(dc).ok_or_else(Error::from_thread)?;
17+
18+
Ok(Self { window, inner: dc })
19+
}
20+
}
21+
22+
impl Drop for DeviceContext {
23+
fn drop(&mut self) {
24+
unsafe { ReleaseDC(self.window.as_raw(), self.inner.as_ptr()) };
25+
}
26+
}

src/wrappers/win32/window/window_class.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::sync::Arc;
66
use windows_core::{Error, Result, HSTRING};
77
use windows_sys::core::PCWSTR;
88
use windows_sys::Win32::UI::WindowsAndMessaging::{
9-
LoadCursorW, RegisterClassW, UnregisterClassW, CS_OWNDC, IDC_ARROW, WNDCLASSW, WNDPROC,
9+
LoadCursorW, RegisterClassW, UnregisterClassW, IDC_ARROW, WNDCLASSW, WNDPROC,
1010
};
1111

1212
#[derive(Clone)]
@@ -22,7 +22,7 @@ impl RegisteredClass {
2222
hInstance: instance.as_raw(),
2323
lpszClassName: class_name.as_ptr(),
2424

25-
style: CS_OWNDC, // TODO: this is very suspicious
25+
style: 0,
2626
cbClsExtra: 0,
2727
cbWndExtra: 0,
2828
hIcon: null_mut(), // Default icon

0 commit comments

Comments
 (0)