Disclosure Statement: This site contains affiliate links, which means that I may receive a commission if you make a purchase using these links. As an eBay Partner, I earn from qualifying purchases.

WebView is always the topmost form

Post Reply
kopernikus
Posts: 4
Joined: Wed Sep 14, 2022 12:20 pm

WebView is always the topmost form

Post by kopernikus »

I am using WebView4Delphi in a child form. "FormStyle" is set to "fsNormal". Nevertheless, the form always remains staying as topmost. Is there a way to change this behavior?
User avatar
salvadordf
Posts: 4079
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: WebView is always the topmost form

Post by salvadordf »

Hi,

This is known issue in WebView2 :
https://github.com/MicrosoftEdge/WebView2Feedback/issues/356
https://github.com/MicrosoftEdge/WebView2Feedback/issues/286
https://github.com/MicrosoftEdge/WebView2Feedback/issues/708

The suggested workaround is to use a browser in "Windowless mode". See the WindowlessBrowser demo for all the details.

The Windowless mode uses the DirectComposition API :
https://docs.microsoft.com/en-us/windows/win32/directcomp/directcomposition-portal

At this moment Delphi doesn't support the DirectComposition API so we have to use the MfPack component available at GitHub :
https://github.com/FactoryXCode/MfPack

Another possible workaround :
CEF has a similar issue and the suggested workaround is to use WS_EX_NOACTIVATE as the window style. I haven't tried this in WebView2 but perhaps you can call SetWindowLongPtr at some point. I'm not sure which handle you should use with SetWindowLongPtr but here you have some possibilities :

Code: Select all

SetWindowLongPtr(Form1.Handle, GWL_EXSTYLE, WS_EX_NOACTIVATE);
SetWindowLongPtr(WVBrowser1.RenderCompHWND, GWL_EXSTYLE, WS_EX_NOACTIVATE);
SetWindowLongPtr(WVBrowser1.D3DWindowCompHWND, GWL_EXSTYLE, WS_EX_NOACTIVATE);
SetWindowLongPtr(WVBrowser1.Widget0CompHWND, GWL_EXSTYLE, WS_EX_NOACTIVATE);
SetWindowLongPtr(WVBrowser1.Widget1CompHWND, GWL_EXSTYLE, WS_EX_NOACTIVATE);
Let us know if you managed to make WS_EX_NOACTIVATE work! :)
kopernikus
Posts: 4
Joined: Wed Sep 14, 2022 12:20 pm

Re: WebView is always the topmost form

Post by kopernikus »

Thank you for your advice, but unfortunately the WS_EX_NOACTIVATE does not work with the suggested handles. On the other hand, I had partial success with calling SetWindowPos. I added the following call to the OnActivate event of the main window:

Code: Select all

SetWindowPos(frmWebView.Handle,Handle,0,0,0,0,SWP_NOACTIVATE or SWP_NOMOVE or SWP_NOSIZE);
Now, on activating the the main window, the child window with the web view goes behind
the main window. But this works only as long as you don't click on the TWVBrowser component.
kopernikus
Posts: 4
Joined: Wed Sep 14, 2022 12:20 pm

Re: WebView is always the topmost form

Post by kopernikus »

The issue seems to be fixed in WebView 1.0.1901
Last edited by kopernikus on Sat Aug 05, 2023 3:04 pm, edited 1 time in total.
kopernikus
Posts: 4
Joined: Wed Sep 14, 2022 12:20 pm

Re: WebView is always the topmost form

Post by kopernikus »

The issue seems to be fixed in WebView2 version 1.0.1901.177
Post Reply