Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows 11 cant make window transperant. #330

Open
jonatino opened this issue Nov 10, 2023 · 4 comments
Open

Windows 11 cant make window transperant. #330

jonatino opened this issue Nov 10, 2023 · 4 comments

Comments

@jonatino
Copy link

I'm trying to run the example from https://github.com/emoon/rust_minifb/blob/master/examples/transparent.rs on Windows but my window is just one solid color, not transparent.

Is it supported on windows?

@jonatino
Copy link
Author

Was able to get it working with the following code.

    let mut window = Window::new("Raqote", BS_WIDTH as usize, BS_HEIGHT as usize, WindowOptions {
        transparency: false,
        borderless: true,
        scale_mode: ScaleMode::Center,
        none: true,
        title: false,
        resize: false,
        scale: Scale::X1,
        topmost: true,
    }).unwrap();
    let font = SystemSource::new()
        .select_best_match(&[FamilyName::SansSerif], &Properties::new())
        .unwrap()
        .load()
        .unwrap();

    let size = window.get_size();

    let hwnd: HWND = window.get_window_handle() as isize;
    println!("{:?}", hwnd);
    unsafe { SetWindowLongA(hwnd, GWL_EXSTYLE, (WS_EX_LAYERED | WS_EX_TRANSPARENT) as i32) };
    let mut margs = MARGINS {
        cxLeftWidth: -1,
        cxRightWidth: -1,
        cyTopHeight: -1,
        cyBottomHeight: -1,
    };
    unsafe { DwmExtendFrameIntoClientArea(hwnd, &mut margs) };
    unsafe { UpdateWindow(hwnd) };
    unsafe { SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, size.0 as i32, size.1 as i32, SWP_SHOWWINDOW) };

    let mut dt = DrawTarget::new(size.0 as i32, size.1 as i32);
    loop {
        dt.clear(SolidSource::from_unpremultiplied_argb(0, 0, 0, 0));

        let mut pb = PathBuilder::new();
        if let Some(pos) = window.get_mouse_pos(MouseMode::Clamp) {
            pb.rect(pos.0, pos.1, 100., 130.);
            let path = pb.finish();
            dt.fill(&path, &Source::Solid(SolidSource::from_unpremultiplied_argb(50, 0, 0xff, 0)), &DrawOptions::default());

            let pos_string = format!("{:?}", pos);
            dt.draw_text(&font, 36., &pos_string, Point::new(0., 100.),
                         &Source::Solid(SolidSource::from_unpremultiplied_argb(0xff, 255, 0, 0)),
                         &DrawOptions::default(),
            );
        }

        window.update_with_buffer(dt.get_data(), size.0, size.1).unwrap();
    }
    ```

@emoon
Copy link
Owner

emoon commented Nov 10, 2023

There is an open issue for it here #133 I haven't added any of this support myself and I have only accepted PR for it, so if you are willing to do that it would be great!

@jonatino
Copy link
Author

@emoon I'll try to make a PR for it. While I'm at it, do you have any objections to me replacing the winapi crate with the microsoft official windows-sys crate?

@emoon
Copy link
Owner

emoon commented Nov 10, 2023

Both sound good to me! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants