Page 1 of 1
How to get focus on WPFwindow started in .Net DLL via VO?
Posted: Wed Apr 21, 2021 10:43 am
by ic2
I've successfully created a WPF window with the new Microsoft WebView2 (Edge Chromium based) runtime component on it. This allows me to show me an e-mail without script errors as on the old IE based HTML viewer. However, when I open the window, focus returns to the VO window from where I opened that e-mail.
oDotNet:= OLEAutoObject{"DotNetLibForVO.VoDotNetClass"}
IF !oDotNet:Finit
// .....
ENDIF
IF !oDotNet:ShowEmailInWebView2(cHTML,cCaption)
....
ENDIF
ShowEmailInWebView2 is an X# method which eventually leads to opening a WPF window, from within my DotNet DLL.
I tried adding this code in VO:
hWindow:=FindWindow(NULL_PSZ, String2Psz(cCaption))
SetForegroundWindow(hWindow)
but this leaves me with 1 question: How can I identify the window? In the above code I tried the caption (=the subject of a mail) I pass to the
.Net library and which is displayed in the WPF window, but the window does not get focus when I add these 2 lines.
Dick
How to get focus on WPFwindow started in .Net DLL via VO?
Posted: Wed Apr 21, 2021 12:54 pm
by Meinhard
Dick,
in Xaml you would use
Topmost="True"
in the <Window....> Tag. In code behind this shoul be:
this.Topmost = true
Regards
Meinhard
How to get focus on WPFwindow started in .Net DLL via VO?
Posted: Wed Apr 21, 2021 1:20 pm
by TerryB1
Hi Dick
The simple answer is “I don’t know” but at the risk of again misunderstanding what you’re trying to do this may be worth some thought.
I assume you’ve used an instance of WebView2.
Could you derive a control say WebView2Derived adding in return window identifier and interrogating that?
Terry
How to get focus on WPFwindow started in .Net DLL via VO?
Posted: Wed Apr 21, 2021 1:52 pm
by NickFriend
Hi Dick,
You can use WindowInteropHelper to set the VO window as the owner of the WPF window, then if you call wpfwindow.Activate() it will bring it to the top.
I expose a method in my WPF COM client class to do this, so it can be called from VO any time you want to bring the WPF window to the top.
Just about to go into a video conference so can't go into more detail at this moment, but we have this working fine.
HTH
Nick
How to get focus on WPFwindow started in .Net DLL via VO?
Posted: Wed Apr 21, 2021 2:22 pm
by TerryB1
Something like the following:
public class WebViewDerived : WebView2
{
string ReturnWindowIdent;
public WebViewDerived() : base()
{
//Add ReturnWindowIdent ...
}
public string GetWindowIdent()
{
return ReturnWindowIdent;
}
}
Terry
How to get focus on WPFwindow started in .Net DLL via VO?
Posted: Wed Apr 21, 2021 2:54 pm
by ic2
Thanks Meinhard, Terry & Nick for the quick response.
I started "top down" and adding Topmost="True" in the XAML already did what I what wanted, great!
The solutions of Nick & Terry may come in handy in other situations.
Dick
How to get focus on WPFwindow started in .Net DLL via VO?
Posted: Wed Apr 21, 2021 6:18 pm
by FFF
So, topmost equals hasFocus ? Godness...
How to get focus on WPFwindow started in .Net DLL via VO?
Posted: Fri May 07, 2021 1:24 pm
by ic2
One addition: Topmost is called Topmost because it doesn't only get focus but it is also impossible to move another window in front of it, when it's open.
This works to get focus too:
SELF:hHandleToWebView2Win:=FindWindow(NULL_PSZ, String2Psz("WebWindow")) // Use this handle after expose
(where WebWindow it the original name/caption of the WPF window (which I overwrite after creation with the subject of the e-mail). In the Expose of the window, after all other potential "focus takers" came along, I program:
IF SELF:hHandleToWebView2Win<>NULL_PTR
SetForegroundWindow(SELF:hHandleToWebView2Win)
SELF:hHandleToWebView2Win:=NULL_PTR
ENDIF
This works to have this window get focus.
Dick