TMyPaintBox = class(TGraphicControl)
直接从TGraphicControl继承下来。上图中表格,数字,都是画出来的:
画表格:
procedure BmpCanvasDrawGrid(Starcode:word;Heigh,Width:integer;Canvas:TCanvas;
GBColor:TColor;GLColor:Tcolor);
var
i:integer;
Count:integer;
begin
with Canvas do begin
Brush.Style := bsSolid;
Brush.Color := GBColor;
Pen.Color := GBColor;
Rectangle(0,0,Width,Heigh); //填充背景色
Pen.Color := GLColor;
Pen.Style := psSolid;
Pen.Width := 1;
Count:=GetGridRowCount(Heigh);
Rectangle(0,0,380,Count * 35); //画个方形,只画到宽为320
for i:=1 to 8 do begin //竖线
if i in [2,6] then Pen.Width := 2
else Pen.Width := 1;
moveto(ZJGridX[i],0);
Lineto(ZJGridX[i],Count * 35);
end;
for i:=1 to Count do begin
if (Starcode mod 4)=0 then Pen.Width := 2
else Pen.Width := 1;
moveto(0,i * 35);
Lineto(379,i * 35);
inc(Starcode);
end;
end;
end;
写数字(部分代码):
with Canvas do begin
for j:=1 to 4 do begin
s:=str[j];
GetTextExtentPoint32(Handle,Pchar(s),length(s),Size);
X := ZJGridX[j+1] + (50 - Size.cx) div 2;
Y:=(Yset+(35 - Font.Height) div 2)-1;
OldBkMode:= SetBkMode(Handle, TRANSPARENT);
TextOut(X+1,Y+1,s);
SetBkMode(Handle, OldBkMode);
end;
Yset:=Yset+35;
end;
如果你写控件,TPANEL,TPaintBox都可以用,只是他们的具体应用有差别。
TPANEL如果你是自己写一个控件继承TPANEL,你就可以用它的Canvas,如果你想只接在TPANEL上画就要用这种方法:
var
Canvas:TCanvas;
HDC:THandle;
begin
Canvas:=TCanvas.Create;
HDC:=GetDC(Panel1.Handle);
Canvas.Handle:=HDC;
……
接下来就是对Canvas的操作了。用TPANEL的好处是,不用管重画事件。
TPaintBox相对简单点,直接操作它的Canvas就行了。但主窗重画时,TPaintBox不会重画,要自己写代码刷新。
不管用哪个控件都要了解TCanvas。如果你对TCanvas很了解的话,对于数字好控制一点不在话下。