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

Building ActiveX Controls with Delphi 3

作者:闵涛 文章来源:闵涛的学习笔记 点击数:5173 更新时间:2009/4/23 18:31:11
data from or insert data into a persistence stream. This happens when a control is being restored from a form file or saved into one.

Working with OLE Streams

In Delphi, the standard streaming class is called TStream, which has Read, Write and Seek methods. Most Delphi objects are derived from TPersistent, which is a class that can save its contents to a TStream. In the OLE world, stream objects provide an interface called IStream that also has Read, Write and Seek methods. Delphi 3 provides a class called TOleStream that exposes an IStream as a TStream. When a TActiveXControl is told to save its state to a stream via the SaveToStream method, it is given the IStream as a parameter. If you want to save extra data for your control to the stream, and the data being saved is a TPersistent-derived object, you can use the stream adaptor to allow the TPersistent object to save itself to the IStream. Here is an example of code that uses TOleStream to save and load a string list in addition to the control''''s properties.

var
  ExtraInfo: TStringList;
procedure TButtonX.SaveToStream( const Stream: IStream);
var
  dStream: TStream;
begin 
  inherited; dStream := TOleStream.Create( Stream ); 
  try 
    ExtraInfo.SaveToStream(dStream); 
  finally
    dStream.free; 
  end;
end;

procedure TButtonX.LoadFromStream( const Stream: IStream );
begin 
  inherited; 
  dStream := TOleStream.Create(Stream ); 
  try 
    ExtraInfo.LoadFromStream(dStream); 
  finally
    dStream.Free; 
  end;
end;

Adding verbs to a control

A verb is a user-initiated action, generally from a menu item, that causes the object to do something interesting. Examples include ''''cut'''', ''''execute'''' or ''''run''''. You can add verb capabilities to your control by adding two pieces of code-one to register the verb and another to execute the verb.


Registering a verb with the object''''s factory lets the verb information be copied into the system registry when the library is installed. This is a requirement of ActiveX because it allows the object''''s verbs to be displayed without having the object loaded in memory first.


To register verbs, call the AddVerb method on the factory object, as in the following code. Note the ampersand (''''&'''') in the verb description strings-it is common practice for the container to display this string in a menu item for the user, and the ampersand is used to indicate the keyboard selection character for the menu item.


In the following example code, a verb called ''''Click'''' is added to the TButtonX control, which will allow the user to click the button from the container''''s ''''Click'''' menu item. When the user selects ''''Click'''', the button''''s click method is called, which simulates a button click.

const
  VERB_CLICK = 100;
initialization 
  with
    TActiveXControlFactory.Create( ComServer, TButtonX, TButton, Class_ButtonX,
        1, '''''''', 0) do 
  begin 
    AddVerb( VERB_CLICK, ''''&Click'''');
  end;
end.

The container is responsible for popping up a menu that contains the object''''s verbs, and it then calls the ActiveX control when the user selects one of the menu items. The DAX class hierarchy calls the object''''s PerformVerb method to actually execute the verb.

The PerformVerb method for the Click example would be as follows:

procedure TButtonXControl.PerformVerb(Verb: Integer);
begin
  case Verb of
  VERB_CLICK: 
    FDelphiControl.Click;
  else 
    inherited PerformVerb(Verb);
  end;
end;

Adding Property Pages to an ActiveX Control

A property page is a form embedded in a notebook control called a property dialog. Like the Delphi Object Inspector, the property dialog''''s purpose is to provide the user with a way to edit the control''''s properties. Rather than presenting the user with a long list of property names and values, the property dialog presents related properties together on a page.

You''''re not required to provide property pages for an ActiveX control, but they can be useful if your intended end-user is a non-technical user.

The property pages that appear inside a property dialog are just normal windows, but they are also OLE contained objects, just like ActiveX Controls. Because a property page is an OLE object, it has to be packaged in an ActiveX library and registered in the system registry.

