1

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?

2
  • Why would you do that? Do you have old forms that you want to reuse? Commented Jan 7, 2010 at 16:54
  • Yes, I have a fairly large application, and I would need to use both technologies until I could convert all forms over to WPF. Commented Jan 7, 2010 at 17:06

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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?
You shouldn't be using Application.DoEvents() anyway.codinghorror.com/blog/archives/000159.html, but if you have to, I really couldn't say what will happen.
I agree that I need to move away from DoEvents, but this app was originally written pre-.net, and it is everywhere.
Bleh, I feel sorry for you. As I said, i'm really not sure how the DoEvents will work with the interop.
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.
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.