Click here to Skip to main content
11,928,078 members (51,886 online)
Rate this:
Please Sign up or sign in to vote.
See more: C++/CLI .NET MFC .NET4

I have a project in Visual Studio 2010 and my solution explorer have two project, the first project is a MFC(Main System) and the second is a C++/CLI(Sub System). I have Windows form from C++/CLI project (second project) and I want call it from MFC project (first project),But I want to set MdiParent properties of my Windows form to MFC main window. Actually I want to set my widows form child of Mainwidow form, in MFC project when I call that.

Any help would be appreciated.
Best Regards.

KTCLogic::BaseForm ^baseform = gcnew KTCLogic::BaseForm();
Posted 16-Apr-13 4:13am
Edited 16-Apr-13 4:19am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You can, but… do yourself a great favor: don't go there, don't waste your time. If you want to use .NET at all, you will much easier migrate to .NET completely, compared to a mixed-mode project with MFC.

If you really want to do it: use Windows API SetParent:[^].

You can get the form's handle as[^].

Do yourself a great favor, don't do it.

Dear Sergey

I am currently work in mixed-mode and Previous solutions in this regard was excellent, But I have problem with my forms child and parents. I don`t know how can I set my new form in .NET(C++/CLI) MdiParent is MFC main form.

Best Regards.
Can't you see that I answered in full?
I see, I will test it as soon as possible so Thank you. :)
Don't forget to accept the answer formally (green button).
In all cases, your follow-up questions will be welcome.
Dear Sergey could you tell me how should I use please ? where I should define this dll and where I should call that ?

[DllImport("User32", CharSet=CharSet.Auto, ExactSpelling=true)]
internal static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndParent);
Why P/Invoke? (You can, but...) With Windows API and mixed mode this is quite pointless, as you can directly use unmanaged code:
link Windows, #include <windows.h> and so on...
I don`t understand how to tell my windows form :

in MFC :

KTCLogic::BaseForm ^baseform = gcnew KTCLogic::BaseForm();
baseform->MdiParent= CManiForm // For example

I have a problem with baseform->MdiParent= CManiForm // For example !?
Sorry. Please disregard my previous message you could see in your notifications; I misplaced it, had to address my words to a different member, not you...
All wrong. You did not tell me that this is MDI. Now, what do you want to make a child of what? You can make a MDI child in MFC, and make a .NET control its child. Before we continue, you badly need another answer. Please see Solution 2.

I find out this solution ;)
In other words, you decided to go in the same direction, .NET-only? Good. Only I don't understand why didn't you accept my solution formally then (green button)... :-)
Only using MDI is a bad idea. Microsoft developers checked themselves and started to discourage it.
Sergey actually I could not understand other solution :P ;)
If you want me to help to understand it, it would be good if you could let me know which part you don't understand.
Dear Sergey
So thank you, I used to do it this way:

1- Create Windows Application project in C# and add in to the MFC main project
2- Add reference of C# project in MFC
3- Create your windows form in C# project
4- After using C++/CLI and lab lab lab
5- From the MFC main form I wrote these codes:

CString logicname;
logicname = "logic1";
FindWindowByTitle fwbt("logic1", true);

HWND hwnd1 ;
hwnd1 = fwbt.FindIt();

KTCLogic::MDIParentMain ^logicmain = gcnew KTCLogic::MDIParentMain();
logicmain->TopMost = true;

FindWindowByTitle fwbt1("KTC-Logic Editor", true);
HWND hwnd2 ;
hwnd2 = fwbt1.FindIt();


So, do you have a better solution? of course I have problem in hosting my windows form in MFC Dialog Box still yet :)
This is a question about this answer, not about "other answer".
Sorry. When I already answered, I started to think that I had to warn you that this is not as simple as it may look. Before you can use "SetParent", you need HWND for both Windows. However, you cannot get a HWND for .NET windowed control before you actually insert it in running System.Window.Forms.Application. So, you could run MFC application in one thread, .NET Forms application in another thread (I tested something pretty similar, in a more complex situation), create a Control or Form you need, and SetParent could "jump" it from one parent to another... In other words, this is possible by too troublesome to do the job.

This is yet another argument against mixed-mode. Mixed-mode modules could be a real blessing, but mixing two different UI it too much to bother. Remember, by main advice was: "Don't to it" in first place. You really should switch to .NET entirely, at least in all related to UI.

If you mean as Mixed-Mode is C++/CLI, My MFC project in C++/CLI right now, so I work in the mixed-mode state. But from the mixed-mode we have still different UI problem, because my project is MFC and C#.NET.
Sergey Alexandrovich Kryukov 22-Apr-13 11:16am
I'm talking not in mixed project, which is fine, but about mixing MFC with System.Windows.Forms.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Here is the idea: who needs MDI, ever? Why torturing yourself and scaring off your users?
Do yourself a great favor: do not use MDI at all. You can do much easier to implement design without it, with much better quality. MDI is highly discouraged even by Microsoft, in fact, Microsoft dropped it out of WPF and will hardly support it. More importantly, you will scare off all your users if you use MDI. Just don't. Please see:[^],
How to Create MDI Parent Window in WPF?[^].

I can explain what to do instead. Please see my past answers:
How to Create MDI Parent Window in WPF? [Solution 2],
Question on using MDI windows in WPF[^],
MDIContainer giving error[^],
How to set child forms maximized, last childform minimized[^].

When it comes to mixed mode (managed + unmanaged) with MFC, it gets a lot worse. Don't do it.

So what is your solution if we want opening our .NET forms from MFC main window?
First, absolute priority: never ever use MDI. There are different options, one of them is the tab control, with tab pages playing the roles of "windows". There are a lot more designs, including docking design as in Visual Studio.
Also, don't make a CLR form a child of MFC window. Instead, make a CLR control (some panel, typically) a child of MFC window. A panel also has HWND, so you can use it.

And again, you are getting in extra difficulties while you could make a .NET UI much faster, with much better quality, better maintenance...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.151126.1 | Last Updated 17 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100