2011. február 13., vasárnap

Work around for bug in RichEdit when using PasteFromClipboard, while PlainText = True


Problem/Question/Abstract:

If you work with a RichEdit and you set the property PlainText to True, then you can get formatted text into your RichEdit when you use:

RichEdit1.PasteFromClipboard;

Because you set PlainText to True, you are not able to "unformat" this text again. A work around for this problem is using the procedure below. (This is a well known bug! On the site of Borland you'll find another - but very complicated - work around at: http://codecentral.borland.com/codecentral/ccweb.exe/listing?id=13345)

Answer:

//----------------------------------------------------------------------
procedure TForm1.EditPasteClick(Sender: TObject);

var
  h: THandle;
  pchP: PChar;
  asS0: ANSIstring;

begin
  ClipBoard.Open;
  try
    h := Clipboard.GetAsHandle(CF_TEXT);
    pchP := GlobalLock(h);
    asS0 := StrPas(pchP);
    GlobalUnlock(h);
  finally
    Clipboard.Close;
  end;
  if Length(asS0) > 0 then
    RichEdit1.SelText := asS0; // Copy in where the cursor / caret is.
end;
//----------------------------------------------------------------------

Nincsenek megjegyzések:

Megjegyzés küldése