1

Summary: So I have two applications: one is WinForms and the other is WPF. I am trying to figure out how to click a button in the WinForms app and have the WPF app respond to that click.

So to get more detailed, I am working on an existing WinForms application, adding new features. For one of those features, I needed to create a separate WPF application with an InkCanvas that sits on top of the WinForms application and allows the user to draw over the WinForms app.

I have 3 buttons on the WinForms app that are meant to control the InkCanvas: one to enable drawing, one to enable erasing, and one to clear the canvas. What I need to figure out is how to handle those 3 buttons' Click events in the WPF application.

I've been looking at things like PRISM's EventAggregator (although I don't know if that would work between separate processes) and WCF. WCF seems like what I should be using, but I have no experience using it, so it is slow going. I have come across articles about WCF Duplex Services (link)(link)

So I guess my question is: what is the best approach to handling a WinForms button's Click event in a separate WPF application? Apologies in advance if this question has already been asked or if it is too vague.

WERUreo
  • 194
  • 2
  • 12
  • Why not just use the WPF control directly in the WinForms application? You can host WPF controls using the `ElementHost` (http://msdn.microsoft.com/en-us/library/system.windows.forms.integration.elementhost.aspx) control... – Ron Beyer Jul 06 '15 at 17:13
  • I have tried doing that. In a nutshell, transparency is the reason I have been unable to get that to work. Basically, whenever I try to make the background of the ElementHost transparent, it carves out a hole in the 3D scene that is being rendered in the WinForms application. I have been unable to figure out how to prevent that from happening. I had posted a question about that exact problem here a few weeks ago (http://stackoverflow.com/questions/30508405/drawing-over-a-3d-rendered-scene-using-c-sharp). – WERUreo Jul 06 '15 at 17:20
  • Have you tried using an entire WPF Window? http://stackoverflow.com/questions/8311956/open-wpf-window-in-windowsform-app – Ron Beyer Jul 06 '15 at 17:22
  • Hmm, let me give that a try... That is one thing I haven't actually tried yet. – WERUreo Jul 06 '15 at 17:24
  • Holy crap, that actually works... Ugh, I feel like I've wasted DAYS trying to get WCF to work when all I needed was to use a WPF window. Thanks so much for the assistance. Now, what should I do with this question? Technically, your suggestion answers my other question (which if you want to write up as an answer to the question I linked above, that would be awesome). – WERUreo Jul 06 '15 at 17:41
  • Glad it worked, will add it as an answer so you can mark it. – Ron Beyer Jul 06 '15 at 17:43

1 Answers1

1

WinForms can host WPF elements using the ElementHost control, built into the Winforms control library. If that doesn't work in this case (because you are using transparency), you can try using a new WPF window that you overlay on top of the application (see Open WPF window in WindowsForm APP).

This way you can wire up event handlers and communication without having to worry about IPC methods or managing application lifetimes between two different executables.

Community
  • 1
  • 1
Ron Beyer
  • 11,003
  • 1
  • 19
  • 37