Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get handle of the MainWindow in WPF [duplicate]

I just created an empty WPF app in VS 2015.

It has

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        var mainWindowHandle = new WindowInteropHelper(this).Handle;
    }
}

But mainWindowHandle is 0 always.

Is it OK? Should it be > 0 ?

like image 423
Friend Avatar asked Mar 13 '23 07:03

Friend


1 Answers

Your window is not shown yet. So the actual window has not been created yet. Try examining this handle in Activated or Loaded event.

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Loaded += OnLoaded;
    }

    private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
    {
        var mainWindowHandle = new WindowInteropHelper(this).Handle;
    }
}
like image 68
filhit Avatar answered Mar 23 '23 14:03

filhit