private void SetReplaceItems(ContextMenu menu) { foreach (var item in replaceItems) { var count = WinApiFunctions.GetMenuItemCount(menu.Handle); var index = item.Index < 0 ? count + item.Index : item.Index; var itemInfo = new MENUITEMINFO(); itemInfo.cbSize = Marshal.SizeOf(itemInfo); itemInfo.fMask = MIIM.MIIM_ID | MIIM.MIIM_SUBMENU; if (WinApiFunctions.GetMenuItemInfo(menu.Handle, index, true, ref itemInfo)) { actions[itemInfo.wID] = item.Action; } } }
private void SetNewItems(ContextMenu menu) { foreach (var item in newItems) { var newItemInfo = new MENUITEMINFO(); newItemInfo.cbSize = Marshal.SizeOf(newItemInfo); newItemInfo.fMask = MIIM.MIIM_CHECKMARKS | MIIM.MIIM_FTYPE | MIIM.MIIM_ID | MIIM.MIIM_STATE | MIIM.MIIM_STRING; newItemInfo.fType = (uint)Consts.MFT_STRING; newItemInfo.fState = (uint)Consts.MFS_ENABLED; newItemInfo.wID = actions.Count + 1; actions[newItemInfo.wID] = item.Action; // newItemInfo.hbmpChecked = hBitmap; // newItemInfo.hbmpUnchecked = hBitmap; newItemInfo.dwTypeData = item.Text; newItemInfo.cch = 8; WinApiFunctions.InsertMenuItem(menu.Handle, item.Index, true, ref newItemInfo); } }
private static IShellItem CreateShellItem(string filename) { try { return(WinApiFunctions.SHCreateItemFromParsingName(filename, IntPtr.Zero, typeof(IShellItem).GUID)); } catch (ArgumentException) { return(null); } // For system files like $MFT, $Boot etc. catch (UnauthorizedAccessException) { return(null); } catch (FileNotFoundException) { return(null); } }
// This function returns after the user canceled the menu or chose a menu item. public void Show() { var contextMenu = CreateShellMenu(filename); if (contextMenu == null) { return; } try { using (var menu = new ContextMenu()) { contextMenu.QueryContextMenu(menu.Handle, 0, FirstCmd, int.MaxValue, (CMF)Consts.CMF_EXPLORE); actions.Clear(); SetReplaceItems(menu); SetNewItems(menu); var wnd = new Form(); Action showMenu = () => { var command = WinApiFunctions.TrackPopupMenuEx(menu.Handle, TPM.TPM_RETURNCMD, pos.X, pos.Y, wnd.Handle, IntPtr.Zero); DispatchCommand(contextMenu, command); }; if (Dispatcher.CurrentDispatcher.CheckAccess()) { showMenu(); } else { Dispatcher.CurrentDispatcher.BeginInvoke(showMenu); } } } catch (Exception ex) { Log.Instance.Error("ShellMenuManager: " + ex.Message); } }
public static BitmapSource ToBitmapSource(this Bitmap bmp) { if (bmp == null) { return(null); } var handle = bmp.GetHbitmap(); BitmapSource source; try { source = Imaging.CreateBitmapSourceFromHBitmap(handle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); } finally { WinApiFunctions.DeleteObject(handle); } return(source); }
public BitmapSource GetThumbnail(string fileName) { var thumbSizePixels = GetThumbSizePixels(); var bitmapHandle = IntPtr.Zero; Bitmap bmpThumbnail = null; const ThumbnailOptions options = ThumbnailOptions.BiggerSizeOk | ThumbnailOptions.ResizeToFit; try { bitmapHandle = GetBitmapHandle(Path.GetFullPath(fileName), thumbSizePixels, options); bmpThumbnail = GetBitmapFromHandle(bitmapHandle); } catch (Exception e) { Log.Instance.Error("Cannot retrieve thumbnail " + e.Message); } finally { // Delete HBitmap to avoid memory leaks. WinApiFunctions.DeleteObject(bitmapHandle); } return(bmpThumbnail == null ? null : bmpThumbnail.ToBitmapSource()); }
public static IntPtr GetID(IShellItem item) { var intermediateID = SHGetIDListFromObject(item); return(WinApiFunctions.ILFindLastID(intermediateID)); }
private static IntPtr SHGetIDListFromObject(IShellItem item) { // todo implement for XP return(WinApiFunctions.SHGetIDListFromObject(item)); }