Cutting Graphics to the Clipboard

From RAD Studio
Jump to: navigation, search

Go Up to Using the Clipboard with Graphics


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:

procedure TForm1.Cut1Click(Sender: TObject);
var
  ARect: TRect;
begin
  Copy1Click(Sender); { copy picture to clipboard }
  with Image.Canvas do
  begin
    CopyMode := cmWhiteness; { copy everything as white }
    ARect := Rect(0, 0, Image.Width, Image.Height); { get bitmap rectangle }
    CopyRect(ARect, Image.Canvas, ARect); { copy bitmap over itself }
    CopyMode := cmSrcCopy; { restore normal mode }
  end;
end;
void __fastcall TForm1::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
}

See Also