How to get focus on WPFwindow started in .Net DLL via VO?

This forum is meant for questions and discussions about the X# language and tools
Post Reply
ic2
Posts: 1798
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

How to get focus on WPFwindow started in .Net DLL via VO?

Post 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
User avatar
Meinhard
Posts: 81
Joined: Thu Oct 01, 2015 4:51 pm

How to get focus on WPFwindow started in .Net DLL via VO?

Post 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
Terry
Posts: 306
Joined: Wed Jan 03, 2018 11:58 am

How to get focus on WPFwindow started in .Net DLL via VO?

Post by Terry »

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
NickFriend
Posts: 248
Joined: Fri Oct 14, 2016 7:09 am

How to get focus on WPFwindow started in .Net DLL via VO?

Post 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
Terry
Posts: 306
Joined: Wed Jan 03, 2018 11:58 am

How to get focus on WPFwindow started in .Net DLL via VO?

Post by Terry »

Something like the following:

public class WebViewDerived : WebView2
{
string ReturnWindowIdent;
public WebViewDerived() : base()
{
//Add ReturnWindowIdent ...
}

public string GetWindowIdent()
{
return ReturnWindowIdent;
}
}

Terry
ic2
Posts: 1798
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

How to get focus on WPFwindow started in .Net DLL via VO?

Post 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
FFF
Posts: 1521
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

How to get focus on WPFwindow started in .Net DLL via VO?

Post by FFF »

So, topmost equals hasFocus ? Godness...
Regards
Karl
(on Win8.1/64, Xide32 2.19, X#2.19.0.2.)
ic2
Posts: 1798
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

How to get focus on WPFwindow started in .Net DLL via VO?

Post 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
Post Reply