Help with collection
Masanori Iwasa -- masa@aoistp.stpn.soft.net Tuesday, December 24, 1996 Environment: Windows95, Visual C++ 4.2-flat Hi there, I'm trying to write a collection in my OCX called columns, and looking at other OCX's, they seem to be implementing the following properties and methods. Add, Remove, Clear, Item, Count, _NewEnum. Quesions : A) In order to get this collection to work, do I need to use CMap etc. ? Looking at the TabStrip OCX, it seems like an object in the collection takes a key and an index. B) Should I derive a class from CCmdTarget, or should I implement an Interface directly in my OCX ? Thanks in advance. Masa
P.J. Tezza -- pj@exemplarsoftware.com Tuesday, December 31, 1996 Dear Masanori, OLE Automation Collection objects are normal OLE Automation objects. OLE = Automation objects can be created by deriving a class from CCmdTarget = and adding OLE Automation properties and methods with ClassWizard. You = can also implement IDispatch yourself or use some other tool like the = ODL compiler. As you have noticed, OLE Automation collection objects = follow a standard for methods names, parameters and return values. This = standard is described in Visual C++ Books Online in the OLE Automation = documentation in the section "All Collection Objects". In order to = follow this standard, you will have to learn quite alot about OLE = Automation and implement the standard methods and properties yourself. = If you are still interested and have a good grasp of OLE Automation = principles, I can give you more pointers to code and classes you can use = to implement your collection objects. PJ pj@exemplarsoftware.com
P.J. Tezza -- pj@exemplarsoftware.com Tuesday, December 31, 1996 By popular request (actually, on one person's request), here is my FAQ = style answer to the question "How do you create OLE Automation = Collections with MFC". The answer I give is based on my experience. Lots = of improvements can probably be made, and suggestions are welcome. Introduction OLE Automation collection objects are normal OLE Automation objects. OLE = Automation objects can be created by deriving a class from CCmdTarget = and adding OLE Automation properties and methods with ClassWizard. You = can also implement IDispatch yourself or use some other tool like the = ODL compiler.=20 OLE Automation collection objects follow a standard for methods names, = parameters and return values. This standard is described in Visual C++ = Books Online in the OLE Automation documentation in the section "All = Collection Objects". In order to follow this standard, you will have to = learn quite a lot about OLE Automation and implement the standard = methods and properties yourself. The recommended study course is to read = the Win32 OLE Automation documentation, followed by the sections on OLE = Automation in the MFC documentation. Finish your OLE Automation studies = with the MSDN OLE Automation technical articles which are currently = located under Technical Articles->Windows Articles->OLE Articles->OLE = Automation. With regards to collections, the most important MSDN = technical articles are "Managing Object Lifetimes in OLE Automation" and = "Implementing OLE Automation Collections". Creating OLE Automation Collection Objects The OLE Automation collection object is an OLE Automation object in its = own right. Using ClassWizard (and/or the appropriate registration = database entries), you can make collection objects "Createable by type = ID" (Otherwise known as "OLE createable" and "OLE creatable" in the MFC = docs. Neither "createable" nor "creatable" are in my dictionary, so I = don't know which one to choose.), but, more often, the collection object = will be a property of some other object. Examples are the Documents = property of an Application object or the Cells property of a Grid = ActiveX (OCX) object.=20 The collection object can be created with ClassWizard by inserting a new = class derived from CCmdTarget and checking or clearing "Createable by = type ID" as appropriate. After you have inserted your collection class, = you will need to add the standard collection properties and methods = which make sense for your collection. You can use the ClassWizard to = insert the appropriate functions, but you will have to code the = implementations yourself. As described in the "Implementing OLE = Automation Collections" technical article, the Item property and = _NewEnum method require special handing. The easiest way to create these = methods is to use ClassWizard normally, then edit the ClassWizard = generated macros by hand to use the DISP_DEFVALUE and = DISP_PROPERTY_EX_ID macros as appropriate. If you are maintaining an ODL = file, you will need to edit it by hand as well. Do not forget to move = all code hand edited outside the ClassWizard comments, or you will = confuse ClassWizard. Design As discussed, in "Implementing OLE Automation Collections", you must = decide if you are using "real collection objects" or "pseudo collection = objects". You will also have to decide if the objects being enumerated = are "real" or "pseudo" and if they are OLE createable. Visual Basic can create type-safe objects using the Dim and New = keywords. For example, the code "Dim MyWord As New Word" creates MyWord = as a Word type object. In the past, however, the code showed in = "Implementing OLE Automation Collections" required Visual Basic users to = use the generic (and slower) Object type. When using the Object type, = type errors like mistyping member names, can only be detected at run = time.=20 For my own applications, I created type-safe collections. The main = reason for this was to increase type safety and performance in my = application's proprietary macro language, but Visual Basic programmers = benefit as well. In order to do this, all non-OLE createable OLE = Automation objects must be "pseudo objects" and OLE createable. Each of = these objects has two states: attached and unattached. An unattached = object is created in Visual Basic using Dim and New. Any access to an = unattached object's properties or methods causes a runtime error. An = unattached object can be attached to a valid object instance in Visual = Basic using Set. For example the code "Set rgdocs =3D app.Documents" = would attach the rgdocs collection object to the app.Documents = collection instance. Attached objects can be used normally. Another delicate issue to consider is multiple access paths. Consider = what will happen to your collections and the collected objects when = objects are inserted, updated and deleted with your application's (or = ActiveX object's) UI and vice versa. What if there are multiple = automation clients? I chose to use "pseudo objects" and optimistic = concurrency protocols to deal with these situations in my applications, = but this may not be an appropriate solution for everyone. Implementation The technical article "Implementing OLE Automation Collections" shows = the basics. You can also grunge around in the MFC source files for = source code. In particular, the CEnumArray MFC implementation class can = scavenged to implement IEnumVARIANT, and the OLE interface macros can be = used. In order the get things running as quickly as possible, you do not have = to implement all the standard collection members to build your = application and test it. I used this strategy and never got the time to = implement the _NewEnum member for some of my collections. The = application was released, and none of the many OLE Automation = programmers have complained yet. As users become familiar with OLE = Automation standards and techniques, I expect my users to become more = demanding. PJ pj@exemplarsoftware.com ---------- From: Razavipour, Reza Sent: Tuesday, December 31, 1996 9:44 AM To: 'pj@exemplarsoftware.com' Subject: RE: Help with collection I am definitely interested in your pointers and example code... Reza Razavipour Biles & Associates reza_r@need.biles.com >---------- >From: P.J. Tezza[SMTP:pj@exemplarsoftware.com] >Sent: Monday, December 30, 1996 11:36 PM >To: 'mfc-l@netcom.com' >Subject: RE: Help with collection > >.... >If you are still interested and have a good grasp of OLE Automation =3D >principles, I can give you more pointers to code and classes you can = use =3D >to implement your collection objects. > >PJ >pj@exemplarsoftware.com > >
Masanori Iwasa -- masa@aoistp.stpn.soft.net Thursday, January 02, 1997 Hi PJ, Yes, I sure would appreciate if you can give me some pointers to some sample or documents. Also, a question : In all the objects I derive from CCmdTarget, do I need to Enable OLE automation ? Masa >---------- >From: P.J. Tezza[SMTP:pj@exemplarsoftware.com] >Sent: Tuesday, December 31, 1996 11:06 AM >To: 'mfc-l@netcom.com' >Subject: RE: Help with collection > >Dear Masanori, > >OLE Automation Collection objects are normal OLE Automation objects. >OLE = >Automation objects can be created by deriving a class from CCmdTarget = >and adding OLE Automation properties and methods with ClassWizard. You >= >can also implement IDispatch yourself or use some other tool like the = >ODL compiler. As you have noticed, OLE Automation collection objects = >follow a standard for methods names, parameters and return values. This >= >standard is described in Visual C++ Books Online in the OLE Automation >= >documentation in the section "All Collection Objects". In order to = >follow this standard, you will have to learn quite alot about OLE = >Automation and implement the standard methods and properties yourself. >= >If you are still interested and have a good grasp of OLE Automation = >principles, I can give you more pointers to code and classes you can >use = >to implement your collection objects. > >PJ >pj@exemplarsoftware.com > >
| Вернуться в корень Архива |