给你一个简单的用PaintBox实现的例子:
//-----------------------------------------------------------------------
TMyCurve = class
private
Fv1: array[0..100] of integer;
Fv2: array[0..100] of integer;
FMaxValue: integer;
FEnabled: boolean;
FPaintBox: TPaintBox;
procedure SetPaintBox(value: TPaintBox);
procedure PaintBoxPaint(Sender: TObject);
procedure SetV1(i: integer; value: integer);
function GetV1(i: integer): integer;
procedure SetV2(i: integer; value: integer);
function GetV2(i: integer): integer;
procedure DrawCoordinate;
procedure DrawCurve;
public
procedure Repaint;
property PaintBox: TPaintBox read FPaintBox write SetPaintBox;
property Enabled: boolean read FEnabled write FEnabled;
property MaxValue: integer read FMaxValue write FMaxValue;
property V1[i:integer]: integer read GetV1 write SetV1;
property V2[i:integer]: integer read GetV2 write SetV2;
end;
procedure TMyCurve.SetPaintBox(value: TPaintBox);
begin
if FPaintBox <> value then
begin
FPaintBox := value;
if FPaintBox <> nil then
begin
with FPaintBox do
OnPaint := PaintBoxPaint;
end;
end;
end;
procedure TMyCurve.Repaint;
begin
PaintBoxPaint(nil);
end;
procedure TMyCurve.PaintBoxPaint(Sender: TObject);
begin
if (FPaintBox = nil) or (FEnabled = false) then
exit;
with FPaintBox.Canvas do
begin
with Brush do
begin
Color := clYellow;
Style := bsSolid;
end;
FillRect(Bounds(0,0,FPaintBox.Width,FPaintBox.Height));
end;
DrawCoordinate;
DrawCurve;
end;
procedure TMyCurve.DrawCoordinate;
var
dx, dy: real;
i: integer;
begin
with FPaintBox.Canvas do
begin
with Pen do
begin
Color := clNavy;
Style := psDot;
Width := 1;
end;
Brush.Style := bsClear;
dx := (FPaintBox.Width - 20) / 10;
dy := (FPaintBox.Height - 20) / 10;
for i := 0 to 10 do
begin
MoveTo(10 + Round(i * dx), 10);
LineTo(10 + Round(i * dx), FPaintBox.Height - 10);
MoveTo(10, 10 + Round(i * dy));
LineTo(FPaintBox.Width - 10, Round(10 + i * dy));
end;
end;
end;
procedure TMyCurve.DrawCurve;
var
dx, dy: real;
i: integer;
begin
if FMaxValue < 2 then
exit;
dx := (FPaintBox.Width - 20) / 100;
dy := (FPaintBox.Height - 20) / FMaxValue; // MaxValue = 500
with FPaintBox.Canvas do
begin
with Pen do
begin
Color := clRed; //
Style := psSolid;
Width := 2;
end;
Brush.Style := bsClear;
// 第一条曲线
MoveTo(10, Round(FPaintBox.Height - 10 - FV1[0] * dy));
for i := 1 to 99 do
begin
LineTo(10 + Round(i * dx), Round(FPaintBox.Height - 10 - FV1 * dy));
end;
// 第二条曲线
Pen.Color := clLime;
MoveTo(10, Round(FPaintBox.Height - 10 - FV2[0] * dy));
for i := 1 to 99 do
begin
LineTo(10 + Round(i * dx), Round(FPaintBox.Height - 10 - FV2 * dy));
end;
end;
end;
procedure TMyCurve.SetV1(i: integer; value: integer);
begin
if (i >= 0) and (i < 100) then
begin
FV1 := value;
Repaint;
end;
end;
function TMyCurve.GetV1(i: integer): integer;
begin
if (i >= 0) and (i < 100) then
Result := FV1
else
Result := 0;
end;
procedure TMyCurve.SetV2(i: integer; value: integer);
begin
if (i >= 0) and (i < 100) then
begin
FV2 := value;
Repaint;
end;
end;
function TMyCurve.GetV2(i: integer): integer;
begin
if (i >= 0) and (i < 100) then
Result := FV2
else
Result := 0;
end;
//-------------------------------------------------------------------------
应用
procedure TForm1.FormCreate(Sender: TObject);
begin
FCurve := TMyCurve.Create;
with FCurve do
begin
PaintBox := PaintBox1;
Enabled := true;
end;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
FCurve.Destroy;
end;
// 画正弦曲线
procedure TForm1.Button2Click(Sender: TObject);
var
i, x1, x2: integer;
begin
Randomize;
x1 := Random(50);
x2 := Random(50);
with FCurve do
begin
MaxValue := 500;
Enabled := false;
for i := 0 to 99 do
begin
V1 := Round(sin((i + x1)/ 100 * 2 * Pi) * 250 + 250);
V2 := Round(sin((i + x2)/ 100 * 2 * Pi) * 200 + 250);
end;
Enabled := true;
Repaint;
end;
end;
// 动态性能图
procedure TForm1.Button3Click(Sender: TObject);
begin
if Timer1.Enabled then
begin
Timer1.Enabled := false;
Timer2.Enabled := false
end else
begin
Randomize;
FCurve.MaxValue := 500;
Timer1.Enabled := true;
Timer2.Enabled := true;
end;
end;
// 更新曲线1,时间间隔1000ms
procedure TForm1.Timer1Timer(Sender: TObject);
var
i: integer;
begin
with FCurve do
begin
MaxValue := 500;
Enabled := false;
for i := 1 to 99 do
V1[i - 1] := V1;
V1[99] := Random(500);
Enabled := true;
Repaint;
end;
end;
// 更新曲线2, 时间间隔500ms
procedure TForm1.Timer2Timer(Sender: TObject);
var
i: integer;
begin
with FCurve do
begin
MaxValue := 500;
Enabled := false;
for i := 1 to 99 do
V2[i - 1] := V2;
V2[99] := Random(500);
Enabled := true;
Repaint;
end;
end;