dbgrid导出为图片的问题 ( 积分: 100 )

  • 主题发起人 那年黄梅花开
  • 开始时间

那年黄梅花开

Unregistered / Unconfirmed
GUEST, unregistred user!
我想把dbgrid保存为jpeg图片
有两个问题
1 加上button1.visible时保存的图片为空白图片
2 加上setControlVisible(false)时程序最后会报错:abstract error等等

procedure TForm1.Button1Click(Sender: TObject);
var
BMP: TBitMap;
tpJpg:TJpegImage;
Rect,SRect:TRect;

ileft,itop,iwidth,iheight: integer;//记录以前的一些变量
begin
ileft:= dbgrid1.left;
itop:= dbgrid1.top;
iwidth:= dbgrid1.width;
iheight:= dbgrid1.height;

DBGrid1.SetBounds(0,0,ClientWidth,ClientHeight);//max

//setControlVisible(false);

//Button1.visible:= false;


BMP := TBitMap.Create;
tpJpg:=TJpegImage.create;
try
BMP.Width := self.ClientWidth ;
BMP.Height := self.ClientHeight;
SRect.Left := 0;
SRect.Top := 0;
SRect.Right := Bmp.Width;
SRect.Bottom:= Bmp.Height;
Rect := SRect;
Rect.Left := Rect.Left;
Rect.Top := Rect.Top;
bmp.PixelFormat := pf24bit;
BMP.Canvas.CopyRect(Rect, self.Canvas, SRect);
tpJpg.Assign(BMP);

tpJpg.SaveToFile('gg.jpg');
Finally
Bmp.Free;
tpJpg.Free;

with DBGrid1 do
SetBounds(ileft,itop,iWidth,iHeight); //restore

//setControlVisible(true);
end;
end;

procedure TForm1.setControlVisible(bVisible: boolean);
var
i: integer;
begin
for i:= 0 to self.ComponentCount-1 do
if TWinControl(Components).Name<> DBGrid1.Name then
TWinControl(self.Components).Visible:= bVisible;
end;
 
在dbgrid1.setbounds后增加一个

dbgrid1.Repaint;
 
另:
Rect.Left := Rect.Left;
Rect.Top := Rect.Top;
应该是:
Rect.Left := ileft;
Rect.Top :=itop;
吧!
 
函数:
function GetCtrlImage(ctrl: TWincontrol): TBitmap; //取控件图片
var
Ofs: Integer;
begin
with ctrl do
begin
Result := TBitmap.Create;
try
Result.Width := ClientWidth;
Result.Height := ClientHeight;
Result.Canvas.Brush := Brush;
Result.Canvas.FillRect(ClientRect);
Result.Canvas.Lock;
try
if GetWindowLong(Handle, GWL_STYLE) and WS_BORDER <> 0 then
Ofs := -1 // Don't draw form border
else
Ofs := 0; // There is no border
PaintTo(Result.Canvas.Handle, Ofs, Ofs);
finally
Result.Canvas.Unlock;
end;
except
Result.Free;
raise;
end;
end; // end with
end;
调用
GetCtrlImage(Dbgrid1).SaveToFile(fileName);
 
哦,看错了,rect的left和top不需要设置了
 
谢谢关注
procedure TForm1.setVisible(bVisible: boolean);
var
i: integer;
begin
for i:= 0 to Self.ComponentCount-1 do
if self.Componentsis TWinControl then
if TWinControl(Self.Components).name<>'DBGrid1' then
TWinControl(Self.Components).Visible:= bVisible;
end;
 
顶部