Mega Code Archive

 
Categories / Delphi / Forms
 

Copy multiple form areas into the clipboard

Title: Copy multiple form areas into the clipboard Question: How can i copy several areas (panels for example) from my form into the clipboard ? Answer: This is a very good example of a 'beautiful solution' for a certain problem. The VCL makes it possible !! Our application had a chart together with detail-information which was placed in a separate panel. The problem was to copy the chart together with the information displayed inside the panel into the clipboard. The method CopyToClipboardBitmap from TChart is not really helping in this case. Thanks to the object oriented structure of the VCL, every class has a method PaintTo. This method is introduced with the class TWinControl. It paints the content of the control into a device context. Therefore its possible that every controls is painting into a bitmap that is created by the main program. TBitmap owns a method SaveToClipboardFormat. After this the bitmap can by transfered into the clipboard via TClipboard.SetAsHandle. Here is the code that copies the content of Chart1 (TChart) and Panel1 (TPanel) into the clipboard: var CBoard : TBitmap; AData : THandle; APalette : HPalette; AFormat : Word; Clipboard : TClipboard; begin CBoard := TBitmap.Create; CBoard.Width := Chart1.Width; CBoard.Height := Chart1.Height + Panel1.Height; Chart1.Refresh; Chart1.PaintTo (CBoard.Canvas.Handle, 0, Panel1.Height); Panel1.PaintTo (CBoard.Canvas.Handle, 0, 0); CBoard.SaveToClipboardFormat (AFormat, AData, APalette); Clipboard := TClipboard.Create; Clipboard.SetAsHandle(AFormat, AData); Clipboard.Destroy; CBoard.Destroy; end;