joek1975
joek1975

Reputation: 3583

WPF & WinForms Integration and Application Class

I am planning to create a WPF application with a main window which would launch various WinForms. Some of the WinForms use the System.Windows.Forms.Application class (DoEvents, Application.Path, etc). Do you think that there will be a problem in doing this?

Can I still use System.Windows.Forms.Application.DoEvents() from a WinForm that is launched from a WPF application?

Upvotes: 1

Views: 1313

Answers (2)

Alastair Pitts
Alastair Pitts

Reputation: 19601

The main problem will the ability to instantiate the Windows Forms window and set it's owner to that of the WPF window. The Winforms will want a IWin32Window which a WPF window isn't. To get around this, you need to make a custom class.

I found this code on Mark Rendle's blog (I've copied it here as I had to use the Google Cache to access the page). LINK - WARNING: May not work

class Shim : IWin32Window
{
  public Shim(System.Windows.Window owner)
  {
    // Create a WindowInteropHelper for the WPF Window
    interopHelper = new WindowInteropHelper(owner);
  }

  private WindowInteropHelper interopHelper;

  #region IWin32Window Members

  public IntPtr Handle
  {
    get
    {
      // Return the surrogate handle
      return interopHelper.Handle;
    }
  }

  #endregion
}

and it's method of use:

namespace System.Windows.Forms
{
  public static class WPFInteropExtensions
  {
    public static DialogResult ShowDialog(
        this System.Windows.Forms.Form form,
        System.Windows.Window owner)
    {
      Shim shim = new Shim(owner);
      return form.ShowDialog(shim);
    }
  }
}

I haven't tested this code, but reading around the internet, it appears that you can host Winforms windows inside of a WPF app.

I just found this link on MSDN that has a very detailed description of how to interop a Win32 control/window in a WPF application.

Hope these help you out.

Upvotes: 2

dada686
dada686

Reputation: 433

I've been doing this sometimes and didn't encounter any problem. However i don't really recommend it, you should prefer WPF when you are in a WPF Application.

for exemple if you want application path use this : System.Reflection.Assembly.GetExecutingAssembly().Location

Upvotes: 0

Related Questions