drawgrid画图之后图像闪动很严重,求指点

      2020-03-17 21:53      HTML5

drawgrid画图之后图像闪动很严重,求指点

我用drawgrid学习制作一个表格工具,意图是通过拖拽工具栏里的人物一寸照片进入表格中可以实现动态排班的效果。
但是制作出来我发现了一个不知道怎么解决的情况就是,例如,我在表格第4列第一行拖入一个头像照片后,图像都剧烈的闪动。
我是基于五子棋程序的思想建立的,底层建立一个矩阵来控制drawgrid的,我检查了一下,发现矩阵的赋值没有任何问题。

请教delphi大师帮忙诊断一下会是哪里出了问题

private
Code:Array[0..16,0..5] of integer;
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
Bmp1:TBitMap;
Bmp2:TBitMap;
Bmp3:TBitMap;
Bmp4:TBitMap;
D:integer;
row:integer;
col:integer;


implementation

uses Unit2;

......

procedure TForm1.DrawGrid1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
Accept:=True;
drawgrid1.MouseToCell(x,y,col,row);
statusBar1.Panels[1].Text:='col='+inttostr(col);
statusBar1.Panels[0].Text:='row='+inttostr(row);
end;

procedure TForm1.DrawGrid1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
i,j:integer;
begin
if(Sender Is TDrawgrid) and (Source Is TSpeedbutton ) then
begin
drawgrid1.MouseToCell(x,y,col,row);
Code[row,col]:=D;
Drawgrid1.OnDrawCell:= DrawGrid1DrawCell;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
i,j: integer;
begin
D:=0;
for i:=1 to 16 do
for j:=1 to 5 do
begin
Code[i,j]:=0;
end;
Drawgrid1.OnDrawCell:= DrawGrid1DrawCell;
end;


procedure TForm1.SpeedButton1MouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
if Button = mbLeft then
(Sender As TSpeedButton).BeginDrag(False);
D:=1;
end;

procedure TForm1.DrawGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
I:Integer;
begin
Bmp1:= SpeedButton1.Glyph;
Bmp2:= Form2.SpeedButton2.Glyph;
Bmp3:= Form2.SpeedButton3.Glyph;
Bmp4:= Form2.SpeedButton4.Glyph;

with (Sender as TDrawGrid) do
begin
Drawgrid1.ColWidths[0]:=20; //第一列大小
For I:= 1 to 17 do//第一列行序号
begin
if (ACol = 0)and(ARow = i) then
Drawgrid1.Canvas.TextOut(Rect.Left+1, Rect.Top+14, IntToStr(ARow))
end;
//第一行列标题
if (ACol = 1)and(ARow = 0) then
Drawgrid1.Canvas.TextOut(Rect.Left+27, Rect.Top+14, 'A')
else if (ACol = 2)and(ARow = 0) then
Drawgrid1.Canvas.TextOut(Rect.Left+27, Rect.Top+14, 'B')
else if (ACol = 3)and(ARow = 0) then
Drawgrid1.Canvas.TextOut(Rect.Left+27, Rect.Top+14, 'C')
else if (ACol = 4)and(ARow = 0) then
Drawgrid1.Canvas.TextOut(Rect.Left+27, Rect.Top+14, 'D')
else if (ACol = 5)and(ARow = 0) then
Drawgrid1.Canvas.TextOut(Rect.Left+27, Rect.Top+14, 'E')
else if (code[acol,arow]=0)and(ACol <> 0)and(ARow <> 0) then
Drawgrid1.Invalidate //清空
else if (code[acol,arow]=1)and(ACol <> 0)and(ARow <> 0) then//成型
Drawgrid1.Canvas.Draw(Rect.Left+10,Rect.Top,Bmp1)
else if (code[acol,arow]=2)and(ACol <> 0)and(ARow <> 0) then
Drawgrid1.Canvas.Draw(Rect.Left+10,Rect.Top,Bmp2)
else if (code[acol,arow]=3)and(ACol <> 0)and(ARow <> 0) then
Drawgrid1.Canvas.Draw(Rect.Left+10,Rect.Top,Bmp3)
else if (code[acol,arow]=4)and(ACol <> 0)and(ARow <> 0) then
Drawgrid1.Canvas.Draw(Rect.Left+10,Rect.Top,Bmp4)
end;
end;

end.

昵称: kalelshey 时间: 2011-08-29 10:15:34

闪烁产生一般都是刷新背景造成的。

你可以建立一个大的TBitmap对象,把你的图先画在TBitmap上面, 然后就bitblt到你显示的Canvas上面,就可以避免图像闪烁的问题。不要直接在显示的Canvas对象上直接画图。

用Drawgrid的话,处理游戏一类的情况还是不太合用的。

昵称: robotdeng 时间: 2011-09-26 18:55:40