如何使LISTVIEW响应DRAG事件?(50分)

  • 主题发起人 主题发起人 gmc007
  • 开始时间 开始时间
G

gmc007

Unregistered / Unconfirmed
GUEST, unregistred user!
dragacceptfiles(form1.handle,true)可以使窗体响应,但<br>dragacceptfiles(listview1.handle,true)却不能使LISTVIEW响应,<br>为什么呢?<br>如何解决?!!
 
unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, ComCtrls,shellapi;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; ListView1: TListView;<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; procedure AppMessage(var Msg: TMsg; var Handled: Boolean);<br><br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>//file://设置需要处理文件WM_DROPFILES拖放消息<br>DragAcceptFiles(ListView1.Handle, TRUE);<br>//file://设置AppMessage过程来捕获所有消息<br>Application.OnMessage := AppMessage;<br><br>end;<br>procedure TForm1.AppMessage(var Msg: TMsg; var Handled: Boolean); <br>var<br>nFiles, I: Integer;<br>Filename: string;<br>ListItem: TListItem;<br>begin<br>//<br>// 注意!所有消息都将通过这里!<br>// 不要在此过程中编写过多的或者需要长时间操作的代码,否则将影响程序的性能 <br>// <br>// 判断是否是发送到ListView1的WM_DROPFILES消息 <br>if (Msg.message = WM_DROPFILES) and (msg.hwnd = ListView1.Handle) then <br>begin <br>// 取dropped files的数量 <br>nFiles := DragQueryFile (Msg.wParam, $FFFFFFFF, nil, 0); <br>// 循环取每个拖下文件的全文件名 <br>try <br>for I := 0 to nFiles - 1 do <br>begin<br>// 为文件名分配缓冲 allocate memory <br>SetLength (Filename, 80); <br>// 取文件名 read the file name <br>DragQueryFile (Msg.wParam, I, PChar (Filename), 80); <br>Filename := PChar (Filename); <br>//file://将全文件名分解程文件名和路径<br>ListItem := ListView1.Items.Add; <br>ListItem.Caption := ExtractFileName(FileName); <br>ListItem.SubItems.Add(ExtractFilePath(FileName)); <br>end; <br>finally <br>//file://结束这次拖放操作<br>DragFinish (Msg.wParam); <br>end; <br>//file://标识已处理了这条消息<br>Handled := True; <br>end;<br>end; <br><br><br>
 
接受答案了.
 
后退
顶部