LoadFromClipBoard (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

The following code draws a bitmap image from the Clipboard when a button is pressed. To place the icon on the clipboard, the SaveToClipboard button must be selected before the LoadFromClipboard button.

Code

#include <Clipbrd.hpp>
#include <memory>       //For STL auto_ptr class

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  TClipboard *cb = Clipboard();
  if (cb->HasFormat(CF_BITMAP))
  {
	std::auto_ptr<Graphics::TBitmap> Bitmap(new Graphics::TBitmap());
	try
	{
	  Bitmap->LoadFromClipboardFormat(CF_BITMAP, cb->GetAsHandle(CF_BITMAP), 0);
	  Canvas->Draw(5, 5, Bitmap.get());
    }
	catch (...)
	{
	  MessageBeep(0);
	  ShowMessage("Error reading image from clipboard!");
	}
	cb->Clear();
  }
  else
  {
	MessageBeep(0);
	ShowMessage("Cannot find image in clipboard!");
  }
}

void __fastcall TForm1::Button2Click(TObject *Sender)
{
  unsigned int DataHandle;
  HPALETTE APalette;
  unsigned short MyFormat;
  std::auto_ptr<Graphics::TBitmap> Bitmap(new Graphics::TBitmap());
  try
  {
	Bitmap->LoadFromFile("C:\\Program Files\\Common Files\\CodeGear Shared\\Images\\Splash\\256Color\\FACTORY.BMP");
	// Generate a clipboard format, with data and palette.
	Bitmap->SaveToClipboardFormat(
	  MyFormat,
	  DataHandle,
	  APalette);
    // Save the data to the clipboard using that format and
    // the generated data.
    Clipboard()->SetAsHandle(MyFormat,DataHandle);
  }
  catch (...)
  {
	 ShowMessage("Failed to copy image to clipboard!");
  }
}

Uses