下列程序是本人在软件开发过程中根据用户操作上的方便,写的一个小过程,主要实现了向listview控件中拖放文件功能,其源代码如下:
procedure tyanzheng.AppMessage(var Msg: TMsg;var Handled: Boolean); var nFiles, I: Integer; ListItem: TListItem; begin if (Msg.message = WM_DROPFILES) and (msg.hwnd = ListView1.Handle) then begin if MessageDlg(''''确定要加入吗'''', mtConfirmation, [mbYes, mbNo], 0) = mrYes then begin // 取dropped files的数量 nFiles := DragQueryFile (Msg.wParam, $FFFFFFFF, nil, 0); // 循环取每个拖下文件的全文件名 try for I := 0 to nFiles - 1 do begin // 为文件名分配缓冲 allocate memory SetLength (Filename, 80); // 取文件名 read the file name DragQueryFile (Msg.wParam, I, PChar (Filename), 80); Filename := PChar (Filename); //将全文件名分解程文件名和路径 ListItem := ListView1.Items.Add; ListItem.Caption := ExtractFileName(FileName); listitem.ImageIndex:=6; ListItem.SubItems.Add(ExtractFilePath(FileName)); filepath:=extractfilepath(filename); //drage:=true; end; finally //结束这次拖放操作 DragFinish (Msg.wParam); end; //标识已处理了这条消息 Handled := True; movefile(pchar(filename),pchar(filespath+listitem.caption)); end;end; end; 注意:本程序功能是完整的,程序中使用了movefile函数,也就是说,实现了把操作的文件移到了系统所在的目录中,因此建议在调试过程中不要用重要的文件作调试用。
程序中用到了向windows发送消息,不明白的话,建议看一些有关方面的文章(本程序可直接运行,须加入到系统中),本程序与《tlistview显示文件夹内容中的程序结合使用,效果更好》
程序用在showform事件中:用法如下:
//设置需要处理文件WM_DROPFILES拖放消息 DragAcceptFiles(ListView1.Handle, TRUE); //设置AppMessage过程来捕获所有消息 Application.OnMessage := AppMessage;
谢谢支持
没有相关教程
|