Clipboard and Custom format lazarus / 21-General ---------------------------------------------------------------------- shyub · 2023-10-10 10:36 I'm writing an editor that saves data in an array: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TVertex2s = record // Координаты вершин в миллиметрах.    X, Y: Single;  end; var  tvM: Array of TVertex2s;Delphi uses the SetAsHandle(Format: integer; Value: THandle) procedure to develop a custom format, which Lazarus does not: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i      var     hBuf: THandle;       Bufptr: Pointer;       MStream: TMemoryStream;     begin       MStream := TMemoryStream.Create;       try       { write your data to the stream }         hBuf := GlobalAlloc(GMEM_MOVEABLE, MStream.Size);         try           BufPtr := GlobalLock(hBuf);           try             Move(MStream.Memory^, BufPtr^, MStream.Size);             Clipboard.SetAsHandle(CF_MYFORMAT, hBuf);           finally             GlobalUnlock(hBuf);           end;         except           GlobalFree(hBuf);           raise;       end;       finally         MStream.Free;       end;     end;   ...........   ...........     var       hBuf: THandle;       BufPtr: Pointer;       MStream: TMemoryStream;     begin       hBuf := Clipboard.GetAsHandle(CF_MYFORMAT);       if hBuf <> 0 then        begin         BufPtr := GlobalLock(hBuf);         if BufPtr <> nil then         try           MStream := TMemoryStream.Create;           try             MStream.WriteBuffer(BufPtr^, GlobalSize(hBuf));           MStream.Position := 0;           { read your data from the stream }           finally             MStream.Free;           end;       finally           GlobalUnlock(hBuf);         end;       end;     end;How can I exchange data in my format via the clipboard? ---------------------------------------------------------------------- wp · 2023-10-10 10:57 I once did this for fpspreadsheet to put worksheet cells in the clipboard in multiple spreadsheet formats: - Declare constant(s) for the new clipboard format: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  cfBiff8Format: Integer = 0;  cfBiff5Format: Integer = 0;  cfHTMLFormat: Integer = 0;  cfTextHTMLFormat: Integer = 0;  cfCSVFormat: Integer = 0; - Register these formats very early (I did it in the initialization section of the unit): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   { Clipboard formats }  cfBiff8Format := RegisterclipboardFormat('Biff8');  cfBiff5Format := RegisterClipboardFormat('Biff5');  cfHTMLFormat := RegisterClipboardFormat('HTML Format');  cfTextHTMLFormat := RegisterClipboardFormat('text/html');  cfCSVFormat := RegisterClipboardFormat('CSV'); - Write a procedure CopyToClipboard (or similar name) which writes the data in the required format to a memory stream and uses this along with the format constant in a call to Clipboard.AddFormat(cfXXXX, stream). The following code puts the selected cells in BIFF8, BIFF5, HTML, CSV and text formats into the clipboard. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TsWorkbookSource.CopyCellsToClipboard;var  sel: TsCellRangeArray;  stream: TStream;  savedCSVParams: TsCSVParams;  s: String;   procedure CopyToClipboard(AStream: TStream; AFileFormat: TsSpreadsheetFormat;  AClipboardFormat: Integer; AParams: TsStreamParams = []);  begin    if AClipboardFormat = 0 then      exit;    FWorkbook.CopyToClipboardStream(AStream, AFileFormat, AParams);    Clipboard.AddFormat(AClipboardFormat, AStream);    (AStream as TMemoryStream).Clear;  end; begin  sel := FWorksheet.GetSelection;  if Length(sel) = 0 then    exit;   Clipboard.Open;  try    Clipboard.Clear;     stream := TMemoryStream.Create;    try      // Write BIFF8 format      CopyToClipboard(stream, sfExcel8, cfBiff8Format);       // Then write BIFF5 format      CopyToClipboard(stream, sfExcel5, cfBiff5Format);       // Then write Windows HTML format      {$IFDEF MSWINDOWS}      CopyToClipboard(stream, sfHTML, cfHtmlFormat, [spWindowsClipboardHTML]);      {$ENDIF}       // Write standard html format (MIME-type "text/html")      CopyToClipboard(stream, sfHTML, cfTextHTMLFormat);       // Then write CSV format      savedCSVParams := CSVParams;      CsvParams.Delimiter := ';';      CsvParams.AutoDetectNumberFormat := false;      CsvParams.SheetIndex := FWorkbook.GetWorksheetIndex(FWorkbook.ActiveWorksheet);      CopyToClipboard(stream, sfCSV, cfCSVFormat);       // Finally write TEXT format      if FWorksheet.GetSelectionCount = 1 then begin      s := FWorksheet.ReadAsText(FWorksheet.ActiveCellRow, FWorksheet.ActiveCellCol);        if s <> '' then begin          stream.Write(s[1], Length(s));          stream.WriteByte(0);          stream.Position := 0;          Clipboard.AddFormat(CF_TEXT, stream);        (stream as TMemoryStream).Clear;        end;      end else    begin        CsvParams.Delimiter := #9;        CopyToClipboard(stream, sfCSV, CF_TEXT);        CSVParams := savedCSVParams;      end;       // To do: XML format, ods format    finally      stream.Free;    end;  finally    Clipboard.Close;  end;end; - Likewise, in reverse order, write a PasteFromClipboard. If you want to study the code in more details have a look at the unit FPSpreadsheetCtrls of the FPSpreadsheet package (https://sourceforge.net/p/lazarus-ccr/svn/HEAD/tree/components/fpspre adsheet/source/visual/fpspreadsheetctrls.pas, https://sourceforge.net/p/lazarus-ccr/svn/HEAD/tree/components/fpsprea dsheet/source/common/fpspreadsheet_clipbrd.inc). ---------------------------------------------------------------------- Remy Lebeau · 2023-10-10 20:50 Quote from: shyub on October 10, 2023, 12:36:08 pmDelphi uses the SetAsHandle(Format: integer; Value: THandle) procedure to develop a custom format, which Lazarus does not ... How can I exchange data in my format via the clipboard? Simply use the Win32 Clipboard API directly, namely RegisterClipboardFormat(), OpenClipboard(), SetClipboardData(), and GetClipboardData(). ---------------------------------------------------------------------- shyub · 2023-10-11 11:41 I tried via WinAPI, but it didn't work... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.FormCreate(Sender: TObject);begin  cfBuf8Format:=0;  cfBuf8Format:=RegisterclipboardFormat('Biff8');  SetLength(M, 4);  M[0].X:=1.23;  M[0].Y:=2.34;  M[1].X:=3.45;  M[1].Y:=4.54;  M[2].X:=5.67;  M[2].Y:=6.78;  M[3].X:=8.89;  M[3].Y:=8.91;end; procedure TForm1.CopyToClipboard();var  Data : THandle;  DataPtr: Pointer;begin  try    Data := GlobalAlloc(GMEM_MOVEABLE, SizeOf(Tmy)*Length(M));     DataPtr := GlobalLock(Data);    Move(M, DataPtr^, SizeOf(Tmy)*Length(M));    try      OpenClipboard(cfBuf8Format);       EmptyClipboard();       SetClipboardData(cfBuf8Format, Data);    finally      CloseClipboard;   end;    GlobalUnlock(Data);  except    GlobalFree(Data);  end;end; procedure TForm1.ReadClipboard();var  Data : THandle;  MStream: TMemoryStream;  BufPtr: Pointer;  A: array of Tmy;  n, d: Integer;begin  if not IsClipboardFormatAvailable(cfBuf8Format) then exit;  if not OpenClipboard(cfBuf8Format) then exit;  Data:=GetClipboardData(cfBuf8Format);  try    BufPtr:=GlobalLock(Data);    MStream:=TMemoryStream.Create;    if MStream<>nil then begin      MStream.WriteBuffer(BufPtr^, GlobalSize(Data));      MStream.Position:=0;      d:=SizeOf(BufPtr);    d:=d div 8;//      for .....    end;  finally    MStream.Free;    GlobalUnlock(Data);    CloseClipboard();  end;end;Doesn't see my type. ---------------------------------------------------------------------- wp · 2023-10-11 12:14 The attached mini project copies the four double points to the clipboard and is able to read it back. BUT: No other application will be able the paste them because this is a very special format for which no application has a reader. If you want to copy/paste Excel data (which I assume because you used the name 'BIFF8' for the clipboard format) you must prepare a complete Excel stream in BIFF8 format and pass it to the AddFormat method of the clipboard. ---------------------------------------------------------------------- Remy Lebeau · 2023-10-11 17:24 Quote from: shyub on October 11, 2023, 01:41:14 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Move(M, DataPtr^, SizeOf(Tmy)*Length(M)); This is wrong.  M is a dynamic array, which means it is a pointer to data elsewhere in memory.  Move() takes an untyped const as input, which means it takes a reference to whatever variable you pass in.  So, you are actually moving the array pointer itself into the allocated memory, you are not moving the data that the array pointer is pointing at.  You need to dereference the array pointer when passing it to Move(), just ass you are doing with DataPtr, eg: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Move(M[0]{or: Pointer(M)^}, DataPtr^, SizeOf(Tmy)*Length(M)); Quote from: shyub on October 11, 2023, 01:41:14 pmCode: [Select]OpenClipboard(cfBuf8Format); This is also wrong.  OpenClipboard() takes an HWND window handle, not a clipboard format integer.  Use the TForm.Handle property when opening the clipboard, eg: Code: [Select]OpenClipboard(Handle); Also, you are not checking the API calls for failures correctly/at all. Try something more like this: Code: [Select]procedure TForm1.FormCreate(Sender: TObject); begin   cfBuf8Format := RegisterClipboardFormat('Biff8');   SetLength(M, 4);   M[0].X := 1.23;  M[0].Y := 2.34;   M[1].X := 3.45;  M[1].Y := 4.54;   M[2].X := 5.67;  M[2].Y := 6.78;   M[3].X := 8.89;  M[3].Y := 8.91; end;     procedure TForm1.CopyToClipboard(); var   Data : THandle;   DataPtr: Pointer; begin   if cfBuf8Format = 0 then Exit;   Data := GlobalAlloc(GMEM_MOVEABLE, SizeOf(Tmy)*Length(M));   if Data = 0 then RaiseLastOSError();   try     DataPtr := GlobalLock(Data);     if DataPtr = nil then RaiseLastOSError();     try       Move(M[0]{or: Pointer(M)^}, DataPtr^, SizeOf(Tmy)*Length(M));     finally       GlobalUnlock(Data);     end;     if not OpenClipboard(Handle) then RaiseLastOSError();     try       if not EmptyClipboard() then RaiseLastOSError();       if not SetClipboardData(cfBuf8Format, Data) then RaiseLastOSError();     finally       CloseClipboard();     end;   except     GlobalFree(Data);     raise;   end; end;     procedure TForm1.ReadClipboard(); var   Data : THandle;   BufPtr: Pointer;   A: array of Tmy;   n: Integer; begin   if (cfBuf8Format = 0) or (not IsClipboardFormatAvailable(cfBuf8Format)) then Exit;   if not OpenClipboard(Handle) then RaiseLastOSError();   try     Data := GetClipboardData(cfBuf8Format);     if Data = 0 then RaiseLastOSError();     BufPtr := GlobalLock(Data);     if BufPtr = nil then RaiseLastOSError();     try       n := GlobalSize(Data) div SizeOf(Tmy);       SetLength(A, n);       Move(BufPtr^, A[0]{or: Pointer(A)^}, SizeOf(Tmy)*n);     finally       GlobalUnlock(Data);     end;   finally     CloseClipboard();   end;   // for ..... end;