扩展WebBrowser控件,使其支持拖放文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
public partial class UserControl1 : WebBrowser
{
    private const int WmDropfiles = 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);
 
    protected override void OnHandleCreated(EventArgs e)
    {
        base.OnHandleCreated(e);
 
        //指示接受文件拖放操作
        if (!DesignMode)
        {
            DragAcceptFiles(Handle, true);
        }
    }
 
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WmDropfiles)
        {
            #region 解析消息
            //获取拖放的文件数量
            var count = DragQueryFile(m.WParam, 0xffffffff, null, 0);
            //解析所有播放的文件路径
            var filePaths = new string[count];
            for (uint i = 0; i < count; i++)
            {
                var sb = new StringBuilder();
                DragQueryFile(m.WParam, i, sb, 1024);
                filePaths[i] = sb.ToString();
            }
            #endregion
 
            //是否继续触发之后的消息处理
            var isCancel = false;
 
            #region 触发自定义文件拖放事件
            if (DragFile != null)
            {
                var args = new DragFileEventArgs(filePaths);
                DragFile(this, args);
                isCancel = args.IsCancel;
            }
            #endregion
 
            if (isCancel) return;
        }
        base.WndProc(ref m);
    }
 
    public event EventHandler<DragFileEventArgs> DragFile;
 
    public class DragFileEventArgs : EventArgs
    {
        private readonly string[] _filePaths;
        public bool IsCancel { get; set; }
 
        public DragFileEventArgs(string[] filePaths)
        {
            _filePaths = filePaths;
        }
 
        public string[] FilePaths
        {
            get { return _filePaths; }
        }
    }
}

  

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。