Ray Konopka Developing Custom.NET Components DevCon 2005 -- Course No: 3118.
-
Upload
brice-blair -
Category
Documents
-
view
228 -
download
0
Transcript of Ray Konopka Developing Custom.NET Components DevCon 2005 -- Course No: 3118.
Ray KonopkaRay Konopka
Developing Custom .NET Components
Developing Custom .NET Components
DevCon 2005 -- Course No: 3118DevCon 2005 -- Course No: 3118
2
AgendaAgenda
.NET Component Model
Comparing the FCL to the VCL
Custom Events
Custom Painting
Component Related Attributes
Sample Control
.NET Component Model
Comparing the FCL to the VCL
Custom Events
Custom Painting
Component Related Attributes
Sample Control
3
.NET Component Model.NET Component Model
Defined in the System.ComponentModel namespace
IncludesSystem.ComponentModel.Component
declarationCommon DelegatesComponent Related Attribute declarationsLicensing ClassesDesign-time Support Classes
Defined in the System.ComponentModel namespace
IncludesSystem.ComponentModel.Component
declarationCommon DelegatesComponent Related Attribute declarationsLicensing ClassesDesign-time Support Classes
4
Component ClassComponent Class
System.ComponentModel.Component provides the base implementation of IComponent in the FCL
Defines the following propertiesDesignModeContainerSiteEvents
Descend from System.ComponentModel.Component to create a nonvisual component
System.ComponentModel.Component provides the base implementation of IComponent in the FCL
Defines the following propertiesDesignModeContainerSiteEvents
Descend from System.ComponentModel.Component to create a nonvisual component
5
Two Control Classes in .NETTwo Control Classes in .NET
Most visual controls in .NET will descend from one of two classes:
System.Windows.Forms.ControlBase WinForms Control classMost similar to existing component models (e.g.
VCL)Rendering handled through Graphics class (i.e.
GDI+)
System.Web.UI.ControlBase ASP.NET Server Control classRenders markup text used by a client’s browser
or viewing device to display visual elements.
Most visual controls in .NET will descend from one of two classes:
System.Windows.Forms.ControlBase WinForms Control classMost similar to existing component models (e.g.
VCL)Rendering handled through Graphics class (i.e.
GDI+)
System.Web.UI.ControlBase ASP.NET Server Control classRenders markup text used by a client’s browser
or viewing device to display visual elements.
6
System.Windows.Forms.Control
System.Windows.Forms.Control
Implements basic functionality for visualization
Defines the control’s bounds (i.e. its Position and Size)
Provides a window handle (Handle)
Provides access to keyboard events
Provides access to mouse events
Provides access to paint events
Supports ambient propertiesCursor, Font, BackColor, ForeColor,
RightToLeft.
Implements basic functionality for visualization
Defines the control’s bounds (i.e. its Position and Size)
Provides a window handle (Handle)
Provides access to keyboard events
Provides access to mouse events
Provides access to paint events
Supports ambient propertiesCursor, Font, BackColor, ForeColor,
RightToLeft.
7
Comparing the FCL to the VCL
Comparing the FCL to the VCL
FCL is quite similar to the VCL, but there are significant differences
No equivalent to TGraphicControl
Actually, no TWinControl or TCustomControl classes either
System.Windows.Forms.Control is more like TCustomControl than TControl
Significantly fewer TCustomXxxx class called XxxxBase in FCL
FCL is quite similar to the VCL, but there are significant differences
No equivalent to TGraphicControl
Actually, no TWinControl or TCustomControl classes either
System.Windows.Forms.Control is more like TCustomControl than TControl
Significantly fewer TCustomXxxx class called XxxxBase in FCL
8
Where is the Align property?
Where is the Align property?
Most of the functionality implemented in the VCL base classes is available in the base FCL classes
However, there are many property name changesAlign DockCaption TextColor BackColorFont.Color ForeColorPopupMenu ContextMenuModalResult DialogResultetc.
Tag is an Object
ParentXxxx properties replaced with ambient properties
Most of the functionality implemented in the VCL base classes is available in the base FCL classes
However, there are many property name changesAlign DockCaption TextColor BackColorFont.Color ForeColorPopupMenu ContextMenuModalResult DialogResultetc.
Tag is an Object
ParentXxxx properties replaced with ambient properties
9
Control StylesControl Styles
TControl.ControlStyle replaced with GetStyle/SetStyle method pair
Some, but not all, styles have corresponding public propertiesResizeRedraw
TControl.ControlStyle replaced with GetStyle/SetStyle method pair
Some, but not all, styles have corresponding public propertiesResizeRedraw
SetStyle( ControlStyles.ResizeRedraw, True );SetStyle( ControlStyles.Opaque, True );
// Double BufferingSetStyle( ControlStyles.UserPaint, True );SetStyle( ControlStyles.AllPaintingInWmPaint, True );SetStyle( ControlStyles.DoubleBuffer, True );
SetStyle( ControlStyles.ResizeRedraw, True );SetStyle( ControlStyles.Opaque, True );
// Double BufferingSetStyle( ControlStyles.UserPaint, True );SetStyle( ControlStyles.AllPaintingInWmPaint, True );SetStyle( ControlStyles.DoubleBuffer, True );
10
Component NotificationsComponent Notifications
Component notifications allow descendants to react to state changes implemented in an ancestor class
In the VCL, a component responds to notifications by handling special messagese.g. cm_EnabledChanged
In .NET, a component responds to notifications by overriding a methodOnEnabledChanged Event Dispatch Method
.NET even surfaces component notifications as public eventsEnabledChanged Event
Component notifications allow descendants to react to state changes implemented in an ancestor class
In the VCL, a component responds to notifications by handling special messagese.g. cm_EnabledChanged
In .NET, a component responds to notifications by overriding a methodOnEnabledChanged Event Dispatch Method
.NET even surfaces component notifications as public eventsEnabledChanged Event
11
FCL and VCL EventsFCL and VCL Events
Unfortunately, the naming convention used for events in the FCL is opposite that used in the VCL
In the VCL…OnPaint is the eventPaint is the event dispatch method
In the FCL… OnPaint is the event dispatch methodPaint is the event
Unfortunately, the naming convention used for events in the FCL is opposite that used in the VCL
In the VCL…OnPaint is the eventPaint is the event dispatch method
In the FCL… OnPaint is the event dispatch methodPaint is the event
12
FCL and VCL EventsFCL and VCL Events
Please Note:Unfortunately, it is common in .NET literature to
see statements like “…take a look at the OnPaint event handler…”
Problem is that base (ancestor) does not get called and thus the real event never gets raised
Please Note:Unfortunately, it is common in .NET literature to
see statements like “…take a look at the OnPaint event handler…”
Problem is that base (ancestor) does not get called and thus the real event never gets raised
13
EventsEvents
Events allow customization through delegation
Events are properties
Implemented using Delegates
Events are optional
Events allow customization through delegation
Events are properties
Implemented using Delegates
Events are optional
14
Creating a Custom EventCreating a Custom Event
Determine the action that triggers event
Define the event type (i.e. the Delegate)Specifies the parameters that will be sent to an
event handler
Declare the event propertySingleton Event (read/write)Multicast Event (add/remove)
Dispatch the event when it occurs.
Determine the action that triggers event
Define the event type (i.e. the Delegate)Specifies the parameters that will be sent to an
event handler
Declare the event propertySingleton Event (read/write)Multicast Event (add/remove)
Dispatch the event when it occurs.
15
Determine Event TriggerDetermine Event Trigger
Events allow developers to “hook” into normal processing
ExampleGenerate a ValueChanged event when data
represented by the component changes
Events allow developers to “hook” into normal processing
ExampleGenerate a ValueChanged event when data
represented by the component changes
16
DelegatesDelegates
The FCL utilizes Delegates to support events
A delegate is a class that encapsulates a linked-list of method pointers
With delegates an event can be sent to multiple subscribers
Specify the parameters that will be sent to event handlers.
The FCL utilizes Delegates to support events
A delegate is a class that encapsulates a linked-list of method pointers
With delegates an event can be sent to multiple subscribers
Specify the parameters that will be sent to event handlers.
17
Define the DelegateDefine the Delegate
May be possible to use predefined delegatee.g. EventHandler
First parameter always “object sender”
Second parameter always EventArgs or descendant
If you require passing additional parameters, you’ll need to create a new EventArgs descendant
May be possible to use predefined delegatee.g. EventHandler
First parameter always “object sender”
Second parameter always EventArgs or descendant
If you require passing additional parameters, you’ll need to create a new EventArgs descendantValueChangingEventHandler =
procedure ( sender: System.Object; e: ValueChangingEventArgs ) of object;
ValueChangingEventHandler = procedure ( sender: System.Object; e: ValueChangingEventArgs ) of object;
18
Case Sensitivity in Delphi?Case Sensitivity in Delphi?
Delphi is not case sensitive, but other .NET languages are (namely C#)
Event handler signature is exported in case sensitiveIn C#, parameters are lower case
As such, recommended to use
and not
Delphi is not case sensitive, but other .NET languages are (namely C#)
Event handler signature is exported in case sensitiveIn C#, parameters are lower case
As such, recommended to use
and notValueChangingEventHandler = procedure ( Sender: System.Object; E: ValueChangingEventArgs ) of object;
ValueChangingEventHandler = procedure ( Sender: System.Object; E: ValueChangingEventArgs ) of object;
ValueChangingEventHandler = procedure ( sender: System.Object; e: ValueChangingEventArgs ) of object;
ValueChangingEventHandler = procedure ( sender: System.Object; e: ValueChangingEventArgs ) of object;
19
Declare the Event PropertyDeclare the Event Property
Unlike the VCL, by convention event names in the FCL do not start with “On”Unlike the VCL, by convention event names in the FCL do not start with “On”
// Multicast Eventproperty ValueChanged: EventHandler add FValueChanged remove FValueChanged;
// Singleton Eventproperty ValueChanging: ValueChangingEventHandler read FValueChanging write FValueChanging;
// Multicast Eventproperty ValueChanged: EventHandler add FValueChanged remove FValueChanged;
// Singleton Eventproperty ValueChanging: ValueChangingEventHandler read FValueChanging write FValueChanging;
20
Raise the EventRaise the Event
Use an Event Dispatch Method
In FCL, event dispatch methods start with “On”
Usually defined as strict protected and virtual
Calling the event (e.g. FValueChanged) causes all of the event handlers in the delegate’s list to be called
Use an Event Dispatch Method
In FCL, event dispatch methods start with “On”
Usually defined as strict protected and virtual
Calling the event (e.g. FValueChanged) causes all of the event handlers in the delegate’s list to be called
procedure RkSpinner.OnValueChanged( e: EventArgs );begin if Assigned( FValueChanged ) then FValueChanged( Self, e );end;
procedure RkSpinner.OnValueChanged( e: EventArgs );begin if Assigned( FValueChanged ) then FValueChanged( Self, e );end;
21
Custom PaintingCustom Painting
Custom painting supported by Graphics class (GDI+)System.DrawingSystem.Drawing.Drawing2DSystem.Drawing.ImagingSystem.Drawing.Text
GDI+ FeaturesAlpha Blending & Anti-Aliased 2D DrawingGradient BrushesUniversal Transformations & FP coordinatesSupport for more Image formats
BMP, GIF, JPEG, PNG, TIFF, ICON, WMF, EMF.
Custom painting supported by Graphics class (GDI+)System.DrawingSystem.Drawing.Drawing2DSystem.Drawing.ImagingSystem.Drawing.Text
GDI+ FeaturesAlpha Blending & Anti-Aliased 2D DrawingGradient BrushesUniversal Transformations & FP coordinatesSupport for more Image formats
BMP, GIF, JPEG, PNG, TIFF, ICON, WMF, EMF.
22
GDI+ Programming ModelGDI+ Programming Model
No more device contexts (DC) – Graphics Object
GDI+ is StatelessNo more selecting pens and brushes into a DCPens, Brushes, etc. are passed to each GDI+
drawing method
Graphic elements are no longer drawn with both Pen and BrushDraw methods use a Pen (eg. DrawRectangle)Fill methods use a Brush (eg. FillEllipse).
No more device contexts (DC) – Graphics Object
GDI+ is StatelessNo more selecting pens and brushes into a DCPens, Brushes, etc. are passed to each GDI+
drawing method
Graphic elements are no longer drawn with both Pen and BrushDraw methods use a Pen (eg. DrawRectangle)Fill methods use a Brush (eg. FillEllipse).
23
GDI+ Programming ModelGDI+ Programming Model
Colors support Alpha ChannelsARGB format
0x880000FF semi-transparent blue
A = 0x00 fully transparentA = 0xFF fully opaque
Rectangles, Points, etc. are classesr.Inflate( 5, 5 ); // Instead of InflateRect( r, 5, 5
);
Rectangles are defined differently!Left, Top, Width, Height.
Colors support Alpha ChannelsARGB format
0x880000FF semi-transparent blue
A = 0x00 fully transparentA = 0xFF fully opaque
Rectangles, Points, etc. are classesr.Inflate( 5, 5 ); // Instead of InflateRect( r, 5, 5
);
Rectangles are defined differently!Left, Top, Width, Height.
24
GDI+ Programming ModelGDI+ Programming Model
Obtaining a Graphics ObjectPassed to OnPaint methods in
PaintEventArgsRequest one using
Graphics.FromHwnd( Handle )If utilizing double-buffering, do not use
FromHwnd
Cleaning UpDispose all GDI+ objectsDispose Graphics object if requested via
FromHwnd
Obtaining a Graphics ObjectPassed to OnPaint methods in
PaintEventArgsRequest one using
Graphics.FromHwnd( Handle )If utilizing double-buffering, do not use
FromHwnd
Cleaning UpDispose all GDI+ objectsDispose Graphics object if requested via
FromHwnd
25
AttributesAttributes
Attributes used to modify the behavior of properties, methods, eventsAttributes used to modify the behavior of properties, methods, events
[ Category( 'Appearance' ) ][ Description( 'The width of buttons, in pixels.' ) ][ DefaultValue( 18 ) ]property ButtonWidth: Integer read FButtonWidth write SetButtonWidth;
// No Category or Description, but DefaultValueproperty ButtonHeight: Integer read FButtonHeight write SetButtonHeight default 18;
[ Category( 'Appearance' ) ][ Description( 'The width of buttons, in pixels.' ) ][ DefaultValue( 18 ) ]property ButtonWidth: Integer read FButtonWidth write SetButtonWidth;
// No Category or Description, but DefaultValueproperty ButtonHeight: Integer read FButtonHeight write SetButtonHeight default 18;
26
AttributesAttributes
Category
Description
Browsable
DefaultValue
DefaultProperty & DefaultEvent
Localizable
ToolboxBitmap
Category
Description
Browsable
DefaultValue
DefaultProperty & DefaultEvent
Localizable
ToolboxBitmap
27
ExampleExample
RayKonopka.Delphi.Controls AssemblyRkSpinner Component
RayKonopka.Delphi.Controls AssemblyRkSpinner Component
28
The Finish LineThe Finish Line
Contact Information
Evaluation Forms
Questions & Answers
Contact Information
Evaluation Forms
Questions & Answers
Ray [email protected]://www.raize.com
Ray [email protected]://www.raize.com