Click here to Skip to main content
15,912,204 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Is there any equivalent for OnHandleCreated for a window in WPF as is there in C# for a form?

My intention is to get the handle for the window and not for it's child controls. I want to access this handle after it is created. In winforms we could use OnHandleCreated to get the handle as soon as it is created. Is it any alternative for this in WPF window?

Any help would be appreciated. Thanks in advance.
Posted
Updated 15-Apr-14 20:27pm
v3
Comments
[no name] 10-Apr-14 8:27am    
Sorry I misread. http://www.bing.com/search?q=wpf+get+window+handle
Sergey Alexandrovich Kryukov 18-Apr-14 19:33pm    
There are no those handles for child controls. This is not windows-based technology. There is a handle of the main window, but there is no a point using it.
I know some analogous events, but first explain what are you trying to do, otherwise it may turn a waste of time.
—SA

1 solution

Hi,

You can try:

C#
class MyWindow : Window
{
    protected override void OnSourceInitialized(EventArgs e)
    {
         base.OnSourceInitialized(e);
         var handle = new WindowInteropHelper(this).Handle;
         // -- or --
         var hwndSource = (HwndSource)PresentationSource.FromVisual(this);
         var handle = hwndSource.Handle;
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900