18.09.2013, 19:00
|
Начинающий
|
|
Регистрация: 10.05.2010
Сообщения: 160
Репутация: 14
|
|
Компонент на основе TCollection и TCollectionitem
Пишу свой первый компонент на основе TCollection и TCollectionitem, всё хорошо, но не магу написать обработчик нажатия, в dfm и pas есть, но в run taime он не работает
Код:
unit DappledShape;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls;
type
TSpot = class(TCollectionItem)
private
FColor: TColor;
FOnClick: TNotifyEvent;
procedure SetColor(const Value: TColor);
protected
procedure Click; dynamic;
public
constructor Create(Collection: TCollection); override;
published
property Color: TColor read FColor write SetColor default clBlack;
property OnClick: TNotifyEvent read FOnClick write FOnClick;
end;
TDappledShape = class;
TItemChangeEvent = procedure(Item: TCollectionItem) of object;
TSpotCollection = class(TCollection)
private
FDappledShape: TDappledShape;
FOnItemChange : TItemChangeEvent;
function GetItem(Index: Integer): TSpot;
procedure SetItem(Index: Integer; const Value: TSpot);
protected
function GetOwner: TPersistent; override;
procedure Update(Item: TCollectionItem); override;
procedure DoItemChange(Item: TCollectionItem); dynamic;
public
constructor Create(DappledShape: TDappledShape);
function Add: TSpot;
property Items[Index: Integer]: TSpot read GetItem write SetItem; default;
published
property OnItemChange: TItemChangeEvent
read FOnItemChange write FOnItemChange;
end;
TDappledShape = class(TCustomControl)
private
FSpots: TSpotCollection;
FButtonItems: TSpot;
FButtonWidth, FButtonHeight: Integer;
procedure SetSpots(const Value: TSpotCollection);
procedure SetButtonHeight(const Value: Integer);
procedure SetButtonWidth(const Value: Integer);
protected
procedure UpdateAllButtons;
procedure Paint; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property Spots: TSpotCollection read FSpots write SetSpots;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Samples', [TDappledShape]);
end;
{ TSpot }
constructor TSpot.Create(Collection: TCollection);
begin
inherited Create(Collection);
FColor := clBlack;
FOnClick:=OnClick;
end;
constructor TDappledShape.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FSpots := TSpotCollection.Create(Self);
Width := 50;
Height := 50;
ControlStyle := [csDoubleClicks, csCaptureMouse, csDisplayDragImage, csPannable];
TabStop := True;
Touch.InteractiveGestures := [igPan, igPressAndTap];
Touch.InteractiveGestureOptions := [igoPanInertia, igoPanSingleFingerHorizontal,
igoPanSingleFingerVertical, igoPanGutter, igoParentPassthrough];
end;
procedure TSpot.Click;
begin
if Assigned(FOnClick) then FOnClick(Self);
end;
procedure TSpot.SetColor(const Value: TColor);
begin
if FColor <> Value
then begin
FColor := Value;
Changed(False)
end
end;
{ TSpotCollection }
function TSpotCollection.Add: TSpot;
begin
Result := TSpot(inherited Add)
end;
constructor TSpotCollection.Create(DappledShape: TDappledShape);
begin
inherited Create(TSpot);
FDappledShape := DappledShape
end;
procedure TDappledShape.SetButtonWidth(const Value: Integer);
begin
if (FButtonWidth <> Value) and (Value > 0) then
begin
FButtonWidth := Value;
UpdateAllButtons;
end;
end;
procedure TDappledShape.SetButtonHeight(const Value: Integer);
begin
if (FButtonHeight <> Value) and (Value > 0) then
begin
FButtonHeight := Value;
UpdateAllButtons;
end;
end;
procedure TDappledShape.UpdateAllButtons;
begin
Invalidate;
end;
procedure TSpotCollection.DoItemChange(Item: TCollectionItem);
begin
if Assigned(FOnItemChange) then FOnItemChange(Item)
end;
function TSpotCollection.GetItem(Index: Integer): TSpot;
begin
Result := TSpot(inherited GetItem(Index))
end;
function TSpotCollection.GetOwner: TPersistent;
begin
Result := FDappledShape
end;
procedure TSpotCollection.SetItem(Index: Integer; const Value: TSpot);
begin
inherited SetItem(Index, Value)
end;
procedure TSpotCollection.Update(Item: TCollectionItem);
begin
inherited Update(Item);
FDappledShape.Invalidate;
DoItemChange(Item)
end;
{ TDappledShape }
destructor TDappledShape.Destroy;
begin
FSpots.Free;
inherited Destroy
end;
procedure TDappledShape.Paint;
var
SaveColor: TColor;
SaveStyle: TBrushStyle;
i: integer;
begin
inherited Paint;
SaveColor := Canvas.Brush.Color;
SaveStyle := Canvas.Brush.Style;
Canvas.Brush.Style := bsSolid;
for i := 0 to FSpots.Count - 1 do
with FSpots.Items[i] do
begin
Canvas.Brush.Color := Color;
Canvas.Pie(
Width div 2 - 50, Height div 2 - 50,
Width div 2 + 50, Height div 2 + 50,
Width div 2, Height div 2,
Width div 2, Height div 2 - 50);
Canvas.Pen.color:=ClWhite;
end;
Canvas.Brush.Style := SaveStyle;
Canvas.Brush.Color := SaveColor
end;
procedure TDappledShape.SetSpots(const Value: TSpotCollection);
begin
FSpots.Assign(Value)
end;
end.
Пример взял в интернете и слегка переделал
|