Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Best practise for embedding .NET Winforms in MFC MDI App

Best practise for embedding .NET Winforms in MFC MDI App

Scheduled Pinned Locked Moved Managed C++/CLI
csharpc++visual-studiowinformscom
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rolf Kristensen
    wrote on last edited by
    #1

    I'm working on a large MFC application in VS2005, where a complete rewrite is not an option as it would take years. Instead I'm trying to enable support for .NET plugins, so expert users of the MFC application can write their own .NET classes, which can be loaded dynamically into the MFC application. I'm using the following example as inspiration: Hosting a WinForms User Control in an MFC Application[^] I'm curious if anyone is having any experience in this scenario. I have found that starting a .NET WinForm as a modal CDialog is very easy as one just can call ShowModal. But integrating a .NET WinForm as a CFormView requires that it is a .NET usercontrol within a MFC window. Not sure if I should require that any .NET WinForm should by implemented as a usercontrol, independent of it should work as a CDialog or CFormView ?

    N 1 Reply Last reply
    0
    • R Rolf Kristensen

      I'm working on a large MFC application in VS2005, where a complete rewrite is not an option as it would take years. Instead I'm trying to enable support for .NET plugins, so expert users of the MFC application can write their own .NET classes, which can be loaded dynamically into the MFC application. I'm using the following example as inspiration: Hosting a WinForms User Control in an MFC Application[^] I'm curious if anyone is having any experience in this scenario. I have found that starting a .NET WinForm as a modal CDialog is very easy as one just can call ShowModal. But integrating a .NET WinForm as a CFormView requires that it is a .NET usercontrol within a MFC window. Not sure if I should require that any .NET WinForm should by implemented as a usercontrol, independent of it should work as a CDialog or CFormView ?

      N Offline
      N Offline
      Nish Nishant
      wrote on last edited by
      #2

      You don't need to make everything an user-control. Here's an article I wrote (a while back) that shows how you can use any Winforms control from an MFC dialog: Using WinForms controls in an MFC dialog[^]

      Regards, Nish


      My technology blog: voidnish.wordpress.com Code Project Forums : New Posts Monitor This application monitors for new posts in the Code Project forums.

      R 1 Reply Last reply
      0
      • N Nish Nishant

        You don't need to make everything an user-control. Here's an article I wrote (a while back) that shows how you can use any Winforms control from an MFC dialog: Using WinForms controls in an MFC dialog[^]

        Regards, Nish


        My technology blog: voidnish.wordpress.com Code Project Forums : New Posts Monitor This application monitors for new posts in the Code Project forums.

        R Offline
        R Offline
        Rolf Kristensen
        wrote on last edited by
        #3

        The example you have posted puts a text box control on a MFC Dialog using the CWinFormsControl. Pretty sure that CWinFormsControl cannot host anything but .NET controls. I don't see anywhere that they take WinForm and hosts it in a MFC CFormView. I have tried to use CWinFormsView and it fails when given the typeid of a .NET Winform, but succeeds when given the typeid of a .NET Control. I'm looking for people with experience in this area, that might show some light in this tunnel where I'm walking. I'm curious of how one would solve the issue with getting the .NET control to close the MFC CFormView, change icon/title of the MFC CFormView. The .NET control doesn't get a parent window-object, but only a win32 window handle (integer). My current solution is to have an explicit SetParent on the user-control and make my CWinFormsView-object also inherit from a parent-interface. I don't want my .NET user-control to know anything about the awful world of CLI/Interop.

        modified on Thursday, September 23, 2010 3:11 AM

        N 1 Reply Last reply
        0
        • R Rolf Kristensen

          The example you have posted puts a text box control on a MFC Dialog using the CWinFormsControl. Pretty sure that CWinFormsControl cannot host anything but .NET controls. I don't see anywhere that they take WinForm and hosts it in a MFC CFormView. I have tried to use CWinFormsView and it fails when given the typeid of a .NET Winform, but succeeds when given the typeid of a .NET Control. I'm looking for people with experience in this area, that might show some light in this tunnel where I'm walking. I'm curious of how one would solve the issue with getting the .NET control to close the MFC CFormView, change icon/title of the MFC CFormView. The .NET control doesn't get a parent window-object, but only a win32 window handle (integer). My current solution is to have an explicit SetParent on the user-control and make my CWinFormsView-object also inherit from a parent-interface. I don't want my .NET user-control to know anything about the awful world of CLI/Interop.

          modified on Thursday, September 23, 2010 3:11 AM

          N Offline
          N Offline
          Nish Nishant
          wrote on last edited by
          #4

          I've never had to do that yet but here's one way I would have approached it: Write a generic .NET user control that can host a WinForms form. And then the CWinFormsView will host this user-control. So your end-users can add their forms and then when you want to host it in the view, you host it via the user-control (which will wrap the form). It will be tricky to do I guess, but certainly worth a shot.

          Regards, Nish


          My technology blog: voidnish.wordpress.com Code Project Forums : New Posts Monitor This application monitors for new posts in the Code Project forums.

          R 1 Reply Last reply
          0
          • N Nish Nishant

            I've never had to do that yet but here's one way I would have approached it: Write a generic .NET user control that can host a WinForms form. And then the CWinFormsView will host this user-control. So your end-users can add their forms and then when you want to host it in the view, you host it via the user-control (which will wrap the form). It will be tricky to do I guess, but certainly worth a shot.

            Regards, Nish


            My technology blog: voidnish.wordpress.com Code Project Forums : New Posts Monitor This application monitors for new posts in the Code Project forums.

            R Offline
            R Offline
            Rolf Kristensen
            wrote on last edited by
            #5

            I like the idea of hosting a WinForm, but do you have any idea how to do this ? So far the only thing I have found is how to host a WinForm inside af WPF form. I don't know if I should get into hosting a WPF-Control, that hosts a WinForm-control using WindowsFormsHost. I guess it will require even more code on the generic user-control to wrap around the custom WinForm so it reacts properly to its events.

            modified on Thursday, September 23, 2010 9:42 AM

            N 1 Reply Last reply
            0
            • R Rolf Kristensen

              I like the idea of hosting a WinForm, but do you have any idea how to do this ? So far the only thing I have found is how to host a WinForm inside af WPF form. I don't know if I should get into hosting a WPF-Control, that hosts a WinForm-control using WindowsFormsHost. I guess it will require even more code on the generic user-control to wrap around the custom WinForm so it reacts properly to its events.

              modified on Thursday, September 23, 2010 9:42 AM

              N Offline
              N Offline
              Nish Nishant
              wrote on last edited by
              #6

              Rolf Kristensen wrote:

              I guess it will require even more code on the generic user-control to wrap around the custom WinForm so it reacts properly to its events.

              It will require a fair bit of work, but once you do it, then your end clients won't have to repeat all that extra work each time they write a plugin/add-on. Since I've never had to do that I can't think of any immediate ideas off the top of my head. If I do get some time, I may try and work on a simple prototype (at least that way I can establish if it's practically doable or not). Alternatively, if you start on something and hit a hurdle, it'd be great if you can continue discussing it here.

              Regards, Nish


              My technology blog: voidnish.wordpress.com Code Project Forums : New Posts Monitor This application monitors for new posts in the Code Project forums.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups