tags:

views:

264

answers:

2

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?

A: 

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

dada686
+2  A: 

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.

Alastair Pitts
Thanks for the good information. I guess my question now is can I still use Application.DoEvents() from a WinForm launched from a WPF application?
joek1975
You shouldn't be using Application.DoEvents() anyway.http://www.codinghorror.com/blog/archives/000159.html, but if you have to, I really couldn't say what will happen.
Alastair Pitts
I agree that I need to move away from DoEvents, but this app was originally written pre-.net, and it is everywhere.
joek1975
Bleh, I feel sorry for you. As I said, i'm really not sure how the DoEvents will work with the interop.
Alastair Pitts
I may just go through the UI and replace it. I would rather spend a little time and get rid of it than have some freaky side effects.
joek1975