防止TWebBrowser接受丢弃的文件

问题描述:

我有一个表单接受要拖放的文件,以及放置在同一表单上的TPanel控件上的TWebBrowser控件。防止TWebBrowser接受丢弃的文件

最主要的是,当我在窗体上放置一个文件时,它的路径被添加到一个TEdit控件中。但是,当用户在表单上拖放文件时,有时他们可能会将其放到TWebBrowser上,这会根据文件类型为用户保存或运行文件。这是我实际上不希望发生的事情,我只想让TWebBrowser忽略掉落的文件或按照表单处理它。

这是我使用治疗WM_DROPFILES消息代码:

procedure TMainForm.AcceptFiles(var msg : TMessage); 
const 
    cnMaxFileNameLen = 255; 
var 
    i, 
    nCount  : integer; 
    acFileName : array [0..cnMaxFileNameLen] of char; 
begin 
    // find out how many files we're accepting 
    nCount := DragQueryFile(msg.WParam, 
          $FFFFFFFF, 
          acFileName, 
          cnMaxFileNameLen); 

    // query Windows one at a time for the file name 
    for i := 0 to nCount-1 do 
    begin 
    DragQueryFile(msg.WParam, i, 
        acFileName, cnMaxFileNameLen); 

    // do your thing with the acFileName 
    //MessageBox(Handle, acFileName, '', MB_OK); 
    Edit1.Text := acFileName; 
    end; 

    // let Windows know that you're done 
    DragFinish(msg.WParam); 
end; 

预先感谢您。任何线索将不胜感激。

要拦截TWebBrowser中的拖放操作,您必须实现IDropTargetIDocHostUIHandler接口。那么你必须使用GetDropTarget方法来传递你自己的IDropTarget实现。

对于样品Delphi代码尝试本文How to handle drag and drop in a TWebBrowser control

+0

谢谢,@RRUZ,这就是我一直在寻找。 – 2012-07-12 18:26:41