ActiveX COM component development 1

Source: Internet
Author: User
Tags vc runtime

COM component development can use vb c # VC ++ and other individuals to be familiar with C #, but C # controls have a huge disadvantage, requiring customers to install the framework, which is quite difficult, of course, VB also needs to install the Runtime Library. VC ++ can be installed or not installed. It depends on your compilation. In other words, the VC Runtime Library is relatively lightweight and acceptable. So I plan to spend some time organizing the basic knowledge of VC ++'s com development, from the creation of a recent ticket to the implementation of various interfaces such as complex application, and the basics. Use of OLE object methods.

VC ++ development is divided into two frameworks: MFC and ATL. This article will summarize them one by one, starting with the most familiar MFC.

1. Create an ActiveX Control

2. Add attributes, methods, and events

1) Add attribute: Switch to Class View

 

Right-click _ dactivextest and the following menu is displayed.

Move the mouse to the "add" menu to bring up the Add attribute adding method. Click Add attribute to enter the attribute type and name, and click Finish.

At this time, the attribute is set, but no value is assigned yet.

Add a variable to the activextestctrl. h file and the header file is as follows:

# Pragma once // activextestctrl. h: cactivextestctrl ActiveX control class declaration. // Cactivextestctrl: for implementation information, see activextestctrl. cpp. Class cactivextestctrl: Public colecontrol {declare_dyncreate (cactivextestctrl) // constructor public: cactivextestctrl (); // rewrite public: Virtual void ondraw (CDC * PDC, const crect & rcbounds, const crect & rcinvalid); Virtual void dopropexchange (cpropexchange * ppx); Virtual void onresetstate (); // implement protected :~ Cactivextestctrl (); declare_olecreate_ex (cactivextestctrl) // class factory and export (cactivextestctrl) // Export (cactivextestctrl) // iddeclare_olectltype (cactivextestctrl) on the property page) // type name and miscellaneous status // message ing declare_message_map () // scheduling ing declare_dispatch_map () // event ing declare_event_map () // scheduling and event idpublic: enum {dispidtest = 1}; protected: BSTR gettest (); void settest (lpctstr newval); Public: // This is the variable added by yourself: javascstring m_test ;};

There are two more methods in the activextestctrl. cpp file: gettest () and settest () to assign values to the attribute.

Modify the following three functions respectively.

Void cactivextestctrl: dopropexchange (cpropexchange * ppx) {exchangeversion (ppx, makelong (_ wverminor, _ partition); colecontrol: dopropexchange (ppx); px_string (ppx, _ T ("test"), m_test); // todo: Call the PX _ function for each persistent custom property .}

BSTR cactivextestctrl: gettest () {afx_manage_state (afxgetstaticmodulestate (); cstring strresult; // todo: add the dispatch handler code strresult = m_test; return strresult. allocsysstring ();}


Void cactivextestctrl: settest (lpctstr newval) {afx_manage_state (locate (); // todo: add the property handler code m_test = newval; invalidatecontrol (); setmodifiedflag ();}


In this way, an attribute is added.

2) add method click Add method pop-up add method wizard input return type void method name function complete

Activextestcrtl. cpp has an additional method. This method is the newly defined method,

 

Void cactivextestctrl: function () {afx_manage_state (afxgetstaticmodulestate (); MessageBox (_ T ("this method is called"); // todo: add the dispatch handler code here}

3) adding Event Events is divided into two types: Control Interface events, such as click events, and internal events.

Switch to the Class View and find the acctivextestcrtl class. Right-click the Add event and select click.


In the CPP file of this class, you only need to execute the required method in this event.


According to the above process, a simple control is created, but there is no complicated interface. simply draw a graphic interface with a circle or delete an ondraw without a graphic control, the following document creates a control with a window.



Zookeeper

ActiveX COM component development 1

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.