Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to Get IWin32Window Handle Used in Windows Forms from WPF Application

0.00/5 (No votes)
4 Sep 2007 1  
This article is to give a workaround for converting between different window handles
Screenshot - MessageBox.jpg

Introduction

If we need to use the functions under Windows Forms namespace, we may come across the problem regarding different window handles. How to convert from System.Window handle in WPF to IWin32Window in Windows Forms is what we need to solve in this article.

Background

Here, we just create an example. Windows Forms message box is called by a thread in WPF and modal state is necessary. In this case, IWin32Window handle is required to set the current main window.

In a general Windows Forms application, there is a function named MessageBox.Show which could pop up a message box with different styles.

The following MessageBoxButtons constants are defined in System.Windows.Forms:

Member Name Description
AbortRetryIgnore The message box contains Abort, Retry, and Ignore buttons.
OK The message box contains an OK button.
OKCancel The message box contains OK and Cancel buttons.
RetryCancel The message box contains Retry and Cancel buttons.
YesNo The message box contains Yes and No buttons.
YesNoCancel The message box contains Yes, No, and Cancel buttons.

In WPF Windows namespace, there is a function named MessageBox.Show as well. However, it has different style definitions.

The following MessageBoxButton constants are defined under System.Windows:

Member name Description
OK The message box displays an OK button.
OKCancel The message box displays OK and Cancel buttons.
OKCancel The message box displays Yes and No buttons.
YesNoCancel The message box displays Yes, No, and Cancel buttons.

But sometimes, the styles such as AbortRetryIgnore style message box is quite useful in application development. But if we need to use the message box in Windows Forms with modal state in a multi-threading model, the IWin32Window handle is required instead of a WPF window handle.

Using the Code

The following code could help us get IWin32Window from a WPF application.

Firstly, we could get a Windows handle as an InPtr from process. After that, we need a WindowWrapper to convert the window handle into IWin32Window.

//Get FriendlyName from Application Domain
string strFriendlyName = AppDomain.CurrentDomain.FriendlyName;

//Get process collection by the application name without extension (.exe)
Process[] pro = Process.GetProcessesByName(
    strFriendlyName.Substring(0, strFriendlyName.LastIndexOf('.')));

//Get main window handle pointer and wrap into IWin32Window
System.Windows.Forms.MessageBox.Show(
    new WindowWrapper(pro[0].MainWindowHandle), 
    "Warning Message.", "Title", 
    MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Warning); 

public class WindowWrapper : System.Windows.Forms.IWin32Window
{
  public WindowWrapper(IntPtr handle)
  {
    _hwnd = handle;
  }
  
  public IntPtr Handle
  {
    get { return _hwnd; }
  }

  private IntPtr _hwnd;
}

History

  • 5th September, 2007: Initial post

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here