260 likes | 447 Views
Developing Custom VCL and VCL.NET Component Designers. DevCon 2005 -- Course No: 3146. Agenda. The Delphi design-time environment Property Editors TAlign property editor Integer property editor Component Editors TButton component editor TListBox component editor
E N D
Developing Custom VCL and VCL.NET Component Designers DevCon 2005 -- Course No: 3146
Agenda • The Delphi design-time environment • Property Editors • TAlign property editor • Integer property editor • Component Editors • TButton component editor • TListBox component editor • TRadioGroup component editor
The Design-Time Environment • Object Inspector uses property editors to • Display individual property values • Edit individual property values • Form Designer uses component editors to • Edit component as a whole • Add items to context menu • Respond to double clicks
Important Points • Design editors work seamlessly within the design environment • Properties and components are not aware of the editors Delphi uses • It is possible to replace an existing editor.
Property Editors • Why create custom property editors? • Provide an alternate way of editing a property • Supply a drop-down list with predefined values • Display a custom dialog to modify the property visually • Support a non-standard custom property
Property Editor Tasks • Convert property value • Native Format string • Define how the property can be edited • Inline - within the Object Inspector • Dialog - using a separate dialog box.
Standard Property Editors • All property editors descend from TPropertyEditor • Defined in the following units • DesignIntf • DesignEditors • VclEditors • DesignMenus • For VCL.NET, must prefix namespace • Borland.Vcl.Design.DesignIntf • Borland.Vcl.Design.DesignEditors • Borland.Vcl.Design.VCLEditors • Borland.Vcl.Design.DesignMenus.
TPropertyEditor TOrdinalProperty TIntegerProperty TColorProperty TCursorProperty TCharProperty TEnumProperty TSetProperty TFloatProperty TStringProperty TCaptionProperty TFontNameProperty TSetElementProperty TClassProperty TFontProperty TStringListProperty TMethodProperty TComponentProperty
Creating a New Property Editor • Create a descendant of TPropertyEditor • Usually descend from standard editor • Ensure Value property works properly • Not a concern if inheriting from standard editor • Define the editing capabilities of the editor • Implement appropriate interface methods • Register the property editor.
Implementing GetValue • Use the following methods to get the value stored in the property • GetFloatValue, GetInt64Value, GetOrdValue, GetStrValue, GetMethodValue, GetVarValue • For object references • GetOrdValue in VCL • GetObjValue in VCL.NET function TIntegerProperty.GetValue: string; begin Result := IntToStr( GetOrdValue ); end;
Implementing SetValue • Use the following methods to set the property value • SetFloatValue, SetInt64Value, SetOrdValue, SetStrValue, SetMethodValue, SetVarValue • For object references • SetOrdValue in VCL • SetObjValue in VCL.NET function TFloatProperty.SetValue( const Value: string ); begin SetFloatValue( StrToFloat( Value ) ); end;
Overriding GetAttributes • Determines how editor appears in the Object Inspector • Returns a set of attributes • Common attributes • paValueList • paSortList • paDialog • paMultiSelect • paAutoUpdate
The GetValues Method • Override when paValueList is specified • Specify values to appear in drop down list • Proc points to Add method of temp string list procedure TFontNameProperty.GetValues( Proc: TGetStrProc ); var I: Integer; begin for I := 0 to Screen.Fonts.Count - 1 do Proc( Screen.Fonts[ I ] ); end;
Owner-Draw Drop-Down Lists • Customize the appearance of items in list • Implement the following interfaces • ICustomPropertyDrawing • ListMeasureHeight • ListMeasureWidth • ListDrawValue • ICustomPropertyListDrawing • PropDrawName • PropDrawValue
The Edit Method • Override Edit to perform action when ellipsis button is pressed or entry is double-clicked • Typically used to display a custom dialog • TFontProperty • Not necessary to display a dialog box • TMethodProperty generates an empty even handler
Registering a Property Editor • Define a Register procedure • Registration unit recommended • Call RegisterPropertyEditor • Parameters determine scope of editor // Edit all TStrings properties with TStringListProperty RegisterPropertyEditor( TypeInfo( TStrings ), nil, '', TStringListProperty ); // But edit the TQuery.SQL property with TRkSQLProperty RegisterPropertyEditor( TypeInfo( TStrings ), TQuery, 'SQL', TRkSQLProperty );
Examples • TRkAlignProperty • Inline editor with owner draw drop-down list • TRkIntegerProperty • Dialog-based property editor
Component Editors • Why create custom component editors? • Add menu items to context menu displayed by Form Designer • Change double-click action • Easier to create than property editors • No need to worry about string representation.
Creating a Component Editor • Derive a new class from • TComponentEditor • TDefaultEditor • Define the editing capabilities of the editor • Override appropriate methods • Register the component editor.
TDefaultEditor • Overrides the Edit method • When component is double-clicked, Edit searches for the following events: • OnCreate • OnChange • OnClick • First defined event • Generates (or navigates to) event handler
Context Menu Methods • Override GetVerbCount • Return number of menu items to add • Override GetVerb • Return string to display for corresponding menu item • Override ExecuteVerb • Implement functionality of new menu items
The PrepareItem Method • Provides access to menu item object for each menu item • Actually an IMenuItem interface • Allows menu items to be customized • Enabled/Disabled • Checked/Unchecked • Create Sub-Menus • Unfortunately, you cannot specify an ImageIndex or Glyph to be used for the menu
The Edit Method • Override to respond to double click • The Edit method of TComponentEditor invokes ExecuteVerb( 0 ) if applicable
Registering a Component Editor • Use the RegisterComponentEditor procedure • Most recently registered editor will be used for editing the component • This allows you to replace an existing editor RegisterComponentEditor( TRadioGroup, TRkRadioGroupEditor );
Example • TRkButtonEditor • Add menu items • Displays a custom dialog • TRkListBoxEditor • Add menu items and sub-menu items to context menu • EditPropertyByName in RkDesignEditors unit • TRkRadioGroupEditor • Displays a custom dialog • Dynamic cascading menus • Registration Units
The Finish Line • Contact Information • Evaluation Forms • Questions & Answers Ray Konopka rkonopka@raize.com http://www.raize.com