Getting the handle of window in C#

前端 未结 3 820
隐瞒了意图╮
隐瞒了意图╮ 2020-12-05 13:49

I have the following class declared:

public partial class MainWindow : Window

And I need to get the actual handle of the window once the wi

相关标签:
3条回答
  • 2020-12-05 14:38
     [DllImport("user32.dll", EntryPoint = "FindWindowEx")]
            public static extern int FindWindowEx(int hwndParent, int hwndEnfant, int lpClasse, string lpTitre);
    
    
    int hwnd = FindWindowEx(0, 0, 0, title);//where title is the windowtitle
    
                    //verification of the window
                    if (hwnd == 0)
                    {
                        throw new Exception("Window not found");
                    }
    
    0 讨论(0)
  • 2020-12-05 14:42

    The earliest place you can get the handle is OnSourceInitialized

    0 讨论(0)
  • 2020-12-05 14:46

    In the OnInitialized method the handle has not yet been created. But you are on the right track. If you put your call in the Loaded event the handle will have been created and it should return the correct handle.

    0 讨论(0)
提交回复
热议问题