Click here to Skip to main content
15,885,435 members
Articles / Desktop Programming / Windows Forms
Article

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

Rate me:
Please Sign up or sign in to vote.
2.38/5 (8 votes)
4 Sep 2007CPOL2 min read 110.1K   568   17   7
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 NameDescription
AbortRetryIgnoreThe message box contains Abort, Retry, and Ignore buttons.
OKThe message box contains an OK button.
OKCancelThe message box contains OK and Cancel buttons.
RetryCancelThe message box contains Retry and Cancel buttons.
YesNoThe message box contains Yes and No buttons.
YesNoCancelThe 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
OKThe message box displays an OK button.
OKCancelThe message box displays OK and Cancel buttons.
OKCancelThe message box displays Yes and No buttons.
YesNoCancelThe 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.

C#
//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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Japan Japan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionSimply Superb Pin
Member 37408809-Aug-13 1:58
Member 37408809-Aug-13 1:58 
GeneralMy vote of 5 Pin
Member 37408809-Aug-13 1:57
Member 37408809-Aug-13 1:57 
GeneralA better way to get hWnd Pin
Gregory Montalvo2-Jun-08 9:22
Gregory Montalvo2-Jun-08 9:22 
QuestionWhat about PresentationSource.FromVisual? Pin
Georgi Atanasov4-Sep-07 22:26
Georgi Atanasov4-Sep-07 22:26 
AnswerRe: What about PresentationSource.FromVisual? [modified] Pin
Leon Fu5-Sep-07 6:32
Leon Fu5-Sep-07 6:32 
GeneralRe: What about PresentationSource.FromVisual? Pin
Georgi Atanasov14-Sep-07 10:25
Georgi Atanasov14-Sep-07 10:25 
Just to take a notice that "this" may be replaced with any valid Visual instance...
So, when you are creating your working thread you are familiar with all your visuals. For example if you want to show a message box after a button click you will use the "Button" as visual whose PresentationSource is to be retrieved.

Thanks,
Georgi

AnswerRe: What about PresentationSource.FromVisual? Pin
Husam El-issa17-Mar-11 9:08
Husam El-issa17-Mar-11 9:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.