using the property grid control from a native app

D

Dave Calkins

I have a native Win32 C++ app built with Visual Studio 2005. I'd like to
make use of a property grid control in this app. For an example of this, in
Visual Studio, see the properties control (select something in a dialog from
the dialog editor or a class from the class list and hit F4).

I used Spy++, which revealed that the window class of the property grid
control used by Visual Studio is "WindowsForms10.Window.8.app.0.378734a".
So, I'm assuming this is a .NET control.

I found the System.Windows.Forms.PropertyGrid class
(http://msdn.microsoft.com/library/d...fsystemwindowsformspropertygridclasstopic.asp)
in the MSDN library, which seems to be the correct control to do the job.

Whats the easiest way to go about using this .NET control in my native C++
app? I'm assuming I can do this using mixed-mode, etc., but was hoping for
a pointer in the right direction wrt how to go about doing this.

I understand that this will force a dependency on having the .NET runtime
installed and I'm ok with that.

Thanks,
Dave
 
D

Dave Calkins

I found a couple articles on the Code Project web site (www.codeproject.com)
which address the below and have included my notes taken from those articles
below.

====
The basics for using a .NET Windows Forms control in a native MFC app are as
follows:

1. Enable /clr for the project
2. Add #include <afxwinforms.h> to your stdafx.h
3. Add a static control to your dialog, positioned where you want the
control
4. Declare a control variable in the dialog class:
CWinFormsControl<System::Windows::Forms::propertyGridControl> m_props;
5. In your dialog's DoDataExchange() method, add:
DDX_ManagedControl(pDX,IDC_PROPS,m_props);
6. now you can use the control variable just as any other native control
7. there are other steps for handling events, etc.

Details are in the below Code Project articles

http://www.codeproject.com/managedcpp/mfcdialogwinforms.asp
http://www.codeproject.com/managedcpp/MfcWinFormsOff.asp

====

Dave
 
G

Gary Chang[MSFT]

Hi David,

Thanks for your valuable summary and suggested articles!

I also found some good articles on this topic, I think you may have
interests on them:

Integrate Windows Forms Into Your MFC Applications Through C++ Interop
http://www.msdn.net/netframework/windowsforms/default.aspx?pull=/msdnmag/iss
ues/06/05/mixandmatch/default.aspx

MFC 8.0 and Windows Forms Integration, Part I
http://www.codeguru.com/cpp/cpp/cpp_mfc/general/article.php/c10883/

MFC 8.0 and Windows Forms Integration, Part II
http://www.codeguru.com/cpp/cpp/cpp_mfc/general/article.php/c11083/

Have a nice weekend!

Best regards,

Gary Chang
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
D

Dave Calkins

The prior message covers flipping the /clr flag for the entire project. The
other option is to just flip the /clr flag for the source file which uses
the . NET control. Some info for doing this is shown below as well.

[ enable /clr for just the source file using the controls ]
- similar to doing it for the whole project, but with the following
differences
- move the #include <afxwinforms.h> from stdafx.h into the source file
where the control is being used
- leave /clr off for the project, but turn it on for the source file
- set exception handling to /EHa for the source file
- disable use of pre-compiled headers for the source file (since the
non-clr precompiled header won't be compatible with the clr source file)
- create a forward-declared inner class in the dialog class using the
control; this class is the control holder
- declare the control holder class in the source file, having it contain
the template instantiation (CWinFormsControl) class
- to use the control just refer to m_controlHolder.m_myControl

The holder class is necessary because you don't want to include the
afxwinforms header in the dialog's header or you'll pollute the rest of the
project.

Dave
 
D

Dave Calkins

Gary,

Those look like excellent articles. Thanks and have a good weekend yourself
:)

Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top