|
| 1 | +using System; |
| 2 | +using System.ComponentModel; |
| 3 | +using System.Runtime.InteropServices; |
| 4 | +using System.Text; |
| 5 | +using System.Windows.Forms; |
| 6 | + |
| 7 | +namespace FileDropAdmin_cs |
| 8 | +{ |
| 9 | + // 解决管理员权限下无法拖放文件的问题 |
| 10 | + public sealed class FileDropAdmin : IMessageFilter, IDisposable |
| 11 | + { |
| 12 | + |
| 13 | + #region native members |
| 14 | + |
| 15 | + [DllImport("user32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)] |
| 16 | + [return: MarshalAs(UnmanagedType.Bool)] |
| 17 | + private static extern bool ChangeWindowMessageFilterEx(IntPtr hWnd, uint message, ChangeFilterAction action, in ChangeFilterStruct pChangeFilterStruct); |
| 18 | + |
| 19 | + [DllImport("shell32.dll", SetLastError = false, CallingConvention = CallingConvention.Winapi)] |
| 20 | + private static extern void DragAcceptFiles(IntPtr hWnd, bool fAccept); |
| 21 | + |
| 22 | + [DllImport("shell32.dll", SetLastError = false, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)] |
| 23 | + private static extern uint DragQueryFile(IntPtr hWnd, uint iFile, StringBuilder lpszFile, int cch); |
| 24 | + |
| 25 | + [DllImport("shell32.dll", SetLastError = false, CallingConvention = CallingConvention.Winapi)] |
| 26 | + private static extern void DragFinish(IntPtr hDrop); |
| 27 | + |
| 28 | + [StructLayout(LayoutKind.Sequential)] |
| 29 | + private struct ChangeFilterStruct |
| 30 | + { |
| 31 | + public uint CbSize; |
| 32 | + public ChangeFilterStatu ExtStatus; |
| 33 | + } |
| 34 | + |
| 35 | + private enum ChangeFilterAction : uint |
| 36 | + { |
| 37 | + MSGFLT_RESET, |
| 38 | + MSGFLT_ALLOW, |
| 39 | + MSGFLT_DISALLOW |
| 40 | + } |
| 41 | + |
| 42 | + private enum ChangeFilterStatu : uint |
| 43 | + { |
| 44 | + MSGFLTINFO_NONE, |
| 45 | + MSGFLTINFO_ALREADYALLOWED_FORWND, |
| 46 | + MSGFLTINFO_ALREADYDISALLOWED_FORWND, |
| 47 | + MSGFLTINFO_ALLOWED_HIGHER |
| 48 | + } |
| 49 | + |
| 50 | + private const uint WM_COPYGLOBALDATA = 0x0049; |
| 51 | + private const uint WM_COPYDATA = 0x004A; |
| 52 | + private const uint WM_DROPFILES = 0x0233; |
| 53 | + |
| 54 | + #endregion |
| 55 | + |
| 56 | + |
| 57 | + private const uint GetIndexCount = 0xFFFFFFFFU; |
| 58 | + |
| 59 | + private Control _ContainerControl; |
| 60 | + |
| 61 | + private readonly bool _DisposeControl; |
| 62 | + |
| 63 | + public Control ContainerControl { get; } |
| 64 | + |
| 65 | + public FileDropAdmin(Control containerControl) : this(containerControl, false) { } |
| 66 | + |
| 67 | + public FileDropAdmin(Control containerControl, bool releaseControl) |
| 68 | + { |
| 69 | + _ContainerControl = containerControl ?? throw new ArgumentNullException("control", "control is null."); |
| 70 | + |
| 71 | + if (containerControl.IsDisposed) throw new ObjectDisposedException("control"); |
| 72 | + |
| 73 | + _DisposeControl = releaseControl; |
| 74 | + |
| 75 | + var status = new ChangeFilterStruct() { CbSize = 8 }; |
| 76 | + |
| 77 | + if (!ChangeWindowMessageFilterEx(containerControl.Handle, WM_DROPFILES, ChangeFilterAction.MSGFLT_ALLOW, in status)) throw new Win32Exception(Marshal.GetLastWin32Error()); |
| 78 | + if (!ChangeWindowMessageFilterEx(containerControl.Handle, WM_COPYGLOBALDATA, ChangeFilterAction.MSGFLT_ALLOW, in status)) throw new Win32Exception(Marshal.GetLastWin32Error()); |
| 79 | + if (!ChangeWindowMessageFilterEx(containerControl.Handle, WM_COPYDATA, ChangeFilterAction.MSGFLT_ALLOW, in status)) throw new Win32Exception(Marshal.GetLastWin32Error()); |
| 80 | + DragAcceptFiles(containerControl.Handle, true); |
| 81 | + |
| 82 | + Application.AddMessageFilter(this); |
| 83 | + } |
| 84 | + |
| 85 | + public bool PreFilterMessage(ref Message m) |
| 86 | + { |
| 87 | + if (_ContainerControl == null || _ContainerControl.IsDisposed) return false; |
| 88 | + if (_ContainerControl.AllowDrop) return _ContainerControl.AllowDrop = false; |
| 89 | + if (m.Msg == WM_DROPFILES) |
| 90 | + { |
| 91 | + var handle = m.WParam; |
| 92 | + |
| 93 | + var fileCount = DragQueryFile(handle, GetIndexCount, null, 0); |
| 94 | + //Debug.Print(fileCount.ToString()); |
| 95 | + var fileNames = new string[fileCount]; |
| 96 | + //Debug.Print(fileNames[1]); |
| 97 | + var sb = new StringBuilder(262); |
| 98 | + var charLength = sb.Capacity; |
| 99 | + for (uint i = 0; i < fileCount; i++) |
| 100 | + { |
| 101 | + if (DragQueryFile(handle, i, sb, charLength) > 0) fileNames[i] = sb.ToString(); |
| 102 | + //Debug.Print(fileNames[i]); |
| 103 | + } |
| 104 | + DragFinish(handle); |
| 105 | + _ContainerControl.AllowDrop = true; |
| 106 | + _ContainerControl.DoDragDrop(fileNames, DragDropEffects.All); |
| 107 | + _ContainerControl.AllowDrop = false; |
| 108 | + return true; |
| 109 | + } |
| 110 | + return false; |
| 111 | + } |
| 112 | + |
| 113 | + public void Dispose() |
| 114 | + { |
| 115 | + if (_ContainerControl == null) |
| 116 | + { |
| 117 | + if (_DisposeControl && !_ContainerControl.IsDisposed) _ContainerControl.Dispose(); |
| 118 | + Application.RemoveMessageFilter(this); |
| 119 | + _ContainerControl = null; |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments