public class WebBrowserEx : WebBrowser
{
private const int WM_DROPFILES = 0x233;
[DllImport("shell32.dll")]
private static extern uint DragQueryFile(
IntPtr hDrop,
uint iFile,
StringBuilder lpszFile,
uint cch);
[DllImport("shell32.dll")]
private static extern void DragAcceptFiles(IntPtr hWnd, bool fAccept);
public WebBrowserEx()
: base()
{
base.AllowWebBrowserDrop = false;
}
public event DragFileEventHandler DragFile;
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
if (!DesignMode)
{
DragAcceptFiles(Handle, true);
}
}
protected override void WndProc(ref Message m)
{
StringBuilder sb = new StringBuilder(1024);
if (m.Msg == WM_DROPFILES)
{
uint count = DragQueryFile(m.WParam, 0xffffffff, null, 0);
string[] files = new string[count];
for (uint i = 0; i < count; i++)
{
DragQueryFile(m.WParam, i, sb, 1024);
files[i] = sb.ToString();
}
OnDragFile(new DragFileEventArgs(files));
return;
}
base.WndProc(ref m);
}
protected virtual void OnDragFile(DragFileEventArgs e)
{
if (DragFile != null)
{
DragFile(this, e);
}
}
}
public delegate void DragFileEventHandler(
object sender,
DragFileEventArgs e);
public class DragFileEventArgs : EventArgs
{
private string[] _files;
public DragFileEventArgs(string[] files)
{
_files = files;
}
public string[] Files
{
get { return _files; }
}
}