Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++/CLI MFC .NET4
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.
 
KTCLogic::BaseForm ^baseform = gcnew KTCLogic::BaseForm();
baseform->Show();
Posted 16-Apr-13 4:13am
Edited 16-Apr-13 4:19am
v3
Rate this: bad
good
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: 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
  Permalink  
Comments
AYDIN EBRAHIMI HOMAY at 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 at 17-Apr-13 0:06am
   
Can't you see that I answered in full?
—SA
AYDIN EBRAHIMI HOMAY at 17-Apr-13 0:16am
   
I see, I will test it as soon as possible so Thank you. :)
Sergey Alexandrovich Kryukov at 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 EBRAHIMI HOMAY at 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);
Sergey Alexandrovich Kryukov at 17-Apr-13 0:51am
   
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...
—SA
AYDIN EBRAHIMI HOMAY at 17-Apr-13 1:07am
   
I don`t understand how to tell my windows form :
 
in MFC :
 
KTCLogic::BaseForm ^baseform = gcnew KTCLogic::BaseForm();
baseform->MdiParent= CManiForm // For example
baseform->Show();
 
I have a problem with baseform->MdiParent= CManiForm // For example !?
Sergey Alexandrovich Kryukov at 17-Apr-13 1:14am
   
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...
—SA
Sergey Alexandrovich Kryukov at 17-Apr-13 1:18am
   
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.
—SA
AYDIN EBRAHIMI HOMAY at 22-Apr-13 0:51am
   
Hi
 
I find out this solution ;)
 
http://msdn.microsoft.com/en-us/library/ahdd1h97%28v=vs.80%29.aspx
http://www.microsoft.com/en-us/download/details.aspx?id=2113
http://msdn.microsoft.com/en-us/library/92y4h944(v=vs.80).aspx
Sergey Alexandrovich Kryukov at 22-Apr-13 0:55am
   
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.
—SA
AYDIN EBRAHIMI HOMAY at 22-Apr-13 1:00am
   
Sergey actually I could not understand other solution :P ;)
Sergey Alexandrovich Kryukov at 22-Apr-13 1:21am
   
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.
—SA
AYDIN EBRAHIMI HOMAY at 22-Apr-13 1:40am
   
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";
theApp.m_LogicTemplate->OpenDocumentFile(logicname,0,1);
FindWindowByTitle fwbt("logic1", true);
 
HWND hwnd1 ;
hwnd1 = fwbt.FindIt();

KTCLogic::MDIParentMain ^logicmain = gcnew KTCLogic::MDIParentMain();
logicmain->TopMost = true;
logicmain->Show();
 
FindWindowByTitle fwbt1("KTC-Logic Editor", true);
HWND hwnd2 ;
hwnd2 = fwbt1.FindIt();

::SetParent(hwnd2,hwnd1);
 
So, do you have a better solution? of course I have problem in hosting my windows form in MFC Dialog Box still yet :)
Sergey Alexandrovich Kryukov at 22-Apr-13 2:14am
   
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.
 
—SA
AYDIN EBRAHIMI HOMAY at 22-Apr-13 4:12am
   
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 at 22-Apr-13 11:16am
   
I'm talking not in mixed project, which is fine, but about mixing MFC with System.Windows.Forms.
—SA
Rate this: bad
good
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:
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
  Permalink  
Comments
AYDIN EBRAHIMI HOMAY at 17-Apr-13 1:42am
   
So what is your solution if we want opening our .NET forms from MFC main window?
Sergey Alexandrovich Kryukov at 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)



Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 17 Apr 2013
Copyright © CodeProject, 1999-2014
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