程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 屬性和控件編輯器

屬性和控件編輯器

編輯:Delphi
Delphi提供了開放的API,是程序員可以增強Delphi IDE的功能。共有4種開放工具的APIs:屬性編輯器、控件編輯器、專家/導航和版本控制系統。本文討論屬性編輯器和控件編輯器,給出的例子說明如何寫自己的Delphi屬性、控件編輯器。

屬性編輯器
屬性編輯器是Delphi IDE的擴展。這聽起來非常復雜和困難,但是實際上是很簡單的。我們可以為枚舉類型構造一個屬性編輯器。記得TForm的顏色屬性嗎?當我們想改變它的值,看到了下拉框中列出了所有的可選值。那就是枚舉類型的屬性編輯器,我們也同樣能做到,只需要幾行代碼,沒什麼特別的。注意到程序員並沒有寫一個屬性編輯器,而是通知Delphi使用枚舉類型的屬性編輯器,為它的枚舉特別定義的。

現有的屬性編輯器

在我們搞清楚屬性編輯器到底內部是什麼之前,先看看Delphi中已有的。開始一個新工程,在implementation中加入"uses DsgnIntf;"編譯,打開browser查找TPropertyEditor(只要輸入TPrope):

Object Browser

如果沒算錯的話,在DSGNINTF中注冊了至少21個客戶屬性編輯器(custom property editors),注意:事實上,還有更多的屬性編輯器在其他單元中,例如C:\Delphi\LIB\PICEDIT.DCU.中的TPictureEditor。

TPropertyEditor

對象察看器為所有的屬性提供缺省的編輯。我們可以使用不同的方法重載這種行為,來使用特別的屬性編輯器(21種預制的屬性編輯器都擴充了對象察看器來處理其屬性)。那麼,究竟是怎樣工作的呢?它是起源一個基類,我們必需重載已達到我們的目的。五個新的Delphi 2.0的方法-其中三個是變量相關的-在編譯開關{$IFDEF WIN32}中一保證一下代碼在所有的Delphi版本中適用。

TypeTPropertyEditor = classprotectedfunction GetPropInfo: PPropInfo;function GetFloatValue: Extended;function GetFloatValueAt(Index: Integer): Extended;function GetMethodValue: TMethod;function GetMethodValueAt(Index: Integer): TMethod;function GetOrdValue: Longint;function GetOrdValueAt(Index: Integer): Longint;function GetStrValue: string;function GetStrValueAt(Index: Integer): string;{$IFDEF WIN32}function GetVarValue: variant;function GetVarValueAt(Index: Integer): variant;{$ENDIF}procedure ModifIEd;procedure SetFloatValue(Value: Extended);procedure SetMethodValue(const Value: TMethod);procedure SetOrdValue(Value: Longint);procedure SetStrValue(const Value: string);{$IFDEF WIN32}procedure SetVarValue(const Value: variant);{$ENDIF}
publicdestructor Destroy; override;
procedure Activate; virtual;function AllEqual: Boolean; virtual;procedure Edit; virtual;function GetAttributes: TPropertyAttributes; virtual;function GetComponent(Index: Integer): TComponent;function GetEditLimit: Integer; virtual;function GetName: string; virtual;procedure GetPropertIEs(Proc: TGetPropEditProc); virtual;function GetPropType: PTypeInfo;function GetValue: string; virtual;procedure GetValues(Proc: TGetStrProc); virtual;procedure Initialize; virtual;{$IFDEF WIN32}procedure Revert;{$ENDIF}procedure SetValue(const Value: string); virtual;{$IFDEF WIN32}procedure ValueAvailable: Boolean;{$ENDIF}
property Designer: TFormDesigner read FDesigner;property PrivateDirectory: string read GetPrivateDirectory;property PropCount: Integer read FPropCount;property Value: string read GetValue write SetValue;end;

TPropertyEditor編輯對象察看器中一個或是一串控件的一個屬性。屬性編輯器根據屬性的類型而被創建,由RegisterPropertyEditor注冊的類型決定。稍候有一個指示程序員如何使用這些工程的例子。所有的published屬性都將出現在對象察看器中,當設計者進行讀寫屬性的值時,其屬性編輯器(為這種屬性類型的)將被使用。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved