Avatar billede Slettet bruger
03. september 2002 - 20:06 Der er 2 løsninger

Drag filer ind i en listbox

Hvordan kan jeg flytte en fil fra windows til en listbox så hele stien til filen bliver addet til listboxen. Og hvis filtypen er noget bestemt, skal den gøre noget "ved siden af"?
Avatar billede dkn Nybegynder
03. september 2002 - 20:51 #1
unit dropfile;

interface

uses
  Windows, Messages, SysUtils, Classes,
  Graphics, Controls, Forms, Dialogs;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    // declare our DROPFILES message handler
    procedure AcceptFiles( var msg : TMessage );
      message WM_DROPFILES;
  end;

var
  Form1: TForm1;

implementation

uses
  ShellAPI;

{$R *.DFM}

procedure TForm1.AcceptFiles( var msg : TMessage );
const
  cnMaxFileNameLen = 255;
var
  i,
  nCount    : integer;
  acFileName : array [0..cnMaxFileNameLen] of char;
begin
  // find out how many files we're accepting
  nCount := DragQueryFile( msg.WParam,
                          $FFFFFFFF,
                          acFileName,
                          cnMaxFileNameLen );

  // query Windows one at a time for the file name
  for i := 0 to nCount-1 do
  begin
    DragQueryFile( msg.WParam, i,
                  acFileName, cnMaxFileNameLen );
   
    if extractfileext(acFileName) = '.txt' then
showmessage('txt fil: '+acFileName);

listbox1.items.add(acFileName);
  end;
  DragFinish( msg.WParam );
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  DragAcceptFiles( Handle, True );
end;

end.
Avatar billede hermandsen Juniormester
04. september 2002 - 08:57 #2
Den her har vist alt hvad du har brug for:

unit DragNDrop;

{ DropForm.Pas - written by Nikias Klohr for the
  "drag'n'drop with Delphi" - tuturial at
  www.SNTD.de
  E-Mail: Nikias@stnd.de
  use this unit wherever you like
  would be nice if you write me a mail if you use it in a "good" application !
}

interface
uses Windows,Messages,ShellApi,SysUtils;

// add "procedure DropFile(var message: TWMDropFiles); message WM_DROPFILES;"
// to the Form. Then call any code you need to from this procedure

// Also add "DragAcceptFiles(handle,true);" at FormCreate; Uses ShellApi

type

  TDropGotFileProc = function(FileName: String; Count: Integer): Boolean;

  function DropPoint(DropMsg: TWMDropFiles): TPoint;

  function DropFileCount(DropMsg: TWMDropFiles): Integer;

  function DropGetFile(DropMsg: TWMDropFiles): String; overload;
  // Gets the first file if there are more than one files added !
  function DropGetFile(DropMsg: TWMDropFiles; Index: Integer): String; overload;

  function DropGetFileExt(DropMsg: TWMDropFiles): String; overload;
  // Gets the first fileextension if there are more than one files added !
  function DropGetFileExt(DropMsg: TWMDropFiles; Index: Integer): String; overload;

  function DropDifferentExt(DropMsg: TWMDropFiles): Boolean;
  // Finds out if the dropped files all have the same extension
  procedure DropGetFiles(DropMsg: TWMDropFiles; GotFileProc: TDropGotFileProc); overload;
  // Calls GotFileProc for any file which was droped
  // If the result of GotFileProc is false then DropGetFiles stops calling it !
  procedure Dropped(DropMsg: TWMDropFiles);
  // Call this when you're finished with the drop operation !!!
implementation

function DropPoint(DropMsg: TWMDropFiles): TPoint;
begin
  DragQueryPoint(DropMsg.Drop, Result);
end;

function DropFileCount(DropMsg: TWMDropFiles): Integer;
begin
  Result := DragQueryFile(DropMsg.Drop, $FFFFFFFF, nil, 0);
end;

function DropGetFile(DropMsg: TWMDropFiles): String; overload;
begin
  Result := DropGetfile(DropMsg, 0);
end;

function DropGetFileExt(DropMsg: TWMDropFiles): String;
begin
  Result := ExtractFileExt(DropGetFile(DropMsg));
end;

function DropGetFileExt(DropMsg: TWMDropFiles; Index: Integer): String;
begin
  Result := ExtractFileExt(DropGetFile(DropMsg, Index));
end;

function DropDifferentExt(DropMsg: TWMDropFiles): Boolean;
var
  I: Integer;
  S: String;
begin
  Result := False;
  S := DropGetFileExt(DropMsg);
  for i := 1 to DropFileCount(DropMsg) -1 do
    if S <> DropGetFileExt(DropMsg, I) then
    begin
      Result := True;
      Exit;
    end;
end;

function DropGetFile(DropMsg: TWMDropFiles; Index: Integer): String; overload;
var
  P: PChar;
begin
  GetMem(P, 255);
  DragQueryFile(DropMsg.Drop, Index, P, 255);
  Result := P;
  FreeMem(P, 255);
end;


procedure DropGetFiles(DropMsg: TWMDropFiles; GotFileProc: TDropGotFileProc); overload;
var
  I: Integer;
begin
  for I := 0 to DropFileCount(DropMsg) -1 do
    if not GotFileProc(DropGetFile(DropMsg, I), I) then Exit;
end;

procedure Dropped(DropMsg: TWMDropFiles);
begin
  DragFinish(DropMsg.Drop);
end;

end.
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester