Copy an image and text to the clipboard at the same time (Views: 31)
Problem/Question/Abstract: How to copy an image and text to the clipboard at the same time Answer: The clipboard can have multiple items in different formats on it. However, you need to add the various items to the clipboard using API functions rather than the Delphi object wrappers. The Delphi wrappers assume they are the only item on the clipboard and clear everything else off. The following shows one way to put both a bitmap and text on the clipboard. { ... } var lBmpFmt: TBMPExportFormat; lTmpBmp: Graphics.TBitmap; lData: THandle; lFormat: Word; lPalette: HPALETTE; lTxtFmt: TSeriesDataText; Data: THandle; DataPtr: Pointer; lTxt: PChar; begin Clipboard.Open; try {Make sure the clipboard is cleared every time. Someone may have put some other formats on it that hide the things we're going to put on it (since there's a search protocol for appropriate types and our types may be lower in the protocol and so not be found when it comes time to paste).} Clipboard.Clear; {Save as a bitmap} lBmpFmt := TBMPExportFormat.Create; try lBmpFmt.Panel := Self; lTmpBmp := lBmpFmt.Bitmap; try lPalette := 0; lTmpBmp.SaveToClipboardFormat(lFormat, lData, lPalette); SetClipboardData(lFormat, lData); if lPalette <> 0 then SetClipboardData(CF_PALETTE, lPalette); finally lTmpBmp.Free; end; finally lBmpFmt.Free; end; {Save as text} lTxtFmt := TSeriesDataText.Create(Self); try lTxt := PChar(lTxtFmt.AsString); finally lTxtFmt.Free; end; Data := GlobalAlloc(GMEM_MOVEABLE + GMEM_DDESHARE, StrLen(lTxt) + 1); try DataPtr := GlobalLock(Data); try Move(lTxt^, DataPtr^, StrLen(lTxt) + 1); if SetClipboardData(CF_TEXT, Data) = 0 then ShowMessage(SysErrorMessage(GetLastError)); finally GlobalUnlock(Data); end; except GlobalFree(Data); raise; end; finally Clipboard.Close; end; end; |