Einzelnen Beitrag anzeigen

schwa226

Registriert seit: 4. Apr 2008
400 Beiträge
 
#9

Re: Drag&Drop einer Datei in TreeView

  Alt 25. Jan 2010, 15:09
Zitat von himitsu:
heißt doch wohl, daß die Drag&Drop-Nachrichten an die ListView gesendet werden sollen
und nicht an die Form, wo aktuell die Nachrichtenbehandlung implementiert wurde?

Oder man kann diese Art nicht verwenden und muß sich an anderer Stelle in die Message-Behandlung einschalten.
Bei ListBox1.Handle geht es das die Message Funktion in der MainForm die WM_DROPFILES Nachricht bekommt.
Bei TreeView1.Handle schaltet zwar der Cursor um jedoch kommt die Nachricht WM_DROPFILES nicht.

Aber es war der richtige Tipp!

So habe ich es nun hinbekommen:

Delphi-Quellcode:
type
  TMainForm= class(TForm)
    TreeView1 : TTreeView;
    .....

  private
    { Private-Deklarationen }
    OldLBWindowProc: TWndMethod;
  public
    { Public-Deklarationen }
    procedure WMDropFiles(var Msg: TMessage);
  end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  //save old windoproc:
  OldLBWindowProc := TreeView1.WindowProc;
  TreeView1.WindowProc := WMDropFiles;
  //enable drag and drop of treeview:
  DragAcceptFiles(TreeView1.Handle, True);

end;

procedure TMainForm.OnClose(Sender: TObject; var Action: TCloseAction);
begin
  //disable drag and drop of treeview:
  DragAcceptFiles(TreeView1.Handle, False);
end

//drag and drop:
procedure TMainForm.WMDropFiles(var Msg: TMessage);
var
  DropH: HDROP; // drop handle
  DroppedFileCount: Integer; // number of files dropped
  FileNameLength: Integer; // length of a dropped file name
  FileName: string; // a dropped file name
  I: Integer; // loops thru all dropped files
  DropPoint: TPoint; // point where files dropped
  AnItem : TTreeNode;
begin

  // Store drop handle from the message
  case Msg.Msg of

    WM_DROPFILES : begin
        DropH := Msg.WParam;
        try
          // Optional: Get point at which files were dropped
          DragQueryPoint(DropH, DropPoint);
          // ... do something with drop point here

          AnItem := TreeView1.GetNodeAt(DropPoint.X, DropPoint.Y) ;
          //only handle the drop if node was under drop mouse:
          if AnItem <> nil then
          begin
            // Get count of files dropped
            DroppedFileCount := DragQueryFile(DropH, $FFFFFFFF, nil, 0);
            // Get name of each file dropped and process it
            for I := 0 to Pred(DroppedFileCount) do
            begin
              // get length of file name
              FileNameLength := DragQueryFile(DropH, I, nil, 0);
              // create string large enough to store file
              // (Delphi allows for #0 terminating character automatically)
              SetLength(FileName, FileNameLength);
              // get the file name
              DragQueryFile(DropH, I, PWideChar(FileName), FileNameLength + 1);
              // process file name (application specific)
              // ... processing code here
            end;
          end;
        finally
          // Tidy up - release the drop handle
          // don't use DropH again after this
          DragFinish(DropH);
        end;
        // Note we handled message
        Msg.Result := 0;
    end; // WM_DROPFILES : begin
    //forward message to original WndProc:
    else OldLBWindowProc(Msg);
  end; //case Msg.Msg of
end;
Damit geht das Drag and Drop Event mit TreeView. Scheint zu gehen, bin mir aber nicht sicher ob es auch richtig "Save" ist.
Delphi 2010, Update 4 & 5
  Mit Zitat antworten Zitat