How to use HwndSource

In my WPF application, I have to allow the user to select a certificate. I do this using the X509Certificate2UI.SelectFromCollection method. For proper handling of the dialog, the SelectFromCollection method requires the IntPtr of the Hwnd parents. For this, I found the following code:

HwndSource source = (HwndSource)HwndSource.FromVisual(Window.GetWindow(this)); var certificates= X509Certificate2UI.SelectFromCollection(...,source.Handle); 

So far, this works fine. My question is that I don’t know much knowledge about Win32 or interop, if after that I need to do the cleaning code, or if there are some problems using the HwndSource class, as I did above?

I saw that HwndSource implements IDisposable, but deleting the object after use closes the parent window. Therefore, this does not seem to be intended for use.

+4
source share
1 answer

The WPF window consists of two parts:

  • The window area that consists of the operating system window
  • The windowless region that is inside the WPF window

Now the WPF window, which is the ContentControl, contains everything as content. Therefore, you can say that every pixel of the content inside the Window class is stored in the External window. Each Visual WPF does not have its own HANDLE associated with it; rather, it is the content for an external window element.

See below for details - http://www.abhisheksur.com/2010/12/win32-handle-hwnd-wpf-objects-note.html

So, when you delete this descriptor, you actually dispose of the main window handler, which leads to the completion of the full application.

Therefore, you only retrieve the window handler here and do not create anything you might need for recycling .. !!

+5
source

Source: https://habr.com/ru/post/1380614/


All Articles