|
Rank: Newbie Groups: Member
Joined: 4/14/2020 Posts: 4
|
Hello,
I am working with the WebView object in WinForms/C# without using the WebControl wrapper component, assigning a Handle directly to the WebView upon creation. The FrmWebViewer class contains a picBox and that is the handle exposed through ViewHandle.
meoEngine = Engine.Create("E-OT4K"); mfrmViewer = new FrmWebViewer("Viewer", mConfiguration, true); meoWebview = new WebView(); meoWebview.Engine = meoEngine; meoWebview.Create(mfrmViewer.ViewHandle);
This works perfectly fine UNTIL the page throws a New Windows request from that WebView object. I have followed the example "Handling New Window" in the documentation but this refers to a WPF example that uses the WebControl wrapper component, something I am not using and have no intention to use as I am moving to .NET Core 3 for which it is not compatible.
So I try and assign this passed WebView object to an existing variable:
public void WebViewEventSpawn(object sender, NewWindowEventArgs e) { meoWebviewNeo = e.WebView; meoWebviewNeo.Activate += new EventHandler(WebViewEventActivate); e.Accepted = true; }
... AND the existing Browser process jams up and the Activate event is never called. From debugging I can see that the Handle for the created WebView object is 0x0 but it is created from the correct Engine with a populated WindowName and sometimes a Url.
What do I do to actually use this WebView object to continue my workflow? Part of this project is to go Headless and use ThreadRunner, does this suffer the same problem?
I hope I am missing something obvious otherwise I need a way to attach a Windows Control Handle to this WebView without using the WebControl wrapper...
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,217
|
Hi,
The key is you have to create the container for the new WebView. Think this new WebView as a hotel guest and you are the hotel manager. If you say you are going to accept this guest, then you must give him/her a room. You can't say you accept him/her but then walk away and leave him/her hanging there.
The container can be anything that provides the new WebView a parent window handle. For example, it can be a new Form, or it can be another PictureBox in the same Form. And most importantly, since you are not using the WebControl class, you would need to create Create on the new WebView with the new parent window handle as well.
Hope this makes sense to you.
Thanks!
|
|
Rank: Newbie Groups: Member
Joined: 4/14/2020 Posts: 4
|
Thank you for the reply - I get the analogy but to extend it further; I have a room (PictureBox) and I have the key (HWND) but the guest (WebView) has no free hands to take the key.
I can only set the Handle for a WebView in the Constructor, however I am not constructing this WebView, it is already constructed when it arrives in the Event Args. In fact, your documentation explicitly says to use this WebView and not create a new one.
I am probably missing something obvious as I am new to C# and it has been many years since I did Win32 coding, but how do I get the Handle for the PictureBox to the passed WebView so that it has a display context? To complete the analogy - how do I free up a hand to give the guest the key to the room that is waiting...
Many Thanks
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,217
|
Hi,
I am not sure why you say "I can only set the Handle for a WebView in the Constructor". That is not true. You pass the handle to the WebView through its Create method as you have already shown in your own code for the primary WebView. You basically need to do exactly the same thing for the secondary WebView except for the "meoWebview = new WebView();" part because as you have already noticed, the secondary WebView has already been constructed.
Thanks!
|
|
Rank: Newbie Groups: Member
Joined: 4/14/2020 Posts: 4
|
The nomenclature confused me. I'd assumed (and we know what that makes me) that the Create method was the Constructor, given its name and contrary to the configuration I was already doing on the object before calling Create.
I now understand that what it does is takes an existing, configured but not LIVE WebView and associates the Handle. Or in the analogy, Create is the GiveTheKeyToTheGuest method.
For anyone else who finds this post, the Spawn event becomes:
public void WebViewEventSpawn(object sender, NewWindowEventArgs e) { meoWebviewNeo = e.WebView; meoWebviewNeo.Create(pictureBox1.Handle); meoWebviewNeo.Activate += new EventHandler(WebViewEventActivate); e.Accepted = true; }
Apologies for not seeing the obvious - Many thanks for your patience.
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,217
|
Yes. You got it. :)
|
|