Findwindow in UWP

ansalc 436 Reputation points
2020-04-25T08:46:34.227+00:00

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

Universal Windows Platform (UWP)
{count} votes

1 answer

Sort by: Most helpful
  1. Richard Zhang-MSFT 6,936 Reputation points
    2020-04-27T09:20:51.617+00:00

    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.