Example: Controls in the .NET in the MFC program

xiaoxiao2021-03-06  15

Starting with .NET Framework 1.1, .NET control can be integrated into the non-hosting host in an ActiveX - but the official support is only for MFC programs that use managed C . Chris Sells describes such an example in MSDN magazine in March 2003 (http://msdn.microsoft.com/msdnmag/issuss/03/03/windowsforms/default.aspx). The code used by this example is slightly cumbersome and does not describe how to handle controls. MFC 8.0 adds a series of support to simplify this integration process (refer to http://msdn2.microsoft.com/library/ahdd1h97.aspx). This makes some more useful classes in the .NET in the MFC program, such as System :: Windows :: Forms :: PropertyGrid than before.

For example, you want to use the System :: Windows :: Forms :: PropertyGrid control in the MFC-based dialog box, first create a dialog-based program to add the necessary references:

#include // MFC Windows Forms Support # using #using #using #using # using using namespace System; using namespace System :: Drawing; using namespace System :: Windows :: Forms; using namespace System :: ComponentModel; using namespace Microsoft :: VisualC :: MFC; using namespace stdcli :: language ;

Then add code (below this class is borrowed from the PropertyGrid control of the .NET framework from the MSDN, and more advanced use methods for this control can also refer to this article).

Public ref class appsettings {public: [Description), Category, DefaultValue (false)] Property Boolean Saveonclose; [Description ", Category, Category, Category (" Global Settings ", Readonly (True), DefaultValue ("Welcome to Applications!")] Property String ^ GreetingText; [Category ", defaultValue (4)] Property int32 itemsinMRU; [Description (" Text Repetition in milliseconds Category ("global settings"), defaultValue (10)] Property int32 maxrepetrate; [Browsable (false), default (false)] Property Boolean SettingSchanged; [Category), DefaultValue ("1.0") , Readonly (TRUE)] Property String ^ AppVersion; Appsettings () {this-> saveonclose = true; this-> GreetingText = GCNEW STRING ("Welcome Application!"); This-> maxrepetrate = 10; this-> itemsinMRU = 4; this-> settingschanged = false; this-> appversion = gcnew string ("1.0");}}; Class CpropertyGridtestdlg: Public CDIALOG

{

/ / In order to be lazy, the default code to be generated is omitted.

CwinFormsControl M_WndPropertyGrid;

BEGIN_DELEGATE_MAP (CPropertyGridTestDlg) EVENT_DELEGATE_ENTRY (PropertyValueChanged, Object, PropertyValueChangedEventArgs) EVENT_DELEGATE_ENTRY (HandleDestroyed, Object, EventArgs) END_DELEGATE_MAP () public: void PropertyValueChanged (Object ^ sender, PropertyValueChangedEventArgs ^ e) {TRACE ( "PropertyValueChanged% S / n", marshal_as (e-> toString ()));} void handledestroyed (Object ^ sender, Eventargs ^ e) {trace ("PropertyValueChanged% S / N", Marshal_as (e-> toString ()));}} Bool CPropertyGridtestdlg :: OnInitdialog () {

/ / In order to be lazy, the default code generated by the guide is omitted again.

// TODO: add this extra initialization code CRect rect; GetDlgItem (IDC_PLACEHOLDER) -> GetWindowRect (rect); // IDC_PLACEHOLDER is used to occupy the place of a Static control GetDlgItem (IDC_PLACEHOLDER) -> DestroyWindow (); ScreenToClient (rect ); m_wndPropertyGrid.CreateManagedControl (WS_VISIBLE | WS_CHILD, rect, this, IDC_PLACEHOLDER); System :: Windows :: Forms :: PropertyGrid ^ pGrid = m_wndPropertyGrid.GetControl (); appSettings ^ appSettings = gcnew appSettings; pGrid-> SelectedObject = appSettings; pGrid-> PropertyValueChanged = MAKE_DELEGATE (PropertyValueChangedEventHandler, PropertyValueChanged); pGrid-> HandleDestroyed = MAKE_DELEGATE (System :: EventHandler, HandleDestroyed);

}

Use this feature in the CTP in VC2005 in February, there are some small questions: There are several warnings when the output window is started: there is an Assert window that can be simply ignored. There is a reason unknown memory leak when you quit.

Exterior: Although I am sure that Marshal_as This function is available in April 2004 (at the Global MVP summit in April 2004, I have seen the reference to this function in Visual C 2005, but so far I haven't found this guy in which header file or namespace. In order to be lazy, I have to write a template function yourself.

Template Interior_Ptr Marshal_as (String ^ s) {Interior_Ptr txt = ptrtostringchars (s); return interface_ptr (txt);} This template function is also in Microsoft's MSDN is also The language is unknown, perhaps this function is not yet enabled.

转载请注明原文地址:https://www.9cbs.com/read-45599.html

New Post(0)