![]() |
Ordner/Dateinamen beim Ziehen in Prog übergeben
Hallo zusammen. Weiß schon, die Überschrift is nicht das wahre :oops: aber das is auch nicht leicht zu beschreiben. Also hier mein problem:
Ich möchte eine Art Bildvorschau machen. Dazu benötige ich allerdings den Ordner, in dem die Bilder sind. Jetzt möchte ich, dass der User den Ordner einfach auf das Programm fenster zieht, und dann steht der Pfad dieses Ordners in einem Edit/Label. Wer sich noch erinnern kann: Im DP - Ghost Thread hat Luckie (oder, war doch Luckie?) seinen Crypter gepostet. Da funktioniert das so. Vielleicht kann mir ja wer helfen. Danke und man liest sich, Stanlay. |
Grüss Dich,
das Zauberwort bei deinem Problem heißt Drack and Drop. :!: Muss mal ein bisschen im Forum danach suchen. quelltext hab ich leider gerade nicht zur verfügung. Sorry. :cry: Kaemmi, |
Danke für die schnelle Antwort! Das das in irgendeinem Sinn was mit Drag and Drop zu tun hat, hab ich mir gedacht. Mich würde interessieren:
a) in welchem Event des Form muss ich eine Procedure eintragen (OnEndDrag ?) und b) in welcher Variablen werden die übergebenen Daten gespeichert? |
Vielleicht hilft dir der Code von den Schweitzern:
Delphi-Quellcode:
{
This way you can drag and drop files to a specific control in a Delphi form. Just create a project and add a ListBox component to Form1.} { 1. First, a procedure to handle the message but without handling it. } interface procedure WMDROPFILES(var Msg: TMessage); implementation procedure TForm1.WMDROPFILES(var Msg: TWMDropFiles); var pcFileName: PChar; i, iSize, iFileCount: integer; begin pcFileName := ''; // to avoid compiler warning message iFileCount := DragQueryFile(Msg.wParam, $FFFFFFFF, pcFileName, 255); for i := 0 to iFileCount - 1 do begin iSize := DragQueryFile(Msg.wParam, i, nil, 0) + 1; pcFileName := StrAlloc(iSize); DragQueryFile(Msg.wParam, i, pcFileName, iSize); if FileExists(pcFileName) then AddFile(pcFileName); // method to add each file StrDispose(pcFileName); end; DragFinish(Msg.wParam); end; { 2. Second, a WindowProc method to replace ListBox1 WindowProc default method and a variable to store ListBox1 WindowProc default method. } interface procedure LBWindowProc(var Message: TMessage); implementation var OldLBWindowProc: TWndMethod; procedure TForm1.LBWindowProc(var Message: TMessage); begin if Message.Msg = WM_DROPFILES then WMDROPFILES(Message); // handle WM_DROPFILES message OldLBWindowProc(Message); // call default ListBox1 WindowProc method to handle all other messages end; {3. In Form1 OnCreate event, initialize all.} procedure TForm1.FormCreate(Sender: TObject); begin OldLBWindowProc := ListBox1.WindowProc; // store defualt WindowProc ListBox1.WindowProc := LBWindowProc; // replace default WindowProc DragAcceptFiles(ListBox1.Handle, True); // now ListBox1 accept dropped files end; {4. In Form1 OnDestroy event, uninitialize all. Not necesary but a good practice.} procedure TForm1.FormDestroy(Sender: TObject); begin ListBox1.WindowProc := OldLBWindowProc; DragAcceptFiles(ListBox1.Handle, False); end; {5. To complete source code, the AddFile method.} interface procedure AddFile(sFileName: string); implementation procedure TForm1.AddFile(sFileName: string); begin ListBox1.Items.Add(sFilename); end; {6. Do not forget to add ShellAPI unit to the uses clause. } Complete code unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) ListBox1: TListBox; procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); private { Private declarations } procedure WMDROPFILES(var Msg: TMessage); procedure LBWindowProc(var Message: TMessage); procedure AddFile(sFileName: string); public { Public declarations } end; var Form1: TForm1; implementation {$R *.DFM} uses ShellAPI; var OldLBWindowProc: TWndMethod; procedure TForm1.AddFile(sFileName: string); begin ListBox1.Items.Add(sFilename); end; procedure TForm1.FormCreate(Sender: TObject); begin OldLBWindowProc := ListBox1.WindowProc; // store defualt WindowProc ListBox1.WindowProc := LBWindowProc; // replace default WindowProc DragAcceptFiles(ListBox1.Handle, True); // now ListBox1 accept dropped files end; procedure TForm1.FormDestroy(Sender: TObject); begin ListBox1.WindowProc := OldLBWindowProc; DragAcceptFiles(ListBox1.Handle, False); end; procedure TForm1.LBWindowProc(var Message: TMessage); begin if Message.Msg = WM_DROPFILES then WMDROPFILES(Message); // handle WM_DROPFILES message OldLBWindowProc(Message); // call default ListBox1 WindowProc method to handle all other messages end; procedure TForm1.WMDROPFILES(var Msg: TMessage); var pcFileName: PChar; i, iSize, iFileCount: integer; begin pcFileName := ''; // to avoid compiler warning message iFileCount := DragQueryFile(Msg.wParam, $FFFFFFFF, pcFileName, 255); for i := 0 to iFileCount - 1 do begin iSize := DragQueryFile(Msg.wParam, i, nil, 0) + 1; pcFileName := StrAlloc(iSize); DragQueryFile(Msg.wParam, i, pcFileName, iSize); if FileExists(pcFileName) then AddFile(pcFileName); // method to add each file StrDispose(pcFileName); end; DragFinish(Msg.wParam); end; end. ![]() |
Moin Stanlay,
das Drag & Drop von Dateien funktioniert etwas anders. Such mal hier nach DragAcceptFiles da müsstest Du Beispiele finden, wie das eingebunden wird. |
Danke erstmal. Das muss ich aber jetz mal verarbeiten...:wink: Ohne dir oder den Schweitzern zu nahe zu treten zu wollen: Geht das auch ein bisschen kürzer. Ich dachte, das wird vielleicht auch in ParamStr gespeichert oder so. Auf jeden Fall nochmal Danke!
|
Zitat:
Ich möchte keineswegs ne Datei öffnen oder sowas. Ich möchte nur den Pfad eines Ordners oder einer Datei, die ich auf das Form gezogen hab kriegen. Oder fällt das auch unter die Kategorie DragAcceptFiles? *kenntsichmitdraganddropgarnichtaus:oops:* |
Moin Stanlay,
Du bekommst auch nur die Pfade (wie z.B. beim OpenDialog) Was Du dann damit anstellst ist Deine Sache. Such Dir besser mal ein Beispiel von hier, die dürften kürzer sein ;-) |
Ich hab bereits gesucht. Aber warscheinlich bin ich zu dumm um das umzusetzen. Könntest du mir vielleicht ein CodeBeispiel geben?
|
Moin Stanlay,
das Beispiel ist doch soweit vollständig und kommentiert? Wo hakt's denn? |
Alle Zeitangaben in WEZ +1. Es ist jetzt 16:07 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz