Programming C++Builder How to use the clipboard with graphics--copy, cut, paste graphics with the clipboard
Notice: Undefined variable: relatedArticlesCount in /home/wwwroot/codeback.net/plugins/content/tags.php on line 149

How to use the clipboard with graphics?

You can use the Windows clipboard to copy and paste graphics within your applications or to exchange graphics with other applications. The VCL's clipboard object makes it easy to handle different kinds of information, including graphics.

Before you can use the clipboard object in your application, you must add an include statement for Clipbrd.hpp to any .cpp file that needs to access clipboard data.
For cross-platform applications, data that is stored on the clipboard when using CLX is stored as a MIME type with an associated TStream object. CLX provides predefined constants for the following MIME types.

MIME type CLX constant
'image/delphi.bitmap' SDelphiBitmap
'image/delphi.component' SDelphiComponent 
'image/delphi.picture' SDelphiPicture
'image/delphi.drawing' SDelphiDrawing

 

How to copy the graphics to the clipboard?

You can copy any picture, including the contents of image controls, to the clipboard. Once on the clipboard, the picture is available to all applications.
To copy a picture to the clipboard, assign the picture to the clipboard object using the Assign method.

This code shows how to copy the picture from an image control named Image to the clipboard in response to a click on an Edit|Copy menu item:

void __fastcallTForm1::Copy1Click(TObject *Sender)
{
  Clipboard()->Assign(Image->Picture);
}

How to cut graphics to the clipboard?

Cutting a graphic to the clipboard is exactly like copying it, but you also erase the graphic from the source. To cut a graphic from a picture to the clipboard, first copy it to the clipboard, then erase the original.

In most cases, the only issue with cutting is how to show that the original image is erased. Setting the area to white is a common solution, as shown in the following code that attaches an event handler to the OnClick event of the Edit|Cut menu item:

void __fastcallTForm1::Cut1Click(TObject *Sender)
{
  TRect ARect;
  Copy1Click(Sender);          // copy picture to clipboard
  Image->Canvas->CopyMode = cmWhiteness; // copy everything as white
  ARect = Rect(0, 0, Image->Width, Image->Height); // get dimensions of image
  Image->Canvas->CopyRect(ARect, Image->Canvas, ARect); // copy bitmap over self
  Image->Canvas->CopyMode = cmSrcCopy; // restore default mode
}

How to paste graphics from the clipboard?

If the clipboard contains a bitmapped graphic, you can paste it into any image object, including image controls and the surface of a form.
To paste a graphic from the clipboard:

  1. Call the clipboard's HasFormat method (if using the VCL) or Provides method (if using CLX) to see whether the clipboard contains a graphic.
    HasFormat (or Provides in CLX) is a Boolean function. It returns true if the clipboard contains an item of the type specified in the parameter. To test for graphics on the Windows platform, you pass CF_BITMAP. In cross-platform applications, you pass SDelphiBitmap.
  2. Assign the clipboard to the destination.

VCL: This VCL code shows how to paste a picture from the clipboard into an image control in response to a click on an Edit|Paste menu item:

void __fastcallTForm1::Paste1Click(TObject *Sender)
{
  Graphics::TBitmap *Bitmap;
  if (Clipboard()->HasFormat(CF_BITMAP)){
 Image1->Picture->Bitmap->Assign(Clipboard());
  }
}

CLX: The same example in CLX for cross-platform development would look as follows:

void __fastcallTForm1::Paste1Click(TObject *Sender)
{
  QGraphics::TBitmap *Bitmap;
  if (Clipboard()->Provides(SDelphiBitmap)){
 Image1->Picture->Bitmap->Assign(Clipboard());
  }
}

The graphic on the clipboard could come from this application, or it could have been copied from another application, such as Microsoft Paint. You do not need to check the clipboard format in this case because the paste menu should be disabled when the clipboard does not contain a supported format.


Add comment


Security code
Refresh

Programming - C++Builder