delphi cxgrid截图
效果图:
点击GetImage,会在程序目录下生成一个表格截图的图片文件,打开后如下图:
以下是代码:
procedure TForm4.SpeedButton1Click(Sender: TObject); var gridView: TcxGridDBTableView; focusedItem: TcxCustomGridTableItem; focusedRect: TRect; aBitmap: TBitmap; begin gridView := cxGrid1DBTableView1; focusedItem := gridView.Controller.focusedItem; focusedRect := gridView.ViewInfo.Bounds; aBitmap := TBitmap.Create; try ABitmap.Width := focusedRect.Width; ABitmap.Height := focusedRect.Height; cxPaintCanvas.BeginPaint(aBitmap.Canvas); try cxPaintControlTo(gridView.Site, cxPaintCanvas, Point(0, 0), focusedRect, True, False); finally cxPaintCanvas.EndPaint; end; aBitmap.SaveToFile('test.png'); finally ABitmap.Free; end; end;
你也可以直接把截图放到剪贴板中而不是生成图片文件:
uses Clipbrd; procedure TForm4.SpeedButton1Click(Sender: TObject); var gridView: TcxGridDBTableView; focusedItem: TcxCustomGridTableItem; focusedRect: TRect; aBitmap: TBitmap; begin gridView := cxGrid1DBTableView1; focusedItem := gridView.Controller.focusedItem; focusedRect := gridView.ViewInfo.Bounds; aBitmap := TBitmap.Create; try ABitmap.Width := focusedRect.Width; ABitmap.Height := focusedRect.Height; cxPaintCanvas.BeginPaint(aBitmap.Canvas); try cxPaintControlTo(gridView.Site, cxPaintCanvas, Point(0, 0), focusedRect, True, False); finally cxPaintCanvas.EndPaint; end; Clipboard.Assign(aBitmap); finally ABitmap.Free; end; end;