RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TClipboard.GetAsHandle Method

Returns a Windows handle to data from the clipboard in the specified format.

Pascal
function GetAsHandle(Format: Word): THandle;
C++
__fastcall THandle GetAsHandle(Word Format);

GetAsHandle retrieves data from the clipboard using Windows handles. 

The handle returned by GetAsHandle is not owned by the application and is valid only as long as the clipboard is open. If you need it for longer, copy the data to another handle. 

The Format parameter can specify any of the formats listed under the HasFormat method. Applications can also define and register their own formats. See the Windows API Help for more information.  

C++ Examples: 

 

/*
The following code draws a bitmap image from the Clipboard 
when a button is pressed.  The SaveToClipboard button must 
be selected before the LoadFromClipboard button in order to
place the icon on the clipboard.
*/
#include <Clipbrd.hpp>

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  TClipboard *cb = Clipboard();
  if (cb->HasFormat(CF_BITMAP))
  {
    Graphics::TBitmap *Bitmap = new Graphics::TBitmap();
    try
    {
      Bitmap->LoadFromClipboardFormat(CF_BITMAP, cb->GetAsHandle(CF_BITMAP), 0);
      Canvas->Draw(5,5,Bitmap);
    }
    catch (...)
    {
      MessageBeep(0);
      ShowMessage("Error reading image from clipboard!");
    }
    delete Bitmap;
    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;
  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!");
  }
  delete Bitmap;
}

 

Delphi Examples: 

{
Open, Close, GetAsHandle example
The following code locks the memory for text on the Clipboard, then reads the text.
}

uses clipbrd;

procedure TForm1.Button1Click(Sender: TObject);
var
  MyHandle: THandle;
  TextPtr: PChar;
begin
  ClipBoard.Open;
try
  MyHandle := Clipboard.GetAsHandle(CF_TEXT);
  TextPtr := GlobalLock(MyHandle);
  ListBox1.Items.Add(StrPas(TextPtr));
  GlobalUnlock(MyHandle);
finally
  Clipboard.Close;
end;
end;
{
The following code draws a bitmap image from the Clipboard 
when a button is pressed.  The SaveToClipboard button must 
be selected before the LoadFromClipboard button in order to
place the icon on the clipboard.
}

procedure TForm1.Button1Click(Sender: TObject);
var
  Bitmap : TBitmap;
  s : string;
  Icon: TIcon;
begin
 Bitmap := TBitMap.Create;
 try
   Image1.Picture.RegisterClipboardFormat(cf_BitMap,TIcon);
   Bitmap.LoadFromClipBoardFormat(
     cf_BitMap,ClipBoard.GetAsHandle(cf_Bitmap),0);
   Canvas.draw(0,0,Bitmap);
 finally
   Bitmap.free;
   Clipboard.Clear;
 end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  MyFormat : Word;
  Bitmap : TBitMap;
  AData : THandle;
  APalette : HPALETTE;
begin
  Bitmap := TBitmap.Create;
  try
    Bitmap.LoadFromFile('c:\Program Files\Common Files\CodeGear Shared\Images\Splash\256color\factory.bmp');
    Bitmap.SaveToClipBoardFormat(
      MyFormat,
      AData,
      APalette);
    ClipBoard.SetAsHandle(MyFormat,AData);
  finally
    Bitmap.Free;
  end;
end;

 

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!