原創(chuàng)|其它|編輯:郝浩|2012-11-29 11:03:01.000|閱讀 583 次
概述:默認情況下,所有常用控件編輯器創(chuàng)建的是空白OnClick事件處理程序。可以通過編寫自定義編輯器來更改該行為。因此,本文將主要介紹如何編寫自定義編輯器。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
默認情況下,所有常用控件編輯器創(chuàng)建的是空白OnClick事件處理程序。可以通過編寫自定義編輯器來更改該行為。此外,自定義編輯器可以添加額外的項目到控件的上下文菜單。
“TfrxComponentEditor”是所有編輯器的基類,在frxDsgnIntf文件中已作申明:
TfrxComponentEditor = class(TObject)
protected
function AddItem(Caption: String; Tag: Integer;
Checked: Boolean = False): TMenuItem;
public
function Edit: Boolean; virtual;
function HasEditor: Boolean; virtual;
function Execute(Tag: Integer; Checked: Boolean): Boolean; virtual;
procedure GetMenuItems; virtual;
property Component: TfrxComponent readonly;
property Designer: TfrxCustomDesigner readonly;
end;
如果你的編輯器沒有在上下文菜單中創(chuàng)建自己的項目,那么你需要重寫“Edit”和“HasEditor”這兩個方法。第一種方法執(zhí)行必要的動作,若對組件的內(nèi)容進行了修改,則返回“True”。如果你的控件有一個編輯器, “HasEditor” 方法便返回“true”。如果返回“FALSE”,或者方法為被重寫時,編輯器將無法打開。如果你的控件沒有編輯器,且你希望添加項目到控件的上下文菜單中,便返回“False”。
如果,編輯器添加項目到上下文菜單中,你應(yīng)該重寫“GetMenuItems”和“Execute”方法。
根據(jù) “frxDsgnIntf” 文件中所定義的過程進行編輯器注冊。
frxComponentEditors.Register(ComponentClass: TfrxComponentClass;
ComponentEditor: TfrxComponentEditorClass);
第一個參數(shù)是創(chuàng)建編輯器的控件類名。第二個參數(shù)是編輯器的類名。
接下來,一起來看看常用控件的一個簡單編輯器。
FastReport VCL 報表控件要求編輯器代碼存放在一個與組件代碼文件夾同名的文件夾中,其后綴名為'Editor'。
uses frxClass, frxDsgnIntf, frxBitBtn;
type
TfrxBitBtnEditor = class(TfrxComponentEditor)
public
function Edit: Boolean; override;
function HasEditor: Boolean; override;
function Execute(Tag: Integer; Checked: Boolean): Boolean; override;
procedure GetMenuItems; override;
end;
function TfrxBitBtnEditor.Edit: Boolean;
var
c: TfrxBitBtnControl;
begin
Result := False;
{ Component property is edited component;
in this case, it is TfrxBitBtnControl }
c := TfrxBitBtnControl(Component);
ShowMessage('This is ' + c.Name);
end;
function TfrxBitBtnEditor.HasEditor: Boolean;
begin
Result := True;
end;
function TfrxBitBtnEditor.Execute(Tag: Integer; Checked: Boolean):
Boolean;
var
c: TfrxBitBtnControl;
begin
Result := True;
c := TfrxBitBtnControl(Component);
if Tag = 1 then
c.Enabled := Checked
else if Tag = 2 then
c.Visible := Checked;
end;
procedure TfrxBitBtnEditor.GetMenuItems;
var
c: TfrxBitBtnControl;
begin
c := TfrxBitBtnControl(Component);
{ AddItem method parameters: menu item name,
its tag and Checked/Unchecked condition }
AddItem('Enabled', 1, c.Enabled);
AddItem('Visible', 2, c.Visible);
end;
initialization
frxComponentEditors.Register(TfrxBitBtnControl, TfrxBitBtnEditor);
end.
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都控件網(wǎng)