Skip to content

fix transparency#1805

Open
Mnaisuka wants to merge 3 commits intor0x0r:masterfrom
Mnaisuka:master
Open

fix transparency#1805
Mnaisuka wants to merge 3 commits intor0x0r:masterfrom
Mnaisuka:master

Conversation

@Mnaisuka
Copy link
Copy Markdown
Contributor

@Mnaisuka
Copy link
Copy Markdown
Contributor Author

If you set transparent and then set hidden to true, transparent will not take effect, but I'm not sure if this is a bug.

if window.hidden:
    browser.Opacity = 0
    browser.Show()
    browser.Hide()
    browser.Opacity = 1
elif window.transparent and is_chromium:
    # hack to make transparent window work
    # window is started hidden and shown on Navigating event.
    # no idea why this works
    browser.Show()
    browser.Hide()
else:
    browser.Show()
if window.hidden and not window.transparent:
    browser.Opacity = 0
    browser.Show()
    browser.Hide()
    browser.Opacity = 1
elif window.transparent and is_chromium:
    def on_shown():
        _user32 = ctypes.windll.user32
        _hwnd = window.native.Handle.ToInt32()
        
        _style = _user32.GetWindowLongW(_hwnd,-20)
        _user32.SetWindowLongW(_hwnd,-20,_style | 0x80000)

        _user32.SetLayeredWindowAttributes(_hwnd,0,180,0x2)
        
        if window.hidden:
            _user32.ShowWindow(_hwnd, 0)

    window.events.shown  += on_shown
else:
    browser.Show()

@r0x0r
Copy link
Copy Markdown
Owner

r0x0r commented Apr 3, 2026

Can you fix lint and format job?

@r0x0r
Copy link
Copy Markdown
Owner

r0x0r commented Apr 3, 2026

Transparency does not work properly with examples/transparent.py. It looks like this for me
image

@Mnaisuka
Copy link
Copy Markdown
Contributor Author

Mnaisuka commented Apr 4, 2026

Perhaps refreshing the window in the loaded event would solve the problem?
`2GAFQBC21F%T3II10 _97

@r0x0r
Copy link
Copy Markdown
Owner

r0x0r commented Apr 5, 2026

This works for me, but the problem is that the existing solution works for me as well. Maybe someone for whom current solution does not cut could test it.

@Cristian-F-M
Copy link
Copy Markdown

Cristian-F-M commented Apr 7, 2026

🧪 Windows Transparency Test Results

Hi, I've tested the proposed changes for transparency on Windows (Chromium/WebView2) and found that the current implementation in the PR has some limitations. Below are the results of two different approaches:

❌ Test A: Testing the "Show/Hide" Hack

I tested the approach of using only browser.Show() / Hide() (as seen in some workarounds), but it results in a solid background on my machine, failing to achieve true transparency.

if window.hidden:
    browser.Opacity = 0
    browser.Show()
    browser.Hide()
    browser.Opacity = 1
elif window.transparent and is_chromium:
    # This hack is not enough for consistent transparency
    browser.Show()
    browser.Hide()
else:
    browser.Show()

It looks like:

image

✅ Test B: Testing Win32 API Layered Window

I also tested forcing the WS_EX_LAYERED style via ctypes and the Win32 API. In this case, the transparency works perfectly as intended. This confirms that manipulating the window styles at the OS level seems necessary for Chromium to render transparency correctly on Windows.

if window.hidden and not window.transparent:
    browser.Opacity = 0
    browser.Show()
    browser.Hide()
    browser.Opacity = 1
elif window.transparent and is_chromium:
    def on_shown():
        import ctypes
        _user32 = ctypes.windll.user32
        _hwnd = window.native.Handle.ToInt32()
        
        # GWL_EXSTYLE = -20 | WS_EX_LAYERED = 0x80000
        _style = _user32.GetWindowLongW(_hwnd, -20)
        _user32.SetWindowLongW(_hwnd, -20, _style | 0x80000)

        # LWA_ALPHA = 0x2
        _user32.SetLayeredWindowAttributes(_hwnd, 0, 180, 0x2)
        
        if window.hidden:
            _user32.ShowWindow(_hwnd, 0)

    window.events.shown += on_shown
else:
    browser.Show()

It looks like:

Captura de pantalla 2026-04-07 144347

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

Successfully merging this pull request may close these issues.

3 participants