COM Component Development Practice (i)

Source: Internet
Author: User
Tags assert insert resource

Preface

Because the project needs, began to engage in ActiveX work, read some information, unfortunately are some of the COM principle, there is no practical hands-on things, on the CodeProject read David Marcionek article "1" after a lot of harvest, But there are also some annoying little problems, so on the basis of some of the easy wrong points to do small notes. This copyright belongs to David Marcionek.

Brief introduction

In this article, we'll create an ActiveX control that, when the control is loaded, displays an animated progress bar to show the user that the control is loading. This control contains features that show how to pass information between controls and Web pages. Below we will use VS2005 step-by-step development.

Create an ActiveX control

To create an ActiveX control, as follows:

1, create an "MFC ActiveX control" project, named Myactivex,

2, in the MFC ActiveX Control Wizard dialog box, select Control Settings

3, in Create control based, select Static. We will use static controls, because we simply display the output information obtained from the control and do not accept input information.

4, in the "Additional features", to ensure that "activates when visible" and "Flicker-free Activation" was selected, "Has an about Box dialog" is not selected.

5, By default, Wizard creates an item that uses MFC in a shared DLL. We have to change this situation because ActiveX controls cannot run unless the required MFC DLLs are already installed on the system. This is one reason why a red fork appears on a Web page that contains ActiveX controls. In the properties of the project, "Configuration Properties"--> "General" to replace "use of MFC" with "The use MFC in a Static Library".

6, the wizard creates the following classes:

1) Cmyactivexapp: This is an ActiveX application class inherited from the COleControlModule class. It is the base class that the OLE control module object inherits from, containing the initialization (InitInstance) and Cleanup (exitinstance) code

2) Cmyactivexctrl: Inherited from COleControl, this is where we implement most of the control's functions.

3) Cmyactivexproppage: Inherits from COlePropertyPage, for managing the control's Property Page dialog box. The wizard has created a default dialog box for us to act as the control's property Page dialog box.

Add animated GIF Support

Here we use a Cpictureex class (specific code see the last "Resources" section), vs2005 add an animated GIF resource has a bug (in fact also exists in vs2008), we can use the following technique to avoid it:

Copy the processingprogressbar.gif to the project folder and rename it to Processingprogressbar.gaf, and in the resource view, right-click the resource file myactivex.rc and select Add Resource. In the Add Resource dialog box, press the Import button and select the Processingprogressbar.gaf file. In the Custom Resource Type dialog box, enter "GIF" as the resource type. This will import the GIF picture file into your project. Then change the imported picture ID from IDR_GIF1 to Idr_progressbar ...

Now start to restore the status quo, first, open the source file of the myactivex.rc, find the definition of Idr_progressbar, and change its file name to "Processingprogressbar.gif". Similarly, the picture file name under the project folder is changed back to Processingprogressbar.gif, and finally, in Solution Explorer view, select Processingprogressbar.gaf, and in its properties, modify the relative path to. Processingprogressbar.gif ".

Add dialog box

Now we add a dialog box for the progress bar image.

1, in the resources view, right-click the dialog box and choose Insert dialog to create a default dialog box.

2, delete the default generated "OK" and "Cancel" button, adjust the dialog box size to 230*40.

3, change the dialog box ID to Idd_maindialog and modify the dialog box properties: Border-none, Style–child, System Menu–false, Visible–true.

4, add a picture control in the dialog box, adjust its size to 200*20, change the control ID to Idc_progressbar, and the color is "white".

5, create a class for the dialog box named Cmaindialog,

Now we add the member variables for the class:

1, add a variable m_maindialog for the Cmyactivexctrl class, the type is Cmaindialog

2, add a variable M_progressbar for the Cmaindialog class, the type is Cpictureex, note here that the control variable is selected and the control for is "Idc_progressbar".

Add support code

OK, now add some code to draw the main dialog box and the progress bar control.

1, for Cmyactivexctrl to handle the code of the Wm_create event, in which to add:

M_maindialog.create (Idd_maindialog,this);

and add in the OnDraw function:

m_MainDialog.MoveWindow(rcBounds,TRUE);
CBrushbrBackGnd(TranslateColor(AmbientBackColor()));
pdc->FillRect(rcBounds,&brBackGnd);

2. In the Cmaindialog class, add the code to handle the Wm_create event, where you add:

if(m_ProgressBar.Load(MAKEINTRESOURCE(IDR_PROGRESSBAR),_T("GIF")))
m_ProgressBar.Draw();

Ok, a simple ActiveX control has been developed, set the compilation mode to "release" mode, and build the entire application.

Create a Web page as an ActiveX control container

You can use Microsoft's ActiveX control Pad. To use it to insert an ActiveX control into a Web page, right-click in the <BODY> tag, select Insert ActiveX controls, and select the one you want.

Open the Web page directly or put it on the IIS server for access, and when all goes well you can see the following image:

Note 1: The previous request to set the compilation mode to "release", in fact, in order to avoid the runtime because of the failure to touch assert to do, otherwise the error will be as follows:

After debugging, you will find that:

You can see that there are problems with the order in which the picture extension controls are loaded, but you don't need to think so much in the browser, so ignore this assert condition here.

Note 2: The author does not have to explain the myactivex.idl document here, I think is a not small mistake, and that's why it leads to a very easy place to make mistakes, and when we follow his tutorial and go through his code one step at a time, we find that we can run through the ActiveX Test Container, but to the browser But life and death are all red fork ... , because the author ignores its interpretation of the modification of the Myactivex.idl interface definition file.

Note There is no ActiveX Control Test Container in 3:vs2008, VS05 Digital signature tool is also changed, so using VS2005 may be better

I followed the tutorial step-by-step to imitate the time, in the above these two points entangled for 3 hours before discovering the cause of the problem.

In the next article, you will learn how to digitally sign ActiveX controls and make them self-signed and destroyed to ensure their security, as well as how to data communication between ActiveX controls and Web pages.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.