The property page doesn''''t have to actually exist in the same library as the control that uses it. This is because some property pages can represent common data types and be reused across multiple libraries. Delphi provides four basic property pages for font, color, picture and string properties. The ClassIDs of these are

  Class_DColorPropPage
  Class_DFontPropPage
  Class_DPicturePropPage
  Class_DStringPropPage
Every property page ''''edits'''' an OLE object. When the property page becomes active, it needs to copy properties from the object into the controls on the page. When the user clicks the Apply button, the page needs to copy the properties back to the OLE object.

To add a property page to your project, you need to start the ActiveX Property Page wizard from the Object Repository. This wizard will generate a new unit and a form. For the purposes of this example, I''''ll also put an edit control on the form, so I can use it to edit the Caption property of my control.

The unit for the resulting property page looks like the following code segment. There are four places to focus on in this code, which are discussed in the text.

unit Unit1;
interface
uses
SysUtils, Windows, Messages, Classes, Graphics, Controls, StdCtrls, ExtCtrls, Forms, ComServ, ComObj, StdVcl, AxCtrls; type TPropertyPage1 = class(TPropertyPage)
The page is derived from TPropertyPage, which in turn is derived from TCustomForm. This means you can design the form as you would design any other form. TPropertyPage adds an OleObject property, which references the object your property page is editing. TPropertyPage also declares the UpdatePropertyPage and UpdateObject methods, which you override below.
  Edit1: TEdit; 
private 
  { Private declarations} 
protected 
  procedure UpdatePropertyPage; override;
  procedure UpdateObject; override; 
public
{ Public declarations } 
end; 

const Class_PropertyPage1: TGUID = ''''{75ACC806-A9A5-11D0-A6DF-444553540000}'''';

implementation {$R *.DFM} procedure TPropertyPage1.UpdatePropertyPage; begin { Update your controls from OleObject }

This method is called by the DAX hierarchy in order to copy data from the OleObject to the page''''s controls. UpdatePropertyPage is called when the property dialog first comes up, but can be called again if the user presses the Undo button (if present).

Here is an example of code to copy the Caption property from OleObject to a control on the page. (This code assumes you''''ve placed an edit control on your form, and it''''s called Edit1).

    Edit1.Text := OleObject.Caption;

If you want your property page to show radio buttons or other complex interacting controls, the code may be more complicated than this simple example but the principle is the same: get the value of a property from OleObject and set the value of one or more controls on the form.

end;

procedure TPropertyPage1.UpdateObject; begin { Update OleObject from your controls }

This method does the reverse of the UpdatePropertyPage method-it copies the data from the controls to OleObject''''s properties. This normally happens when the user presses the OK or Apply keys.


Here is an example of code to copy data back from the edit control to OleObject''''s Caption property:

  OleObject.Caption := Edit1.Text;
end;

initialization TActiveXPropertyPageFactory.Create( ComServer, TPropertyPage1, Class_PropertyPage1);

This code registers the property page as a COM object in the ActiveX library. TActiveXPropertyPageFactory is the class to use when creating the factory for property pages. As with ActiveX Controls, ComServer is the global variable that represents the ActiveX library. TPropertyPage1 is the form class declared above, and Class_PropertyPage1 is the object''''s ClassID.

end.

Connecting the Property Page to an ActiveX Control

Once you''''ve designed a property page, you need to add the page to the control''''s list of pages. DAX asks an ActiveX Control to provide the ClassIDs of all its property pages by calling the protected DefinePropertyPages method. The method''''s parameter is a callback that you can call to add the ClassID of one of your property pages to the list. When DefinePropertyPages returns, the property dialog creates the page objects and selects the first one to the front.

procedure TButtonX.DefinePropertyPages(DefinePropertyPage: TDefinePropertyPage);
begin
  DefinePropertyPage(Class_PropertyPage1);
end;

Because this code is executed when the user brings up the property dialog, you have complete control about which pages to present to the user. Depending on the user''''s license or access rights, you may choose not to show certain pages for an object.

Accessing Ambient Properties

An ambient property is a property provided by the control''''s container. Once the control is inserted in a container, it can query for the values of the container''''s ambient properties.

The cont

上一页  [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……
    咸宁网络警察报警平台