转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> Delphi程序 >> 正文
Building ActiveX Controls with Delphi 3         ★★★★

Building ActiveX Controls with Delphi 3

作者:闵涛 文章来源:闵涛的学习笔记 点击数:5174 更新时间:2009/4/23 18:31:11
ainer can define whatever ambient properties it wants to expose. ActiveX defines a standard set of ambient properties, which includes: BackColor, DisplayName, and others. A container is not required to provide any or all of these properties, but if it does, Microsoft defines which dispids to use for each.

ActiveX allows you to access ambient properties through the site''''s IDispatch interface. Delphi provides a dispinterface, IAmbientDispatch, which can be used to access the standard interfaces. Since it''''s a dispinterface, it''''s really just an IDispatch pointer and can be cast to any other dispinterface. If you''''re interested in querying the container for a nonstandard ambient property you''''ll need to define a new dispinterface that defines the property and its dispid, then cast FAmbientDispatch to the new dispinterface. Here''''s the declaration of IAmbientDispatch:

IAmbientDispatch = dispinterface
  [''''{00020400-0000-0000-C000-000000000046}''''] 
  property BackColor: Integer dispid DISPID_AMBIENT_BACKCOLOR; 
  property DisplayName: WideString dispid DISPID_AMBIENT_DISPLAYNAME;
  property Font: IFontDisp dispid DISPID_AMBIENT_FONT;
  property ForeColor: Integer dispid DISPID_AMBIENT_FORECOLOR;
  property LocaleID: Integer dispid DISPID_AMBIENT_LOCALEID;
  property MessageReflect: WordBool dispid DISPID_AMBIENT_MESSAGEREFLECT;
  property ScaleUnits: WideString dispid DISPID_AMBIENT_SCALEUNITS;
  property TextAlign: Smallint dispid DISPID_AMBIENT_TEXTALIGN;
  property UserMode: WordBool dispid DISPID_AMBIENT_USERMODE;
  property UIDead: WordBool dispid DISPID_AMBIENT_UIDEAD;
  property ShowGrabHandles: WordBool dispid DISPID_AMBIENT_SHOWGRABHANDLES;
  property ShowHatching: WordBool dispid DISPID_AMBIENT_SHOWHATCHING;
  property DisplayAsDefault: WordBool dispid DISPID_AMBIENT_DISPLAYASDEFAULT;
  property SupportsMnemonics: WordBool dispid DISPID_AMBIENT_SUPPORTSMNEMONICS;
  property AutoClip: WordBool dispid DISPID_AMBIENT_AUTOCLIP;
end;

Example: The following code responds to the button click, and sets the button''''s caption to the DisplayName ambient property. The DisplayName property is usually the control''''s name in its container.

procedure TButtonX.Click;
var 
  Site: IOleClientSite;
  Ambients: IDispatch;
begin
  GetClientSite( Site );
  if Site <> nil then
    Site.QueryInterface(IDispatch, Ambients);
  if Ambients <> nil then
  begin
    Caption := IAmbientDispatch(Ambients).DisplayName;
  end;
end;
Tracking Changes to Ambient Properties

When the container changes the value of one of its ambient properties, it informs the control by calling the object''''s OnAmbientPropertyChange method. In Delphi, you can implement your own handler for this method by overriding the method and re-implementing the IOleControl interface in your class.



Bug: Ambient Confusion

As if the ActiveX specification wasn''''t confusing enough, Microsoft has been confused about how to implement ambient properties. Certain MS containers, such as Access 97, incorrectly assume the IDispatch interface it provides for ambient properties can be the same as the event sink. To make matters worse, some versions of MFC assume they must be the same IDispatch pointers. The lesson here is that over the years ActiveX has become enough of an architectural mess that nobody can ensure-or for that matter define-complete compliance. What this means for you as an ActiveX control developer is that you need to be diligent about testing your control in a variety of containers, and be prepared to encounter some strange and unexpected behaviors. Even Microsoft has published controls that work in Internet Explorer but not in other containers, and each container has different reactions to the control''''s incompatibility.

Adding Custom Registry Entries

You may want to add new registry entries for your control. For example, Microsoft recently published a specification, called component categories, that involves adding registry entries under the Component Categories key. DAX provides a means of adding registry entries in the factory class.

Every Delphi COM factory provides a virtual UpdateRegistry method that gets called when the library is registered or unregistered. If you want to add new items to the registry when the library is registered, derive a new factory class from TActiveXControlFactory and override its UpdateRegistry method. Then replace the factory Create call at the bottom of the control''''s implementation unit with a call that creates an instance of your new factory class.

Example:

The following code defines a special factory class called TSpecialFactory that adds a sub-key of the class key called "SpecialKey". This key has a numeric value, which you set in the constructor.

type TSpecialFactory = class(TActiveXControlFactory) 
public 
  constructor Create(ComServer: TComServerObject; ActiveXControlClass: TActiveXControlClass; WinControlClass:
    TWinControlClass; const ClassID: TGUID; ToolboxBitmapID:
    Integer; const LicStr: string; MiscStatus: Integer; SpecialKeyValue:
    Integer); override;
  procedure UpdateRegistry(Register: Boolean); override;
protected 
    FSpecialKeyValue: Integer; 
end;
constructor TSpecialFactory.Create(ComServer: TComServerObject; ActiveXControlClass: TActiveXControlClass; WinControlClass: TWinControlClass; const ClassID: TGUID; ToolboxBitmapID: Integer; const LicStr: string; MiscStatus: Integer; SpecialKeyValue: Integer); var TypeAttr: PTypeAttr; begin FSpecialKeyValue := SpecialKeyValue; inherited Create(ComServer, ActiveXControlClass, WinControlClass, ClassID, ToolboxBitmapID, LicStr, MiscStatus); end;
procedure TSpecialFactory.UpdateRegistry(Register: Boolean); var ClassKey: string; begin ClassKey := ''''CLSID\'''' + GUIDToString(ClassID); if Register then begin inherited UpdateRegistry(Register); CreateRegKey(ClassKey + ''''\SpecialKey'''', '''''''', IntToStr(FSpecialKeyValue)); end else begin DeleteRegKey(ClassKey + ''''\SpecialKey''''); inherited UpdateRegistry(Register); end; end;

To use this class factory, simply replace the existing class factory creation code in the initialization section of your ActiveX library: The last line in the example below sets the value of "SpecialKey" to 1234.

initialization
  TSpecialFactory.Create(ComServer, TButtonX, TButton, 
    Class_ButtonX, 1, '''''''', 0, 1234);
end.    

Web Deployment and Code Signing

Web Deployment

ActiveX controls built with Delphi can be deployed to a web site for downloading into Internet Explorer. Before you can deploy your control, there are several things you need to figure out:
  • Where the binary codebase will reside on your server. This could be an URL like "http://www.mycorp.com/code/DAXSamp.ocx".
  • A directory where Delphi can copy the HTML file. If the HTTP server is running on your machine, this could be a local filename like "c:\https\codebase\".
  • A directory where Delphi can copy the codebase file. If the HTTP server is running on your machine, this could be a local filename like "c:\https\pages\".

Once you''''ve specified these basic options, you can copy the code to the server by invoking the Project|Web Deploy command. When you invoke the Web Deploy command, Delphi generates a web page that refers to your control and copies it to the HTML destination directory. Delphi then copies the codebase to the web server''''s codebase destination directory.

The generated HTML code looks like the following code. Using the above example directories, Delphi copies it to a file called c:\https\pages\DAXSamp.HTM.


Delphi ActiveX Test Page

You should see your Delphi forms or controls embedded in the form below.


It the server is a remote Web server that you don''''t have file-system access to, you will need to tell Delphi to put the files in a local directory. Then, you can use your usual web-page deployment method (for example, FTP or FrontPage) to deploy the code to your server.

Code Signing

Signing your ActiveX control accomplishes two things. First, it identifies you or your organization as the author of the code. Second, it gives the recipient of the code the ability to verify that what they received is what you made and it hasn''''t been tampered with.

The key element in code signing is your certificate, which is a code key assigned to you by a company called a certificate authority. The certificate file appears in the form of a .SPC (Software Publisher''''s Certificate) file, delivered to you by the certificate authority. You also need a private key file (.PVK), which you created as part of your application to the CA for the SPC file.

Microsoft also provides you with a means of creating untrusted keys for testing purposes. See Microsoft''''s web site, http://www.microsoft.com/workshop/prog/security/authcode/codesign.htm for the paper, "Signing Code with Microsoft''''s Authenticode". The process is also described in the Microsoft INET SDK, which is where you will find the tools required for manufacturing the untrusted test keys.

Delphi 3 provides a project options page where you can specify your code signature information, including the file that contains the cr

上一页  [1] [2] [3] [4] [5] [6] [7] [8]  下一页


[办公软件]excel中的VBA中的With语句的使用介绍及实例  [系统软件]OLE with the internet explorer
[Delphi程序]Delphi深度探索-数据库明了的ActiveX控件  [Delphi程序]override deal with window closing in database …
[Delphi程序]DELPHI实现activex控件的限制  [Delphi程序]Delphi使用VB编写的ActiveX控件全攻略
[Delphi程序]Delphi使用VB6编写的ActiveX控件???  [Delphi程序]MediaPlayer9 ActiveX 攻略(原创)
[VB.NET程序]Socket Programming with VB  [VB.NET程序]Managing Windows with WMI
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台