Using Custom Controls in a Managed C Application

   

Using Custom Controls in a Managed C++ Application

In order to test the control you just created, you will have to create an application that uses the control. Select New, Project from the File menu. Select Visual C++ Projects from the list of project types and select the Managed C++ Application template from the list of available templates. In order to prevent having to switch back and forth between the two projects, make sure the radio button labeled Add to Solution is selected. Give your project the name CPPTestHarness and click OK to create the project.

The first thing you should do is set some of your project settings so that the two projects can work together. First, change the output path for the ShapeControl project so that the final binary file is placed into a bin directory regardless of whether it is built in debug or release mode. To do this, right-click the project name in Solution Explorer and select Properties. Select All Configurations from the Configurations drop-down box in the Property Pages dialog. Next, change the Output Directory property to point to the bin directory, as shown in Figure 23.1. Click OK to save the changes. The next step is to specify the build order of the two projects. Because the test harness project depends on the DLL of the control project to be present before it can be built, click Project, Project Dependencies on the main menu. In the Project Dependencies dialog, select the CPPTestHarness project in the Project drop-down list and then check the ShapeControl project in the Depends On list box, as shown in Figure 23.2. Click OK to make the changes. Finally, in order for the IDE to launch the test harness executable rather than trying to launch the control DLL when you debug your project, right-click the CPPTestHarness project and click on Set as StartUp Project.

Figure 23.1. Changing the output directory to place binary files in a common location.

graphics/23fig01.jpg

Figure 23.2. Specifying build dependencies to ensure proper project build order.

graphics/23fig02.jpg

The test harness itself will consist of a single Windows Form and will contain the control you created earlier. Create a new Windows Form class named CPPTestForm. Create a function named InitForm and call that function within the default constructor. The InitForm function is a private member function that accepts no parameters and returns void. You are now ready to create the custom control and place it on the form.

In order to use the control, you must first import the assembly and reference the namespace you wish to use, which in this case is ShapeControl. As explained at the beginning of the hour, instantiating a custom control is similar to creating any other type of Windows Form control. Therefore, create a private member variable that is a pointer to a ShapeControl class, as shown on line 26 of Listing 23.2. The next step is to instantiate a new instance of the ShapeControl class within the InitForm function. The remaining steps simply involve setting the Location property of the control and setting the various other properties of the main form itself, as you have done in previous hours. Finally, ensure that you launch the form by calling Application::Run within your _tmain application entry point.

Listing 23.2 Testing ShapeControl with a Test Harness
 1: #include "stdafx.h"  2:  3: #using <mscorlib.dll>  4: #using <System.dll>  5: #using <System.Drawing.dll>  6: #using <System.Windows.Forms.dll>  7: #using <../ShapeControl/bin/ShapeControl.dll>  8:  9: #include <tchar.h> 10: 11: using namespace System; 12: using namespace System; 13: using namespace System::Drawing; 14: using namespace System::Windows::Forms; 15: using namespace ShapeControl; 16: 17: __gc class CPPTestForm : public Form 18: { 19:    public: 20:         CPPTestForm() 21:         { 22:             InitForm(); 23:         } 24: 25:     private: 26:         ShapeControl* m_pShapeControl; 27: 28:         void InitForm() 29:         { 30:             m_pShapeControl = new ShapeControl(); 31:             SuspendLayout(); 32: 33:             m_pShapeControl->Location = System::Drawing::Point(10, 10); 34:             m_pShapeControl->Name = "ShapeControl"; 35:             // 36:             // Form1 37:             // 38:             AutoScaleBaseSize = System::Drawing::Size(5, 13); 39:             ClientSize = System::Drawing::Size(292, 266); 40:             Control* pControls? = { m_pShapeControl }; 41:             Controls->AddRange( pControls ); 42:             Name = "CPPTestForm"; 43:             Text = "CPPTestForm"; 44:             ResumeLayout(false); 45:         } 46: }; 47: 48: // This is the entry point for this application 49: int _tmain(void) 50: { 51:     Application::Run( new CPPTestForm ); 52:     return 0; 53: } 

Compile your project and launch the application by clicking Debug, Start Without Debugging from the main menu. At this point, you probably think I have been lying to you because I told you that using a custom control is the same as using any other Window Form control. However, when you launched the test harness you received a System.IO.FileNotFoundException, as shown in Figure 23.3. This is due to the fact that there are a few special procedures you must perform on your control assembly before it can be used.

Figure 23.3. Custom controls cannot be used without first some assembly attributes being changed.

graphics/23fig03.jpg


   
Top


Sams Teach Yourself Visual C++. NET in 24 Hours
Sams Teach Yourself Visual C++.NET in 24 Hours
ISBN: 0672323230
EAN: 2147483647
Year: 2002
Pages: 237

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net