Einzelnen Beitrag anzeigen

Benutzerbild von Luckie
Luckie

Registriert seit: 29. Mai 2002
37.621 Beiträge
 
Delphi 2006 Professional
 
#1

Drag & Drop vom Explorer auf unterschiedliche Steuerelem

  Alt 8. Mär 2010, 08:41
Mit folgendem Beispiel kann man Dateien aus dem Explorer auf unterschiedliche Steuerelemente seines Formulares ziehen:
Delphi-Quellcode:
unit Unit6;

interface

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

type
  TForm6 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    procedure FormCreate(Sender: TObject);
  private
    { Private-Deklarationen }
    procedure AppMessage(var Msg: Tmsg; var Handled: Boolean);
    function IsDropPointInside(const aDropPoint: TPoint; const aControl: TControl): Boolean;
  public
    { Public-Deklarationen }
  end;

var
  Form6: TForm6;

implementation

{$R *.dfm}

uses
  ShellAPI;

procedure TForm6.AppMessage(var Msg: Tmsg; var Handled: Boolean);
const
  BufferLength: word = 255;
var
  DroppedFilename: string;
  FileIndex: Word;
  QtyDroppedFiles: Word;
  pDroppedFilename: array[0..255] of Char;
  DropPoint: TPoint;
begin
  if Msg.Message = WM_DROPFILES then
  begin
    FileIndex := $FFFF;
    QtyDroppedFiles := DragQueryFile(Msg.WParam, FileIndex, pDroppedFilename, BufferLength);
    for FileIndex := 0 to (QtyDroppedFiles - 1) do
    begin
      DragQueryFile(Msg.WParam, FileIndex, pDroppedFilename, BufferLength);
      DroppedFilename := StrPas(pDroppedFilename);
      DropPoint := Msg.pt;
      if IsDropPointInside(DropPoint, Edit1) then
        Edit1.Text := DroppedFilename
      else if IsDropPointInside(DropPoint, Edit2) then
        Edit2.Text := DroppedFilename;
    end;
    DragFinish(Msg.WParam);
    Handled := true;
  end;
end;

procedure TForm6.FormCreate(Sender: TObject);
begin
  DragAcceptFiles(Edit1.Handle, true);
  DragAcceptFiles(Edit2.Handle, true);
  Application.OnMessage := AppMessage;
end;

function TForm6.IsDropPointInside(const aDropPoint: TPoint; const aControl: TControl): Boolean;
begin
  Result := PtInRect(aControl.ClientRect, aControl.ScreenToClient(aDropPoint));
end;

end.
Danke auch an Dezipaitor für seine Hilfe.

Stichwörter: WM_DROPPFILES, Drag and drop, DragAcceptFiles
Michael
Ein Teil meines Codes würde euch verunsichern.
  Mit Zitat antworten Zitat