question

ansalc-1556 avatar image
0 Votes"
ansalc-1556 asked ·

Findwindow in UWP

I need to set the foreground window in UWP

In VB I used:

Declare Auto Function FindWindow Lib "USER32.DLL" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Declare Auto Function SetForegroundWindow Lib "USER32.DLL" (ByVal hWnd As IntPtr) As Boolean

SetForegroundWindow(FindWindow("xyz", "abc"))


How can I do it in UWP?

When I used then in UWP it compiles but it does not run, it says:
Severity Code Description Project File Line Suppression State
Warning MCG : warning MCG0007: Unresolved P/Invoke method 'USER32.DLL!FindWindow' for method 'System.IntPtr CaUW.MainPage.FindWindow(System.String, System.String)'. Calling this method would throw exception at runtime. Please make sure the P/Invoke either points to a Windows API allowed in UWP applications, or a native DLL that is part of the package. If for some reason your P/Invoke does not satisfy those requirements, please use [DllImport(ExactSpelling=true) to indicate that you understand the implications of using non-UWP APIs. CameraUW

windows-uwp
· 4
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello, I want to confirm that do you want to keep the current window always in the foreground? If not, could you explain more detail about your expected behavior.

0 Votes 0 ·

I want to replicate the behaviour that I was able to achieve in Visual Basic with those functions.


I want to bring a certain window to the foreground when needed.


0 Votes 0 ·

My intention is to bring the window that contains WebView into focus when it is loaded


I've tried WebView2.Focus(FocusState.Programmatic) but it does not work for some reason (my other thread talks about it).


So I'd like to try an alternative like the one below


Private Async Sub WebView2_DOMContentLoaded(sender As WebView, args As WebViewDOMContentLoadedEventArgs) Handles WebView2.DOMContentLoaded



 ' WebView2.Focus(FocusState.Programmatic)

 SetForegroundWindow(FindWindow("Windows.UI.Core.CoreWindow", "Microsoft Edge"))


0 Votes 0 ·

Hi, Is it work for you? If so, you can accept it.

0 Votes 0 ·

1 Answer

RichardZhang-MSFT avatar image
0 Votes"
RichardZhang-MSFT answered ·

Hello,​

Welcome to our Microsoft Q&A platform!

Are you using UWP's multi-view? If yes, you can refer to the instructions below. If not, please let me know

When creating multiple windows, you can use ApplicationView

 Private Async Sub Button_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
     Dim newView As CoreApplicationView = CoreApplication.CreateNewView()
     Dim newViewId As Integer = 0
     Await newView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, Function()
                   Dim frame As Frame = New Frame()
                   frame.Navigate(GetType(SecondaryPage), Nothing)
                   Window.Current.Content = frame
                   Window.Current.Activate()
                   newViewId = ApplicationView.GetForCurrentView().Id
                                                                      End Function)
     Dim viewShown As Boolean = Await ApplicationViewSwitcher.TryShowAsStandaloneAsync(newViewId)
 End Sub

In the above code, newViewId is the identifier of the window, if you need to use it to switch between windows later, you can save it as a global variable.

Later, if you want a window to be displayed in the foreground, you can write:

 Await ApplicationViewSwitcher.SwitchAsync(newViewId);

For more related content, you can refer to this document.

Thanks.


· 4 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello Richard,


Thank you for your interesting information.


However, I am not using UWP's multi-view.


My app launches a WebView object after some time and I need to bring that WebView to the foreground.


I know the Class Name and Window Name of the window that contains the WebView, so, if I could implement SetForegroundWindow(FindWindow("xyz", "abc")) in UWP it would be easy


0 Votes 0 ·

Hello, if you are not using the multi-window creation method provided by UWP, can you provide a code example so that we can understand your intention?

0 Votes 0 ·

Oh, no, SetForegroundWindow(FindWindow("Windows.UI.Core.CoreWindow", "Microsoft Edge")) does not work, that's why I am asking


0 Votes 0 ·

Hello, in UWP applications, you can try the new window creation and switching scheme recommended by UWP applications. The API you currently use may not be supported in UWP.

0 Votes 0 ·