Click here to Skip to main content
15,125,968 members
Articles / Desktop Programming / Windows Forms
Article
Posted 4 Sep 2007

Stats

103.2K views
552 downloads
15 bookmarked

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
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)

Share

About the Author

Leon Fu
Japan Japan
No Biography provided

Comments and Discussions

 
QuestionSimply Superb Pin
Member 37408809-Aug-13 2:58
MemberMember 37408809-Aug-13 2:58 
GeneralMy vote of 5 Pin
Member 37408809-Aug-13 2:57
MemberMember 37408809-Aug-13 2:57 
GeneralA better way to get hWnd Pin
Gregory Montalvo2-Jun-08 10:22
MemberGregory Montalvo2-Jun-08 10:22 
QuestionWhat about PresentationSource.FromVisual? Pin
Georgi Atanasov4-Sep-07 23:26
MemberGeorgi Atanasov4-Sep-07 23:26 
AnswerRe: What about PresentationSource.FromVisual? [modified] Pin
Leon Fu5-Sep-07 7:32
MemberLeon Fu5-Sep-07 7:32 
GeneralRe: What about PresentationSource.FromVisual? Pin
Georgi Atanasov14-Sep-07 11:25
MemberGeorgi Atanasov14-Sep-07 11:25 
AnswerRe: What about PresentationSource.FromVisual? Pin
Husam El-issa17-Mar-11 10:08
MemberHusam El-issa17-Mar-11 10: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.