中国程序员联盟 正在重新改版中ing 不便之处还请见谅 改版后将内容涉及java delphi .net php
 
  首页 | 数据库开发 | 网络通讯 | 多线程 | 多媒体开发 | 图像处理 | 程序人生 | 系统函数 | 控件开发 | Web服务
 
  当前位置:笨鱼delphi技术网>系统函数>文章内容

delphi 表单接收拖动的文件

来源:国外 关于:Pablo Reyes 发布时间:2007-07-03   [收藏] [推荐]
Problem/Question/Abstract:
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.
interface
procedure WMDROPFILES(var Msg: TMessage);
implementation
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, 0, 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.

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

[浏览: 次]   
上一篇:delphi 查询工作组中的电脑   下一篇:delphi 如何写二进制文件到注册表
[收藏] [推荐] [返回顶部] [打印本页] [关闭窗口]  
    评论加载中…
google adsense热点文章
·delphi Delphi中ShellExecute的妙用
·delphi 如何快速读取文本文件
·delphi 如何判断输入值是否中文
·delphi 在应用层截获键盘消息
·delphi delphi实现服务开启与关闭
·delphi 实时记录事件日志
·delphi 使MEMO自动滚动
·delphi 如何区分键盘两个Enter键
·delphi 切换界面的方法
·delphi 汉字输入法的编程及使用
·delphi Delphi程序输入法自动切换最简
·delphi 程序缩小为任务右下角的小图标
     delphi技术网 | firefox 下载 | Avant Browser下载 | dedecms 技术网 | drupal 爱好者 | php 技术网
  Copyright@www.delphichm.com,2006-2009.All Rights Reserved.
 
程序员联盟 | delphi Java .net|QQ:707102932