Pies.pas
上传用户:psxgmh
上传日期:2013-04-08
资源大小:15112k
文件大小:4k
- unit Pies;
- interface
- uses Classes, Controls, Forms, Graphics, StdCtrls;
- type
- //定义一个角度类,用于馅饼类的角度属性类型使用
- TAngles = class(TPersistent)
- private
- FStartAngle: Integer; //开始角度
- FEndAngle: Integer;//终止角度
- FOnChange: TNotifyEvent;//变化事件
- procedure SetStart(Value: Integer); //写属性StartAngle的方法
- procedure SetEnd(Value: Integer); //写属性EndAngle的方法
- public
- procedure Assign(Value: TAngles); //给StartAngle,EndAngle属性赋值
- procedure Changed; //过程,表示已改变
- published
- property StartAngle: Integer read FStartAngle write SetStart;
- property EndAngle: Integer read FEndAngle write SetEnd;
- property OnChange: TNotifyEvent read FOnChange write FOnChange;
- end;
- //定义馅饼类
- TPie = class(TGraphicControl)
- FPen: TPen; //定义画笔
- FBrush: TBrush;//定义刷子
- FEdit: TEdit; //
- FAngles: TAngles; //定义角度
- constructor Create(AOwner: TComponent); override;
- procedure Paint; override;
- procedure SetBrush(Value: TBrush); //设置刷子属性
- procedure SetPen(Value: TPen); //设置画笔属性
- procedure SetAngles(Value: TAngles); //设置角度属性
- procedure StyleChanged(Sender: TObject);//当刷子、画笔、角度改变时,处理过程
- published
- property Angles: TAngles read FAngles write SetAngles;
- property Brush: TBrush read FBrush write SetBrush;
- property Pen: TPen read FPen write SetPen;
- property OnClick;
- property OnDblClick;
- property OnDragDrop;
- property OnDragOver;
- property OnEndDrag;
- property OnMouseDown;
- property OnMouseMove;
- property OnMouseUp;
- end;
- implementation
- uses Windows;
- //=====TAngles类的实现==============//
- procedure TAngles.Assign(Value: TAngles);
- begin
- StartAngle := Value.StartAngle;
- EndAngle := Value.EndAngle;
- end;
- procedure TAngles.SetStart(Value: Integer);
- begin
- if Value <> FStartAngle then
- begin
- FStartAngle := Value;
- //给FOnchanged事件赋值,表示已改变
- Changed;
- end;
- end;
- procedure TAngles.SetEnd(Value: Integer);
- begin
- if Value <> FEndAngle then
- begin
- FEndAngle := Value;
- //给FOnchanged事件赋值,表示已改变
- Changed;
- end;
- end;
- procedure TAngles.Changed;
- begin
- if Assigned(FOnChange) then FOnChange(Self);
- end;
- //====================TPie类的实现==========//
- constructor TPie.Create(AOwner: TComponent);
- begin
- inherited Create(AOwner);
- Width := 100;
- Height := 100;
- FPen := TPen.Create;
- FPen.OnChange := StyleChanged;
- FBrush := TBrush.Create;
- FBrush.OnChange := StyleChanged;
- FAngles := TAngles.Create;
- FAngles.OnChange := StyleChanged;
- FAngles.StartAngle := 180;
- FAngles.EndAngle := 90;
- end;
- //当馅饼改变时,重画整个组件
- procedure TPie.StyleChanged(Sender: TObject);
- begin
- Invalidate;
- end;
- //设置刷子属性
- procedure TPie.SetBrush(Value: TBrush);
- begin
- FBrush.Assign(Value);
- end;
- //设置画笔属性
- procedure TPie.SetPen(Value: TPen);
- begin
- FPen.Assign(Value);
- end;
- //设置角度属性
- procedure TPie.SetAngles(Value: TAngles);
- begin
- FAngles.Assign(Value);
- Invalidate;
- end;
- //重载paint过程
- procedure TPie.Paint;
- var
- StartA, EndA: Integer;
- midX, midY, stX, stY, endX, endY: Integer;
- sX, sY, eX, eY: Real;
- begin
- StartA := FAngles.StartAngle;
- EndA := FAngles.EndAngle;
- midX := Width div 2;
- midY := Height div 2;
- sX := Cos((StartA / 180.0) * pi);
- sY := Sin((StartA / 180.0) * pi);
- eX := Cos((EndA / 180.0) * pi);
- eY := Sin((EndA / 180.0) * pi);
- stX := Round(sX * 100);
- stY := Round(sY * 100);
- endX := Round(eX * 100);
- endY := Round(eY * 100);
- with Canvas do
- begin
- Pen := FPen;
- Brush := FBrush;
- Pie(0,0, Width,Height, midX + stX, midY - stY, midX + endX, midY - endY);
- end;
- end;
- end.