void OnDropFiles(IntPtr hDrop) { var filenames = new List <string>(); int count = Shell32.DragQueryFile(hDrop, 0xFFFFFFFF, null, 0); for (uint i = 0; i < count; i++) { var sb = new StringBuilder(256); if (Shell32.DragQueryFile(hDrop, i, sb, (uint)sb.Capacity) > 0) { filenames.Add(sb.ToString()); } } Shell32.DragFinish(hDrop); // var pt = this.PointToClient(Control.MousePosition); var de = new DropFilesEventArgs(filenames.ToArray(), pt); this.PostDropFiles(de); if (de.Handled) { return; } if (filenames.Count > 0) { OpenDocuments(filenames.ToArray()); } }
/// <summary> /// Gets the file list. /// </summary> /// <param name="this">The IDataObject instance.</param> /// <returns>The file list in the data object.</returns> public static List <string> GetFileList(this IDataObject @this) { // Create the format object. var formatEtc = new FORMATETC(); // Set up the format object, based on CF_HDROP. formatEtc.cfFormat = (short)CLIPFORMAT.CF_HDROP; formatEtc.ptd = IntPtr.Zero; formatEtc.dwAspect = DVASPECT.DVASPECT_CONTENT; formatEtc.lindex = -1; formatEtc.tymed = TYMED.TYMED_HGLOBAL; // Get the data. // ReSharper disable RedundantAssignment var storageMedium = new STGMEDIUM(); // ReSharper restore RedundantAssignment @this.GetData(ref formatEtc, out storageMedium); var fileList = new List <string>(); // Things can get risky now. try { // Get the handle to the HDROP. var hDrop = storageMedium.unionmember; if (hDrop == IntPtr.Zero) { throw new ArgumentException("Failed to get the handle to the drop data."); } // Get the count of the files in the operation. var fileCount = Shell32.DragQueryFile(hDrop, UInt32.MaxValue, null, 0); // Go through each file. for (uint i = 0; i < fileCount; i++) { // Storage for the file name. var fileNameBuilder = new StringBuilder(MaxPath); // Get the file name. var result = Shell32.DragQueryFile(hDrop, i, fileNameBuilder, (uint)fileNameBuilder.Capacity); // If we have a valid result, add the file name to the list of files. if (result != 0) { fileList.Add(fileNameBuilder.ToString()); } } } finally { Ole32.ReleaseStgMedium(ref storageMedium); } return(fileList); }
/// <summary>Intercept the WndProc for this control</summary> protected override void WndProc(ref Message m) { //Win32.WndProcDebug(hwnd, msg, wparam, lparam, "vt100"); switch (m.Msg) { case Win32.WM_KEYDOWN: #region { var ks = new Win32.KeyState(m.LParam); if (!ks.Alt && Buffer != null) { var vk = Win32.ToVKey(m.WParam); // Forward navigation keys to the control if (vk == EKeyCodes.Up || vk == EKeyCodes.Down || vk == EKeyCodes.Left || vk == EKeyCodes.Right) { Cmd(m.Msg, m.WParam, m.LParam); return; } // Handle clipboard shortcuts if (Win32.KeyDown(EKeyCodes.ControlKey)) { if (vk == EKeyCodes.C) { Copy(); return; } if (vk == EKeyCodes.V) { Paste(); return; } return; // Disable all other shortcuts } // Add the key press to the buffer input AddToBuffer(vk); // Let the key events through if local echo is on if (Buffer.Settings.LocalEcho) { break; } // Block key events from getting to the ctrl return; } break; } #endregion case Win32.WM_CHAR: #region { return; } #endregion case Win32.WM_DROPFILES: #region { var drop_info = m.WParam; var count = Shell32.DragQueryFile(drop_info, 0xFFFFFFFFU, null, 0); var files = new List <string>(); for (int i = 0; i != count; ++i) { var sb = new StringBuilder((int)Shell32.DragQueryFile(drop_info, (uint)i, null, 0) + 1); if (Shell32.DragQueryFile(drop_info, (uint)i, sb, (uint)sb.Capacity) == 0) { throw new Exception("Failed to query file name from dropped files"); } files.Add(sb.ToString()); sb.Clear(); } HandleDropFiles(files); return; } #endregion } base.WndProc(ref m); }
public void Initialize(IntPtr pidlFolder, IntPtr pDataObj, IntPtr hKeyProgID) { if (pDataObj == IntPtr.Zero) { throw new ArgumentException(); } ApplicationInfo.RegisterAppName(GetType().Assembly); Translator.RegisterTranslationAssembly(GetType().Assembly); Translator.SetInterfaceLanguage(AppConfig.LanguageID); FORMATETC fe = new FORMATETC(); fe.cfFormat = (short)CLIPFORMAT.CF_HDROP; fe.ptd = IntPtr.Zero; fe.dwAspect = DVASPECT.DVASPECT_CONTENT; fe.lindex = -1; fe.tymed = TYMED.TYMED_HGLOBAL; STGMEDIUM stm = new STGMEDIUM(); // The pDataObj pointer contains the objects being acted upon. In this // example, we get an HDROP handle for enumerating the selected files // and folders. IDataObject dataObject = (IDataObject)Marshal.GetObjectForIUnknown(pDataObj); dataObject.GetData(ref fe, out stm); try { // Get an HDROP handle. IntPtr hDrop = stm.unionmember; if (hDrop == IntPtr.Zero) { throw new ArgumentException(); } // Determine how many files are involved in this operation. uint nFiles = Shell32.DragQueryFile(hDrop, UInt32.MaxValue, null, 0); // Enumerate the selected files and folders. if (nFiles > 0) { this.fileList = new List <string>(); StringBuilder fileName = new StringBuilder(Kernel32.MAX_FILE_BUFFER); for (uint i = 0; i < Math.Min(nFiles, Kernel32.MAX_FILES); i++) { // Get the next file name. if (Shell32.DragQueryFile(hDrop, i, fileName, fileName.Capacity) != 0 && MediaRenderer.IsSupportedMedia(fileName.ToString())) { // Add the file name to the list. fileList.Add(fileName.ToString()); } } // If we did not find any files we can work with, throw // exception. if (fileList.Count == 0) { Marshal.ThrowExceptionForHR(WinError.E_FAIL); } } else { Marshal.ThrowExceptionForHR(WinError.E_FAIL); } } catch (Exception ex) { //ErrorDispatcher.DispatchException(ex); Logger.LogException(ex); fileList.Clear(); } finally { Ole32.ReleaseStgMedium(ref stm); } }