?? 為delphi 4創建連接主頁的組件 (2000年7月19日).txt
字號:
為Delphi 4創建連接主頁的組件 (2000年7月19日)
本站更新 分類: 作者:周友 劉春芳 推薦: 閱讀次數:622
(http://www.codesky.net)
--------------------------------------------------------------------------------
關鍵詞:組件、類補全、包
摘要:本文以創建連接主頁的組件為例,詳細介紹了在Delphi 4下如何創建自己組件的方法及一些技巧。
為Delphi 4創建連接主頁的組件之前,首先應確定組件是否為可視組件,本文中要創建的組件是不可視組件,
由Tcomponent基本類派生。組件核心是由ShellApi中的函數ShellExecute來完成的,其函數說明在ShellApi
中可以找到,方法是按住Ctrl點擊ShellExecute,Delphi 4直接跳轉到ShellApi.PAS單元中的ShellExcute聲
明處(前提是在Uses條目中應包含ShellApi):
function ShellExecute(hWnd: HWND; Operation, FileName, Parameters,
Directory: PChar; ShowCmd: Integer): HINST; stdcall;
各參數含義可參見Delphi在線幫助:
HWnd :父窗口句柄
Operation :指向執行指定操作的字符串的指針
FileName :指向文件名或文件夾字符串的指針
Parameters :指向指定可執行文件參數字符串的指針
Directory :指向指定缺省目錄字符串的指針
ShowCmd :當文件打開時是否顯示
下面我們利用Component Wizard 編寫組件,方法是選擇
菜單File | New,從New Items的第一頁New中選擇Component或選擇菜單Component | New Component,此時會顯示如下窗口:
在對話框中輸入以下信息:
Ancestor type(父輩類型) :TComponent
Class Name(組件類名) :TExplorerWeb
Palette Page(組件面板頁名稱) :Zhouy
輸入完成后,按OK按鈕,Component Wizard自動生成如下標準樣本代碼:
unit ExplorerWeb;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
type
TExplorerWeb = class(TComponent)
private
{ Private declarations }
protected
{ Protected declarations }
public
{ Public declarations }
published
{ Published declarations }
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Zhouy', [TExplorerWeb]);
end;
end.
標準代碼中所有語句只是為了給你提供方便,如果你不需要可以刪去。
接下來我們要做的就是在樣本基礎上完善程序。在樣本代碼的開始部分是Uses條目,其中包含了標準組件所需的必要條目,現在需要增加我們的組件所需條目:ShellApi。
在類聲明中做如下聲明:
//為創建OnErrorEvent事件,先定義一個指向方法的指針
//ErrorCode 為要傳遞的錯誤代碼
TNotifyEvent = procedure(Sender: TObject; ErrorCode: Integer) of Object;
private
FOnErrorEvent: TNotifyEvent;
public
constructor Create(AOwner: TComponent); override; // 重載Create 方法
destructor Destroy; override; // 重載Destroy 方法
procedure Open; // 本組件中用到的方法
published
{ Published declarations }
// 定義屬性及事件
property OnErrorEvent: TNotifyEventr read FOnErrorEvent write FOnErrorEvent;
property HomePage: string; // 屬性
好了,此時讓我們利用Delphi 4最重要的新特征之一類補全技術,自動為對象中的所有正確聲明和實現完成代碼。將光標放在任何一個新聲明的方法上,按Ctrl+Shift+C!哇!你會驚喜地發現標準樣本變成了如下模樣:
unit ExplorerWeb;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,ShellApi;
type
TNotifyEvent = procedure(Sender: TObject;ErrorCode: Integer) of Object;
TExplorerWeb = class(TComponent)
private
FOnErrorEvent: TNotifyEvent;
FHomePage: string;
procedure SetHomePage(const Value: string);
{ Private declarations }
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Open;
published
{ Published declarations }
property OnErrorEvent: TNotifyEvent read FOnErrorEvent write FOnErrorEvent;
property HomePage: string read FHomePage write SetHomePage;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Zhouy', [TExplorerWeb]);
end;
{ TExplorerWeb }
constructor TExplorerWeb.Create(AOwner: TComponent);
begin
end;
destructor TExplorerWeb.Destroy;
begin
end;
procedure TExplorerWeb.Open;
begin
end;
procedure TExplorerWeb.SetHomePage(const Value: string);
begin
FHomePage := Value;
end;
end.
繼續完善自動生成的代碼:
constructor TExplorerWeb.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FHomePage := 'http://www.keylab.net/~zhouy';
end;
destructor TExplorerWeb.Destroy;
begin
inherited Destroy;
end;
procedure TExplorerWeb.Open;
var
ErrorCode: Integer;
begin
ErrorCode := ShellExecute(Application.Handle, 'Open', PChar(FHomePage),
nil, nil, SW_SHOWNORMAL);
if Assigned( FOnErrorEvent ) then FOnErrorEvent( Self, ErrorCode );
end;
下面我們再為組件創建一個圖標。與組件關聯的圖標放置在Compoent Pallete 中,它們定義在一個具有.DCR擴展名的文件中,如果不提供該文件,Delphi使用與對象的父類關聯的圖標。如果在組件的父類或父類以上類中的任何地方都沒有圖標,那么就會使默認的圖標。
我們可以使用Image Editor 創建圖標:
· 打開Image Editor并選擇New | Component Resource(后綴為.DCR),出現Utitled1.DCR對話框
·右擊Contents 項 -> New -> BitBmp
·Colors 選16色,Size選28x28或更小24x24
·從其他文件或自己勾畫一幅圖,本例以C:\Program Files\Common Files\Borland Shared\Images\Icons\Earth.ICO為基礎修改而成的
·將位圖改名為TexplorerWeb,并以ExplorerWeb.DCR 存盤退出。
還可以使用附件畫圖工具或其他編輯器來創建位圖,并創建.RC文件:
TexplorerWeb Bitmap ExplorerWeb.bmp
用命令行進行編譯:brc -r ExplorerWeb.RC
將所得的文件ExplorerWeb.RES 更名為ExplorerWeb.DCR
運行測試組件程序(這一步由讀者自己去做),無誤后將它放入Componet Pallette中。可以通過選擇Component | Install Component然后選擇Into New Component頁來實現,也可以用更簡單的方法創建包:
File | New | Package菜單,將激活Package Editor并創建一個名為Package1.DPK的新文件,如圖所示:
將缺省包保存為Zhouy.DPK。選擇Add按鈕,添加ExplorerWeb組件,此時可以看到Contains下有兩項:ExplorerWeb.DCR和ExplorerWeb.PAS。選擇Compile進行編譯,無誤后便可在Component Pallette中生成Zhouy頁,其中有一名為ExplorerWeb的組件。
至此我們創建了Delphi 4 下連接主頁的組件,此組件有HomePage屬性及OnErrorEvent事件。
下面簡單總結一下制作組件的過程:先確定組件的父類,利用Component Wizard,輔以Delphi 類補全技術自動生成組件程序,并完善所用到的聲明和實現;再用Image Editor 制作組件圖標;最后將組件增加到相應的包中編譯完成。
最后舉例說明如何使用我們制作的組件。
新建一個Project,添加一個Button組件和新制作的ExplorerWeb組件。
在Button的OnClick事件中寫入:
procedure TForm1.ButtonClick(Sender: TObject);
begin
ExplorerWeb.Open;
end;
在ExplorerWeb的OnErrorEvent事件中寫入:
procedure TForm1.ExplorerWebErrorEvent(Sender: TObject; ErrorCode: Integer);
begin
case ErrorCode of
0: MessageBox( Handle, '錯誤: 內存不足','錯誤提示',0 );
ERROR_FILE_NOT_FOUND: MessageBox( Handle, '錯誤:沒有找到文件',
'錯誤提示' ,0 );
ERROR_PATH_NOT_FOUND: MessageBox( Handle, '錯誤:目錄錯誤',
'錯誤提示' ,0 );
ERROR_BAD_FORMAT: MessageBox( Handle, '錯誤:錯誤的文件格式',
'錯誤提示' ,0 );
else
MessageBox( Handle,PChar('錯誤號: ' + IntToStr( ErrorCode ),'錯誤提示',0 );
end;
end;
運行此程序,當點擊按鈕時,將自動連接至‘Delphi 驛站’。若有錯誤,會給出相應提示。
以上是筆者制作組件的一點心得,愿與讀者們共享!并請批評指正。
聯系地址:山東省計算中心
聯系電話:0531-2605533
郵政編碼:250014
聯 系 人:周友(山東省計算中心) 劉春芳(濟南永寧制藥股份有限公司)
Email:zhouy@keylab.net
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -