Skip to content

#266 breaks DPI awareness for open_blocking windows #270

Closed

Description

@AlexCharlton

I'm not yet sure why, but #266 seems to have stopped windows created with open_blocking from fetching the right resolution. The window.get_dpi call in after_create is always returning the default 96, regardless of the scale of my screen. Calling get_dpi later on (manually triggering in a click event handler) continues to return 96.

Activity

  1. AlexCharlton commented on Jun 16, 2026

    @AlexCharlton
    ContributorAuthor

    After some experimentation, I've found that this relates to the DpiAwarenessContext getting dropped before window creation. Creating a new DpiAwarenessContext in create_window will open a window with the right resolution, and doesn't seem to have any deleterious effects in a parented context:

    pub fn create_window<W: WindowImpl>(
        title: &HSTRING, style: WindowStyle, nc_size: PhySize, parent: HWND,
        initializer: impl FnOnce(HWnd) -> W + 'static,
    ) -> Result<HWND> {
        let instance = HInstance::get();
        let window_class = RegisteredClass::register_new(instance, Some(wnd_proc::<W>))?;
    
        let data = WindowData::new(initializer, window_class.clone());
    
        let extended_user_32 = super::ExtendedUser32::load().unwrap();
        let _dpi_ctx = super::DpiAwarenessContext::new(&extended_user_32).unwrap();
    
        let hwnd = unsafe {
            CreateWindowExW(
                style.style_ex,
                window_class.as_atom_ptr(),
                title.as_ptr(),
                style.style,
                0,
                0,
                nc_size.width.try_into().unwrap_or(i32::MAX),
                nc_size.height.try_into().unwrap_or(i32::MAX),
                parent,
                null_mut(),
                instance.as_raw(),
                Rc::into_raw(data).cast(),
            )
        };
    
        if hwnd.is_null() {
            return Err(Error::from_win32());
        }
    
        Ok(hwnd)
    }

    Creating the context any later (like in initialize/after_create) does not have any effect.

    @prokopyl, not sure if you think this is the right place to do this, though. If so, I can open a PR.

  2. prokopyl commented on Jun 17, 2026

    @prokopyl
    Member

    Ah, yeah it's quite embarrassing that I missed this, considering the whole point of the DpiAwarenessContext is for it to wrap CreateWindow. 😅

    Thanks for the quick catch, I will release this fix asap!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions