Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to accept dropped files from the explorer Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
18-Jun-03
Category
Files Operation
Language
Delphi 4.x
Views
115
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Pablo Reyes

This way you can drag and drop files to a specific control in a Delphi form

Answer:

Just create a project and add a ListBox component to Form1. 

1. First, a procedure to handle the message but without handling it. 

1   interface
2   
3   procedure WMDROPFILES(var Msg: TMessage);
4   
5   implementation
6   
7   procedure TForm1.WMDROPFILES(var Msg: TMessage);
8   var
9     pcFileName: PChar;
10    i, iSize, iFileCount: integer;
11  begin
12    pcFileName := ''; // to avoid compiler warning message
13    iFileCount := DragQueryFile(Msg.WParam, $FFFFFFFF, pcFileName, 255);
14    for i := 0 to iFileCount - 1 do
15    begin
16      iSize := DragQueryFile(Msg.wParam, 0, nil, 0) + 1;
17      pcFileName := StrAlloc(iSize);
18      DragQueryFile(Msg.WParam, i, pcFileName, iSize);
19      if FileExists(pcFileName) then
20        AddFile(pcFileName); // method to add each file
21      StrDispose(pcFileName);
22    end;
23    DragFinish(Msg.WParam);
24  end;


2. Second, a WindowProc method to replace ListBox1 WindowProc default method and a 
variable to store ListBox1 WindowProc default method. 

25  interface
26  
27  procedure LBWindowProc(var message: TMessage);
28  
29  implementation
30  
31  var
32    OldLBWindowProc: TWndMethod;
33  
34  procedure TForm1.LBWindowProc(var message: TMessage);
35  begin
36    if message.Msg = WM_DROPFILES then
37      WMDROPFILES(message); // handle WM_DROPFILES message
38    OldLBWindowProc(message);
39      // call default ListBox1 WindowProc method to handle all other messages
40  end;


3. In Form1 OnCreate event, initialize all. 

41  procedure TForm1.FormCreate(Sender: TObject);
42  begin
43    OldLBWindowProc := ListBox1.WindowProc; // store defualt WindowProc
44    ListBox1.WindowProc := LBWindowProc; // replace default WindowProc
45    DragAcceptFiles(ListBox1.Handle, True); // now ListBox1 accept dropped files
46  end;


4. In Form1 OnDestroy event, uninitialize all. Not necesary but a good practice. 

47  procedure TForm1.FormDestroy(Sender: TObject);
48  begin
49    ListBox1.WindowProc := OldLBWindowProc;
50    DragAcceptFiles(ListBox1.Handle, False);
51  end;


5. To complete source code, the AddFile method. 

52  interface
53  
54  procedure AddFile(sFileName: string);
55  
56  implementation
57  
58  procedure TForm1.AddFile(sFileName: string);
59  begin
60    ListBox1.Items.Add(sFilename);
61  end;


6. Do not forget to add ShellAPI unit to the uses clause. 


Component Download: http://www.delphi3000.com/redirect.asp?Link=../article/2135/DroppedFiles.zip

			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC