Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
Hi
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.

C++
KTCLogic::BaseForm ^baseform = gcnew KTCLogic::BaseForm();
baseform->Show();
Posted
Updated 16-Apr-13 3:19am
v3

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: http://msdn.microsoft.com/en-us/library/windows/desktop/ms633541%28v=vs.85%29.aspx[^].

You can get the form's handle as http://msdn.microsoft.com/en-us/library/system.windows.forms.control.handle.aspx[^].

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

—SA
 
Share this answer
 
Comments
Aydin Homay 17-Apr-13 0:00am    
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.
Sergey Alexandrovich Kryukov 17-Apr-13 0:06am    
Can't you see that I answered in full?
—SA
Aydin Homay 17-Apr-13 0:16am    
I see, I will test it as soon as possible so Thank you. :)
Sergey Alexandrovich Kryukov 17-Apr-13 0:21am    
Sure.
Don't forget to accept the answer formally (green button).
In all cases, your follow-up questions will be welcome.
—SA
Aydin Homay 17-Apr-13 0:44am    
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);
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:
http://en.wikipedia.org/wiki/Multiple_document_interface#Disadvantages[^],
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.

—SA
 
Share this answer
 
Comments
Aydin Homay 17-Apr-13 1:42am    
So what is your solution if we want opening our .NET forms from MFC main window?
Sergey Alexandrovich Kryukov 17-Apr-13 1:47am    
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...
—SA

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900