[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 659
  • Last Modified:

Decoding clipboard for jpeg and multiple selections

I need a bit of help here in sorting out the clipboard with multiple, including non standard images.

What I want to do is allow the user to select multiple "images" from Explorer, copy and then paste into my app. More specificallly, paste the info into my app so that I can then decide what to do with it.

To start with, suppose I have a single jpeg file selected and copied to the clipboard.  The code below
says that the clipboard contains  9 objects

DataObject
Shell IDList Array
CF_HDROP (Win 95)
DataObjectAttributes
Preferred DropEffect
Shell Object Offsets
FileName
FileNameW
Ole Private Data

- so, how can I interpret this lot as meaning that the clipboard contains a single jpeg filename, and how do I then go get the filename?


If I select and copy multiple objects, I get something quite similar (only 8 entries this time)
DataObject
Shell IDList Array
CF_HDROP (Win 95)
DataObjectAttributes
Preferred DropEffect
FileName
FileNameW
Ole Private Data

Where do I go from here?





=============================================================

procedure TForm1.BtnShowFormatsClick(Sender: TObject);
{
  http://www.efg2.com/Lab/Library/UseNet/2001/1124.txt
}
Var
  buf: Array [0..60] of Char;
  n  : Integer;
  fmt: Word;
  name: String[30];
begin
  dsmemo1.Clear;
  for n := 0 to Clipboard.FormatCount-1 do begin
    fmt := Clipboard.Formats[n];
    If GetclipboardFormatName( fmt, buf, Pred(Sizeof(buf))) <> 0 Then
      memo1.Lines.Add( StrPas( buf ))
    Else Begin
      Case fmt of
          1: name := 'CF_TEXT';
          2: name := 'CF_BITMAP';
          3: name := 'CF_METAFILEPICT';
          4: name := 'CF_SYLK';
          5: name := 'CF_DIF';
          6: name := 'CF_TIFF';
          7: name := 'CF_OEMTEXT';
          8: name := 'CF_DIB';
          9: name := 'CF_PALETTE';
         10: name := 'CF_PENDATA';
         11: name := 'CF_RIFF';
         12: name := 'CF_WAVE';
         13: name := 'CF_UNICODETEXT';
         14: name := 'CF_ENHMETAFILE';
         15: name := 'CF_HDROP (Win 95)';
         16: name := 'CF_LOCALE (Win 95)';
         17: name := 'CF_MAX (Win 95)';
      $0080: name := 'CF_OWNERDISPLAY';
      $0081: name := 'CF_DSPTEXT';
      $0082: name := 'CF_DSPBITMAP';
      $0083: name := 'CF_DSPMETAFILEPICT';
      $008E: name := 'CF_DSPENHMETAFILE';
      $0200..$02FF: name := 'private format';
      $0300..$03FF: name := 'GDI object';
     Else
       name := 'unknown format';
     End;
     memo1.Lines.Add( name );
   end;
  end;
end;
0
Mutley2003
Asked:
Mutley2003
  • 3
1 Solution
 
CynnaCommented:
OK, "GetClipboardFiles" will get you file names of the files copied in Windows Explorer.
Demonstration of how to use it assumes you placed Memo1 and Button1 on Form1. So do the following:

1. Add "clipbrd, shellapi" to your uses list
2. Place Memo1 and Button1 on your form
3. Copy/paste code bellow
4  Run the program

5. Go to Windows Explorer and copy few files
6. Return to our demo and click on Button1 - Memo1 should display file names that you copied in step 5.


-------- CODE --------

type TFileList = Array of String;

function GetClipboardFiles(var FileList: TFileList): Boolean;  // uses clipbrd, shellapi;
var f: THandle;
    buffer: Array [0..MAX_PATH] of Char;
    i, numFiles: Integer;
begin
  Result:=FALSE;
  Clipboard.Open;
  try
    if not Clipboard.HasFormat(CF_HDROP) then Exit;
    f:= Clipboard.GetAsHandle(CF_HDROP);
    If f <> 0 then begin
       with TStringList.Create do begin
            numFiles := DragQueryFile( f, $FFFFFFFF, nil, 0 );
            Form1.memo1.Clear;
            for i:= 0 to numfiles - 1 do begin
              buffer[0] := #0;
              DragQueryFile( f, i, buffer, sizeof(buffer));
              Add(buffer);
            end;
            SetLength(FileList, Count);
            for i:=0 to Count-1 do FileList[i]:=Strings[i];
      end;
      Result:=TRUE;
    end;
  finally
    Clipboard.close;
  end;
end;

// --------------- DEMO ------------------

procedure TForm1.Button1Click(Sender: TObject);
var Files: TFileList;
    i: Integer;
begin
  if GetClipboardFiles(Files) then begin
     for i:=0 to High(Files) do Memo1.Lines.Add(Files[i]);
  end
  else ShowMessage('No files copied!');
end;

0
 
CynnaCommented:
Oops, small mistake - delete  "Form1.memo1.Clear;" line inside GetClipboardFiles function.
0
 
Mutley2003Author Commented:
worked like a dream, thank you
0
 
CynnaCommented:
You are welcome ;)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now