Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   Delphi Clipboard und Rich Text Format (https://www.delphipraxis.net/147173-clipboard-und-rich-text-format.html)

PeterPanino 3. Feb 2010 02:25


Clipboard und Rich Text Format
 
Hallo,

ich möchte feststellen, ob sich in der Zwischenablage das Rich Text Format befindet. Leider scheint es keine vor-definierte Konstante für das Clipboard Rich Text Format zu geben (wie etwa CF_TEXT oder CF_BITMAP). Deshalb habe ich einen Text aus OO Writer in die Zwischenablage kopiert, und dann den Clipboard Explorer von shmia verwendet, um mir den Integer-Wert für das Clipboard Rich Text Format anzeigen zu lassen:



Leider musste ich dann feststellen, dass an einem anderen Tag ein anderer Wert für das Clipboard Rich Text Format angezeigt wurde, nämlich: 49280. Der Wert für das Clipboard Rich Text Format scheint also keine Konstante zu sein, sondern ist womöglich davon abhängig, welches Programm etwas im Rich Text Format in die Zwischenablage kopiert? Kann das jemand bestätigen?

Natürlich kann ich (bzw. mein Programm) auch damit leben, dass das Clipboard Rich Text Format keine Konstante ist, sondern jedes mal den Wert abfragen, wenn ein Clipboard-Ereignis eintritt, z.B.:

Delphi-Quellcode:
  uses Clipbrd, Windows;
   
  function My_GetClipBoardFormatName(format: Word): string;
  var
    r : Integer;
  begin
    SetLength(Result, 256);
    r := GetClipboardFormatName(format, PChar(Result), Length(Result));
    (*if r = 0 then
       RaiseLastWin32Error;*)
    SetLength(Result, r);
  end;

  procedure GetCBFormats;
  var
    f: Integer;
    AFormat: string;
  begin
    CBFormats.Clear;
    for f := 0 to Clipboard.FormatCount - 1 do
    begin
      AFormat := My_GetClipBoardFormatName(Clipboard.Formats[f]);
      if AFormat <> '' then
        CBFormats.Add(AFormat);
    end;
  end;

  function MyClipboardHasFormat(const CFormat: string): Boolean;
  var
    cf: Integer;
  begin
    Result := False;
    for cf := 0 to CBFormats.Count - 1 do
    begin
      if CompareText(CFormat, CBFormats[cf]) = 0 then
      begin
        Result := True;
        BREAK;
      end;
    end;
  end;

  // Anwendung:
  CBFormats := TStringList.Create;
  GetCBFormats;
  if MyClipboardHasFormat('Rich Text Format') then
  // ...
Aber vielleicht kennt jemand eine andere Methode (die einen Konstanten-Wert verwendet), um festzustellen, ob sich das Rich Text Format im Clipboard befindet?

MacGuyver 4. Feb 2010 21:38

Re: Clipboard und Rich Text Format
 
Moin auch :hi:

Das brauchst du so nicht. Am Einfachsten nimmst du eine globale Variable und rufst RegisterClipboardFormat aus Windows.pas auf.

Auszug aus der Onlinehilfe
Zitat:

The RegisterClipboardFormat function registers a new clipboard format. This format can then be used as a valid clipboard format.

If a registered format with the specified name already exists, a new format is not registered and the return value identifies the existing format. This enables more than one application to copy and paste data using the same registered clipboard format. Note that the format name comparison is case-insensitive.
Und jetzt hast du quasi deine "Konstante". Wenn dein Programm vor allen anderen Anwendungen schon das Format registriert, nehmen die anderen den Wert, den dein Programm vorgibt.

PeterPanino 4. Feb 2010 23:12

Re: Clipboard und Rich Text Format
 
Zitat:

Zitat von MacGuyver
Wenn dein Programm vor allen anderen Anwendungen schon das Format registriert, nehmen die anderen den Wert, den dein Programm vorgibt.

Danke für den Tipp. Aber was ist, wenn schon ein anderes Programm diesen Wert vorgibt? Dann habe ich praktisch 2 oder mehr Rich Text Formate im Clipboard und erhalte so nicht den Rich Text, den ein anderes Programm in das Clipboard kopiert.

Etwas wundert mich aber schon ein wenig: In Windows sind für alle möglichen exotischen Clipboard-Formate Konstanten vor-definiert, nur für das äußerst wichtige Rich Text Format gibt es keine vor-definierte Konstante. Ist da etwas Wichtiges vergessen worden? Edit: (Die einzige vor-definierte Konstante scheint die Zeichenfolge 'Rich Text Format' ein, da alle Programme, die Rich Text in das Clipboard kopieren, sich daran halten).

MacGuyver 4. Feb 2010 23:17

Re: Clipboard und Rich Text Format
 
Zitat:

Dann habe ich praktisch 2 oder mehr Rich Text Formate im Clipboard
Nein, dann bekommst du den Wert, den die andere Anwendung auch verwendet.

Die Konstanten sind sicherlich aus der Anfangszeit, neuere muss man eben registrieren. So haben die das auf jeden Fall zukunftssicher gestaltet.

PeterPanino 5. Feb 2010 00:06

Re: Clipboard und Rich Text Format
 
Zitat:

Zitat von MacGuyver
Nein, dann bekommst du den Wert, den die andere Anwendung auch verwendet.

Kannst du ein Code-Beispiel für diese Behauptung zeigen?

Luckie 5. Feb 2010 07:10

Re: Clipboard und Rich Text Format
 
Wieso Beweis für seine Behauptung? Glaubst du dem Windows SDK nicht?
Zitat:

a new format is not registered and the return value identifies the existing format.
Einfach registrieren und wenn es schon registriert ist, bekommst du die ID zurück. Wo ist jetzt dein Problem?


Alle Zeitangaben in WEZ +1. Es ist jetzt 17:28 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz