/// <summary> /// プロパティの初期設定を行います。 /// </summary> private void Initialize() { // SearchCondition初期化 SearchFolderItemFactory.SetCondition(this.SearchCondition.SearchConditionNative); // SearchScopePaths初期化 var shellItems = new List <IShellItem>(this.SearchScopePaths.Length); var shellItemGuid = new Guid(ShellIID.IShellItem); foreach (var path in this.SearchScopePaths) { IShellItem scopeShellItem; var hr = ShellNativeMethods.SHCreateItemFromParsingName(path, IntPtr.Zero, ref shellItemGuid, out scopeShellItem); if (HRESULT.Succeeded(hr)) { shellItems.Add(scopeShellItem); } } var scopeShellItemArray = new ShellItemArray(shellItems.ToArray()); var result = SearchFolderItemFactory.SetScope(scopeShellItemArray); if (HRESULT.Failed(result)) { throw ShellException.FromHRESULT(result); } }
// T7E: Gets property store for shortcut public void SetApplicationIdForShortcut(string shortcutPath, string appId) { // Get shell item IShellItem2 shellItem; Guid guid = new Guid(ShellIIDGuid.IShellItem2); int hrc = ShellNativeMethods.SHCreateItemFromParsingName(shortcutPath, IntPtr.Zero, ref guid, out shellItem); // Get property store Guid psGuid = new Guid(ShellIIDGuid.IPropertyStore); IPropertyStore propStore = null; int hr = shellItem.GetPropertyStore( ShellNativeMethods.GETPROPERTYSTOREFLAGS.GPS_READWRITE, ref psGuid, out propStore); // Set appid property PropVariant pv = new PropVariant(); PropertyKey AppIdKey = new PropertyKey(new Guid("9F4C2855-9F79-4B39-A8D0-E1D42DE1D5F3"), 5); pv.SetString(appId); propStore.SetValue(ref AppIdKey, ref pv); propStore.Commit(); Marshal.ReleaseComObject(propStore); pv.Clear(); }
/// <summary> /// Adds a location (folder, library, search connector, known folder) to the list of /// places available for the user to open or save items. This method actually adds an item /// to the <b>Favorite Links</b> or <b>Places</b> section of the Open/Save dialog. Overload method /// takes in a string for the path. /// </summary> /// <param name="path">The item to add to the places list.</param> /// <param name="location">One of the enumeration values that indicates placement of the item in the list.</param> public void AddPlace(string path, FileDialogAddPlaceLocation location) { if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException("path"); } // Get our native dialog if (nativeDialog == null) { InitializeNativeFileDialog(); nativeDialog = GetNativeFileDialog(); } // Create a native shellitem from our path IShellItem2 nativeShellItem; Guid guid = new Guid(ShellIIDGuid.IShellItem2); int retCode = ShellNativeMethods.SHCreateItemFromParsingName(path, IntPtr.Zero, ref guid, out nativeShellItem); if (!CoreErrorHelper.Succeeded(retCode)) { throw new Exception(LocalizedMessages.CommonFileDialogCannotCreateShellItem, Marshal.GetExceptionForHR(retCode)); } // Add the shellitem to the places list if (nativeDialog != null) { nativeDialog.AddPlace(nativeShellItem, (ShellNativeMethods.FileDialogAddPlacement)location); } }
/// <summary> /// Create new shell library. /// </summary> /// <param name="libraryName">Name of the library to be created.</param> /// <param name="sourcePath">Library path.</param> /// <param name="overwrite">A value indicating whether to overwrite an existing library.</param> /// <returns>Created <see cref="ShellLibrary" />.</returns> public static ShellLibrary Create(string libraryName, string sourcePath, bool overwrite) { Contract.Requires <ArgumentException>(!String.IsNullOrWhiteSpace(libraryName)); Contract.Requires <ArgumentException>(!String.IsNullOrWhiteSpace(sourcePath)); Contract.Ensures(Contract.Result <ShellLibrary>() != null); if (!Directory.Exists(sourcePath)) { throw new DirectoryNotFoundException(ErrorMessages.ShellLibraryFolderNotFound); } var guid = new Guid(ShellIID.IShellItem); var flags = GetLibrarySaveOptions(overwrite); IShellItem shellItemIn; ShellNativeMethods.SHCreateItemFromParsingName(sourcePath, IntPtr.Zero, ref guid, out shellItemIn); var shellLibraryInterface = CreateShellLibraryNativeInterface(); IShellItem2 shellItem2; shellLibraryInterface.Save(shellItemIn, libraryName, flags, out shellItem2); return(new ShellLibrary(new ShellItem(shellItem2), shellLibraryInterface, libraryName)); }
/// <summary> /// Release the native and managed objects /// </summary> /// <param name="disposing">Indicates that this is being called from Dispose(), rather than the finalizer.</param> protected virtual void Dispose(bool disposing) { if (disposing) { _internalName = null; _internalParsingName = null; properties = null; parentShellObject = null; } if (properties != null) { properties.Dispose(); } if (_internalPIDL != IntPtr.Zero) { ShellNativeMethods.ILFree(_internalPIDL); _internalPIDL = IntPtr.Zero; } if (nativeShellItem != null) { Marshal.ReleaseComObject(nativeShellItem); nativeShellItem = null; } if (NativePropertyStore != null) { Marshal.ReleaseComObject(NativePropertyStore); NativePropertyStore = null; } }
private void GetImageReference() { IPropertyStore store = ShellPropertyCollection.CreateDefaultPropertyStore(ParentShellObject); using (PropVariant propVar = new PropVariant()) { store.GetValue(ref propertyKey, propVar); Marshal.ReleaseComObject(store); store = null; string refPath; ((IPropertyDescription2)Description.NativePropertyDescription).GetImageReferenceForValue( propVar, out refPath); if (refPath == null) { return; } int index = ShellNativeMethods.PathParseIconLocation(ref refPath); if (refPath != null) { imageReferencePath = refPath; imageReferenceIconIndex = index; } } }
internal static IntPtr PidlFromUnknown(IntPtr unknown) { IntPtr pidl; int retCode = ShellNativeMethods.SHGetIDListFromObject(unknown, out pidl); return(CoreErrorHelper.Succeeded(retCode) ? pidl : IntPtr.Zero); }
/// <summary> /// <see cref="KNOWNFOLDER_DEFINITION" />を取得し、 /// 各プロパティを初期化します。 /// </summary> /// <param name="knownFolderInterface"></param> private void Initialize(IKnownFolder knownFolderInterface) { Contract.Requires(knownFolderInterface != null); KNOWNFOLDER_DEFINITION knownFolderDefinition; knownFolderInterface.GetFolderDefinition(out knownFolderDefinition); try { this.ParsingName = PtrToString(knownFolderDefinition.pszParsingName); this.CanonicalName = PtrToString(knownFolderDefinition.pszName); this.Category = (KnownFolderCategories)knownFolderDefinition.category; this.Description = PtrToString(knownFolderDefinition.pszDescription); this.ParentId = knownFolderDefinition.fidParent; this.RelativePath = PtrToString(knownFolderDefinition.pszRelativePath); InitializeResourceProperties(knownFolderDefinition); this.FolderDefinitionFlags = (FolderDefinitionFlags)knownFolderDefinition.kfdFlags; this.FileAttributes = (FileAttributes)knownFolderDefinition.dwAttributes; this.FolderTypeId = knownFolderDefinition.ftidType; this.FolderType = String.Empty; this.FolderId = knownFolderInterface.GetId(); InitializePath(knownFolderInterface); this.Redirection = (RedirectionCapability)knownFolderInterface.GetRedirectionCapabilities(); this.Security = PtrToString(knownFolderDefinition.pszSecurity); } finally { ShellNativeMethods.FreeKnownFolderDefinitionFields(ref knownFolderDefinition); } }
/// <summary> /// Create a new instance of the <see cref="ShellLibrary" /> class /// to the specified library name. /// </summary> /// <param name="libraryName">Name of the library to be created.</param> /// <param name="isReadOnly">A value that indicates whether the library is readonly.</param> /// <returns>Created <see cref="ShellLibrary" />.</returns> public static ShellLibrary Load(string libraryName, bool isReadOnly = true) { Contract.Requires <ArgumentNullException>(libraryName != null); Contract.Requires <ArgumentException>(!String.IsNullOrWhiteSpace(libraryName)); Contract.Ensures(Contract.Result <ShellLibrary>() != null); var shellItemPath = Path.Combine(ShellKnownFolders.Libraries.Path, libraryName + FileExtension); IShellItem shellItem; var guid = new Guid(ShellIID.IShellItem); var hr = ShellNativeMethods.SHCreateItemFromParsingName(shellItemPath, IntPtr.Zero, ref guid, out shellItem); if (HRESULT.Failed(hr)) { throw ShellException.FromHRESULT(hr); } var shellLibraryInterface = CreateShellLibraryNativeInterface(); var flags = isReadOnly ? STGM.STGM_READ : STGM.STGM_READWRITE; shellLibraryInterface.LoadLibraryFromItem(shellItem, flags); return(new ShellLibrary(new ShellItem((IShellItem2)shellItem), shellLibraryInterface, libraryName)); }
internal static IntPtr PidlFromParsingName(string name) { IntPtr pidl; ShellNativeMethods.ShellFileGetAttributesOptions sfgao; int retCode = ShellNativeMethods.SHParseDisplayName( name, IntPtr.Zero, out pidl, (ShellNativeMethods.ShellFileGetAttributesOptions) 0, out sfgao); return(CoreErrorHelper.Succeeded(retCode) ? pidl : IntPtr.Zero); }
/// <summary> /// Create a new instance of the <see cref="ShellObject" /> class. /// </summary> /// <param name="pidl"><c>PIDL</c>。</param> /// <param name="riid"><c>GUID</c>。</param> /// <returns></returns> private static ShellObject CreateShellObject(IntPtr pidl, ref Guid riid) { IShellItem2 shellItem2; var code = ShellNativeMethods.SHCreateItemFromIDList(pidl, ref riid, out shellItem2); if (HRESULT.Failed(code)) { return(null); } return(ShellFactory.FromShellItem(new ShellItem(shellItem2))); }
/// <summary> /// Override the thumbnail and peek bitmap. /// By providing this bitmap manually, Thumbnail Window manager will provide the /// Desktop Window Manager (DWM) this bitmap instead of rendering one automatically. /// Use this property to update the bitmap whenever the control is updated and the user /// needs to be shown a new thumbnail on the taskbar preview (or aero peek). /// </summary> /// <param name="hBitmap">A bitmap handle for the image to use. /// <para>When the TabbedThumbnail is finalized, this class will delete the provided hBitmap.</para></param> /// <remarks> /// If the bitmap doesn't have the right dimensions, the DWM may scale it or not /// render certain areas as appropriate - it is the user's responsibility /// to render a bitmap with the proper dimensions. /// </remarks> internal void SetImage(IntPtr hBitmap) { // Before we set a new bitmap, dispose the old one if (CurrentHBitmap != IntPtr.Zero) { ShellNativeMethods.DeleteObject(CurrentHBitmap); } // Set the new bitmap CurrentHBitmap = hBitmap; // Let DWM know to invalidate its cached thumbnail/preview and ask us for a new one TaskbarWindowManager.InvalidatePreview(TaskbarWindow); }
/// <summary> /// Load icon from resource. /// </summary> /// <param name="size">Icon size.</param> /// <returns><see cref="ShellIcon"/>.</returns> public ShellIcon LoadIcon(int size = 0) { Contract.Ensures(Contract.Result <ShellIcon>() != null); var smallIcons = new[] { IntPtr.Zero }; var largeIcons = new[] { IntPtr.Zero }; ShellNativeMethods.ExtractIconEx(this.LibraryPath, this.ResourceId, largeIcons, smallIcons, 1); var hicon = largeIcons[0]; if (hicon == IntPtr.Zero) { throw new Win32Exception(); } return(new ShellIcon(hicon)); }
/// <summary> /// Create new instance of the <see cref="ShellItem" /> class /// to the specified <c>PIDL</c>. /// </summary> /// <param name="pidl"><c>PIDL</c>.</param> /// <returns><see cref="ShellItem" />.</returns> /// <exception cref="ArgumentException"><paramref name="pidl" /> is <c>null</c>.</exception> /// <exception cref="ShellException">Failed to create <see cref="ShellItem" />.</exception> internal static ShellItem FromPIDL(PIDL pidl) { Contract.Requires <ArgumentException>(pidl != PIDL.Null); Contract.Ensures(Contract.Result <ShellItem>() != null); IShellItem2 shellItem; var code = ShellNativeMethods.SHCreateItemFromIDList( pidl, ref ShellIIDGuid.IShellItem2, out shellItem); if (HRESULT.Failed(code)) { throw new ShellException(ErrorMessages.ShellFactoryUnableToCreateItem, Marshal.GetExceptionForHR(code)); } return(new ShellItem(shellItem)); }
/// <summary> /// Creates a ShellObject given a parsing name /// </summary> /// <param name="parsingName"></param> /// <returns>A newly constructed ShellObject object</returns> internal static ShellObject Create(string parsingName) { if (string.IsNullOrEmpty(parsingName)) { throw new ArgumentNullException("parsingName"); } // Create a native shellitem from our path IShellItem2 nativeShellItem; Guid guid = new Guid("7E9FB0D3-919F-4307-AB2E-9B1860310C93"); int retCode = ShellNativeMethods.SHCreateItemFromParsingName(parsingName, IntPtr.Zero, ref guid, out nativeShellItem); if (retCode != 0) { throw new Exception("ShellObjectFactoryUnableToCreateItem", Marshal.GetExceptionForHR(retCode)); } return(ShellObjectFactory.Create(nativeShellItem)); }
public static IShellItem2 ParseShellItem2Name(String value) { Guid ishellItem2GuidCopy = _ishellItem2Guid; IShellItem2 shellItem; HResult result = ShellNativeMethods.SHCreateItemFromParsingName(value, IntPtr.Zero, ref ishellItem2GuidCopy, out shellItem); if (result == HResult.Ok) { return(shellItem); } else { // TODO: Ideally we'd interrogate each Win32 error to decide if this function should return null (e.g. nonsensical filename) or throw (some system error). // But that takes too much effort, so just always return null for now. return(null); } }
/// <summary> /// Create new instance of the <see cref="ShellItem" /> class /// to the specified <c>ParsingName</c>. /// </summary> /// <param name="parsingName">Parsing Name.</param> /// <returns><see cref="ShellItem" />.</returns> /// <exception cref="ArgumentException"><paramref name="parsingName" /> is <c>null</c> or empty string.</exception> /// <exception cref="ShellException">Failed to create <see cref="ShellItem" />.</exception> public static ShellItem FromParsingName(string parsingName) { Contract.Requires <ArgumentException>(!String.IsNullOrWhiteSpace(parsingName)); Contract.Ensures(Contract.Result <ShellItem>() != null); IShellItem2 shellItem2; var code = ShellNativeMethods.SHCreateItemFromParsingName( parsingName, IntPtr.Zero, ref ShellIIDGuid.IShellItem2, out shellItem2); if (shellItem2 == null || HRESULT.Failed(code)) { throw new ShellException(ErrorMessages.ShellFactoryUnableToCreateItem, Marshal.GetExceptionForHR(code)); } return(new ShellItem(shellItem2)); }
/// <summary> /// Returns the hash code of the object. /// </summary> /// <returns></returns> public override int GetHashCode() { if (!hashValue.HasValue) { uint size = ShellNativeMethods.ILGetSize(PIDL); if (size != 0) { byte[] pidlData = new byte[size]; Marshal.Copy(PIDL, pidlData, 0, (int)size); byte[] hashData = hashProvider.ComputeHash(pidlData); hashValue = BitConverter.ToInt32(hashData, 0); } else { hashValue = 0; } } return(hashValue.Value); }
static void Main(string[] args) { Console.BufferWidth = 120; Console.WindowWidth = 120; Console.WindowHeight = 50; //IntPtr desktopP; //var desktop = ShellExtension.GetDesktopFolder(out desktopP); //ShellNativeMethods.SHGetDesktopFolder //GetChilds(desktop); //--------------- CreateLib(); ListFolders(); RegisterFolder(); //-------------------- IShellItem s; NonFileSystemKnownFolder sf = (NonFileSystemKnownFolder)ShellObject.FromParsingName(KnownFolders.Computer.ParsingName); IntPtr ppidl; uint flag = 0; SHILCreateFromPath(@"d:\temp", out ppidl, ref flag); var tmp = ShellNativeMethods.SHCreateShellItem(IntPtr.Zero, sf.NativeShellFolder, ppidl, out s); GetFolder(); //DeleteFolder(); Console.WriteLine("Try to get extension again:"); GetFolder(); Console.ReadKey(); }
/// <summary> /// Create new instance of the <see cref="ShellItem" /> class /// to the specified <c>IDListPtr</c> and parent <see cref="IShellFolder" />. /// </summary> /// <param name="idListPtr"><c>IDListPtr</c>.</param> /// <param name="parentFolder">Parent <see cref="ShellFolderItem" />.</param> /// <returns><see cref="ShellItem" />.</returns> /// <exception cref="ArgumentNullException"><paramref name="idListPtr" /> is <see cref="IntPtr.Zero" />.</exception> /// <exception cref="ArgumentNullException"><paramref name="parentFolder" /> is <c>null</c>.</exception> /// <exception cref="ShellException">Failed to create <see cref="ShellItem" />.</exception> internal static ShellItem FromIdList(IntPtr idListPtr, ShellFolderItem parentFolder) { Contract.Requires <ArgumentNullException>(idListPtr != IntPtr.Zero); Contract.Requires <ArgumentNullException>(parentFolder != null); Contract.Ensures(Contract.Result <ShellItem>() != null); IShellItem shellItem; var code = ShellNativeMethods.SHCreateItemWithParent( IntPtr.Zero, parentFolder.ShellFolderInterface, idListPtr, ShellIIDGuid.IShellItem2, out shellItem); if (HRESULT.Failed(code)) { throw new ShellException(ErrorMessages.ShellFactoryUnableToCreateItem, Marshal.GetExceptionForHR(code)); } return(new ShellItem((IShellItem2)shellItem)); }
internal ShellThumbnail(ShellItem shellItem, IntPtr imageHandle, double width, double height) { Contract.Requires <ArgumentNullException>(shellItem != null); Contract.Requires <ArgumentOutOfRangeException>(0.0 <= width); Contract.Requires <ArgumentOutOfRangeException>(0.0 <= height); this.OriginalWidth = width; this.OriginalHeight = height; this.ImageHandle = imageHandle; var sfi = new SHFILEINFO(); const uint flags = SHGFI.SHGFI_PIDL | SHGFI.SHGFI_ICON | SHGFI.SHGFI_SYSICONINDEX | SHGFI.SHGFI_OVERLAYINDEX; ShellNativeMethods.SHGetFileInfo(shellItem.PIDL, 0, ref sfi, flags); this.IconIndex = sfi.iIcon & 0x00FFFFFF; this.OverlayIndex = (sfi.iIcon >> 24) & 0xFF; }
/// <summary> /// Release the native objects. /// </summary> /// <param name="disposing"></param> protected virtual void Dispose(bool disposing) { if (disposing) { //_taskbarWindow = null; // < should never set this to null as it affects use externally if (Icon != null) { Icon.Dispose(); } Icon = null; _title = null; _tooltip = null; WindowsControl = null; } if (CurrentHBitmap != IntPtr.Zero) { ShellNativeMethods.DeleteObject(CurrentHBitmap); CurrentHBitmap = IntPtr.Zero; } }
/// <summary> /// Release the native objects. /// </summary> /// <param name="disposing"></param> protected virtual void Dispose(bool disposing) { if (disposing) { _taskbarWindow = null; if (Icon != null) { Icon.Dispose(); } Icon = null; _title = null; _tooltip = null; WindowsControl = null; } if (CurrentHBitmap != IntPtr.Zero) { ShellNativeMethods.DeleteObject(CurrentHBitmap); CurrentHBitmap = IntPtr.Zero; } }
// T7E: Gets property store for shortcut public string GetApplicationIdForShortcut(string shortcutPath) { // Get shell item IShellItem2 shellItem; Guid guid = new Guid(ShellIIDGuid.IShellItem2); int hrc = ShellNativeMethods.SHCreateItemFromParsingName(shortcutPath, IntPtr.Zero, ref guid, out shellItem); // Get property store Guid psGuid = new Guid(ShellIIDGuid.IPropertyStore); IPropertyStore propStore = null; int hr = shellItem.GetPropertyStore( ShellNativeMethods.GETPROPERTYSTOREFLAGS.GPS_READWRITE, ref psGuid, out propStore); // Get appid property PropVariant pv = new PropVariant(); PropertyKey AppIdKey = new PropertyKey(new Guid("9F4C2855-9F79-4B39-A8D0-E1D42DE1D5F3"), 5); propStore.GetValue(ref AppIdKey, out pv); string retrievedAppId; if (pv.IsNullOrEmpty) { retrievedAppId = ""; } else { retrievedAppId = (string)pv.Value; } Marshal.ReleaseComObject(propStore); pv.Clear(); return(retrievedAppId); }
/// <summary> /// Dispatches a window message so that the appropriate events /// can be invoked. This is used for the Taskbar's thumbnail toolbar feature. /// </summary> /// <param name="m">The window message, typically obtained /// from a Windows Forms or WPF window procedure.</param> /// <param name="taskbarWindow">Taskbar window for which we are intercepting the messages</param> /// <returns>Returns true if this method handles the window message</returns> internal bool DispatchMessage(ref Message m, TaskbarWindow taskbarWindow) { if (taskbarWindow.EnableThumbnailToolbars) { if (m.Msg == (int)TaskbarNativeMethods.WM_TASKBARBUTTONCREATED) { AddButtons(taskbarWindow); } else { if (!buttonsAdded) { AddButtons(taskbarWindow); } switch (m.Msg) { case TaskbarNativeMethods.WM_COMMAND: if (CoreNativeMethods.HIWORD(m.WParam.ToInt64(), 16) == THUMBBUTTON.THBN_CLICKED) { int buttonId = CoreNativeMethods.LOWORD(m.WParam.ToInt64()); var buttonsFound = from b in taskbarWindow.ThumbnailButtons where b.Id == buttonId select b; foreach (ThumbnailToolbarButton button in buttonsFound) { button.FireClick(taskbarWindow); } } break; } // End switch } // End else } // End if // If we are removed from the taskbar, ignore all the messages if (taskbarWindow.EnableTabbedThumbnails) { if (taskbarWindow.TabbedThumbnail.RemovedFromTaskbar) { return(false); } else if (m.Msg == (int)TabbedThumbnailNativeMethods.WM_ACTIVATE) { // Raise the event taskbarWindow.TabbedThumbnail.OnTabbedThumbnailActivated(); SetActiveTab(taskbarWindow); return(true); } else if (m.Msg == (int)TaskbarNativeMethods.WM_DWMSENDICONICTHUMBNAIL) { int width = (int)((long)m.LParam >> 16); int height = (int)(((long)m.LParam) & (0xFFFF)); Size requestedSize = new Size(width, height); // Fire an event to let the user update their bitmap // Raise the event taskbarWindow.TabbedThumbnail.OnTabbedThumbnailBitmapRequested(); IntPtr hBitmap = IntPtr.Zero; // Default size for the thumbnail Size realWindowSize = new Size(200, 200); if (taskbarWindow.TabbedThumbnail.WindowHandle != IntPtr.Zero) { TabbedThumbnailNativeMethods.GetClientSize(taskbarWindow.TabbedThumbnail.WindowHandle, out realWindowSize); } else if (taskbarWindow.TabbedThumbnail.WindowsControl != null) { realWindowSize = new Size( Convert.ToInt32(taskbarWindow.TabbedThumbnail.WindowsControl.RenderSize.Width), Convert.ToInt32(taskbarWindow.TabbedThumbnail.WindowsControl.RenderSize.Height)); } if ((realWindowSize.Height == -1) && (realWindowSize.Width == -1)) { realWindowSize.Width = realWindowSize.Height = 199; } // capture the bitmap for the given control // If the user has already specified us a bitmap to use, use that. if (taskbarWindow.TabbedThumbnail.ClippingRectangle != null && taskbarWindow.TabbedThumbnail.ClippingRectangle.Value != Rectangle.Empty) { if (taskbarWindow.TabbedThumbnail.CurrentHBitmap == IntPtr.Zero) { hBitmap = GrabBitmap(taskbarWindow, realWindowSize); } else { hBitmap = taskbarWindow.TabbedThumbnail.CurrentHBitmap; } // Clip the bitmap we just got Bitmap bmp = Bitmap.FromHbitmap(hBitmap); Rectangle clippingRectangle = taskbarWindow.TabbedThumbnail.ClippingRectangle.Value; // If our clipping rect is out of bounds, update it if (clippingRectangle.Height > requestedSize.Height) { clippingRectangle.Height = requestedSize.Height; } if (clippingRectangle.Width > requestedSize.Width) { clippingRectangle.Width = requestedSize.Width; } bmp = bmp.Clone(clippingRectangle, bmp.PixelFormat); // Make sure we dispose the bitmap before assigning, otherwise we'll have a memory leak if (hBitmap != IntPtr.Zero && taskbarWindow.TabbedThumbnail.CurrentHBitmap == IntPtr.Zero) { ShellNativeMethods.DeleteObject(hBitmap); } hBitmap = bmp.GetHbitmap(); } else { // Else, user didn't want any clipping, if they haven't provided us a bitmap, // use the screencapture utility and capture it. hBitmap = taskbarWindow.TabbedThumbnail.CurrentHBitmap; // If no bitmap, capture one using the utility if (hBitmap == IntPtr.Zero) { hBitmap = GrabBitmap(taskbarWindow, realWindowSize); } } // Only set the thumbnail if it's not null. // If it's null (either we didn't get the bitmap or size was 0), // let DWM handle it if (hBitmap != IntPtr.Zero) { Bitmap temp = TabbedThumbnailScreenCapture.ResizeImageWithAspect(hBitmap, requestedSize.Width, requestedSize.Height, true); if (taskbarWindow.TabbedThumbnail.CurrentHBitmap == IntPtr.Zero) { ShellNativeMethods.DeleteObject(hBitmap); } hBitmap = temp.GetHbitmap(); TabbedThumbnailNativeMethods.SetIconicThumbnail(taskbarWindow.WindowToTellTaskbarAbout, hBitmap); temp.Dispose(); } // If the bitmap we have is not coming from the user (i.e. we created it here), // then make sure we delete it as we don't need it now. if (taskbarWindow.TabbedThumbnail.CurrentHBitmap == IntPtr.Zero) { ShellNativeMethods.DeleteObject(hBitmap); } return(true); } else if (m.Msg == (int)TaskbarNativeMethods.WM_DWMSENDICONICLIVEPREVIEWBITMAP) { // Try to get the width/height int width = (int)(((long)m.LParam) >> 16); int height = (int)(((long)m.LParam) & (0xFFFF)); // Default size for the thumbnail Size realWindowSize = new Size(200, 200); if (taskbarWindow.TabbedThumbnail.WindowHandle != IntPtr.Zero) { TabbedThumbnailNativeMethods.GetClientSize(taskbarWindow.TabbedThumbnail.WindowHandle, out realWindowSize); } else if (taskbarWindow.TabbedThumbnail.WindowsControl != null) { realWindowSize = new Size( Convert.ToInt32(taskbarWindow.TabbedThumbnail.WindowsControl.RenderSize.Width), Convert.ToInt32(taskbarWindow.TabbedThumbnail.WindowsControl.RenderSize.Height)); } // If we don't have a valid height/width, use the original window's size if (width <= 0) { width = realWindowSize.Width; } if (height <= 0) { height = realWindowSize.Height; } // Fire an event to let the user update their bitmap // Raise the event taskbarWindow.TabbedThumbnail.OnTabbedThumbnailBitmapRequested(); // capture the bitmap for the given control // If the user has already specified us a bitmap to use, use that. IntPtr hBitmap = taskbarWindow.TabbedThumbnail.CurrentHBitmap == IntPtr.Zero ? GrabBitmap(taskbarWindow, realWindowSize) : taskbarWindow.TabbedThumbnail.CurrentHBitmap; // If we have a valid parent window handle, // calculate the offset so we can place the "peek" bitmap // correctly on the app window if (taskbarWindow.TabbedThumbnail.ParentWindowHandle != IntPtr.Zero && taskbarWindow.TabbedThumbnail.WindowHandle != IntPtr.Zero) { Point offset = new Point(); // if we don't have a offset specified already by the user... if (!taskbarWindow.TabbedThumbnail.PeekOffset.HasValue) { offset = WindowUtilities.GetParentOffsetOfChild(taskbarWindow.TabbedThumbnail.WindowHandle, taskbarWindow.TabbedThumbnail.ParentWindowHandle); } else { offset = new Point(Convert.ToInt32(taskbarWindow.TabbedThumbnail.PeekOffset.Value.X), Convert.ToInt32(taskbarWindow.TabbedThumbnail.PeekOffset.Value.Y)); } // Only set the peek bitmap if it's not null. // If it's null (either we didn't get the bitmap or size was 0), // let DWM handle it if (hBitmap != IntPtr.Zero) { if (offset.X >= 0 && offset.Y >= 0) { TabbedThumbnailNativeMethods.SetPeekBitmap(taskbarWindow.WindowToTellTaskbarAbout, hBitmap, offset, taskbarWindow.TabbedThumbnail.DisplayFrameAroundBitmap); } } // If the bitmap we have is not coming from the user (i.e. we created it here), // then make sure we delete it as we don't need it now. if (taskbarWindow.TabbedThumbnail.CurrentHBitmap == IntPtr.Zero) { ShellNativeMethods.DeleteObject(hBitmap); } return(true); } // Else, we don't have a valid window handle from the user. This is mostly likely because // we have a WPF UIElement control. If that's the case, use a different screen capture method // and also couple of ways to try to calculate the control's offset w.r.t it's parent. else if (taskbarWindow.TabbedThumbnail.ParentWindowHandle != IntPtr.Zero && taskbarWindow.TabbedThumbnail.WindowsControl != null) { System.Windows.Point offset; if (!taskbarWindow.TabbedThumbnail.PeekOffset.HasValue) { // Calculate the offset for a WPF UIElement control // For hidden controls, we can't seem to perform the transform. GeneralTransform objGeneralTransform = taskbarWindow.TabbedThumbnail.WindowsControl.TransformToVisual(taskbarWindow.TabbedThumbnail.WindowsControlParentWindow); offset = objGeneralTransform.Transform(new System.Windows.Point(0, 0)); } else { offset = new System.Windows.Point(taskbarWindow.TabbedThumbnail.PeekOffset.Value.X, taskbarWindow.TabbedThumbnail.PeekOffset.Value.Y); } // Only set the peek bitmap if it's not null. // If it's null (either we didn't get the bitmap or size was 0), // let DWM handle it if (hBitmap != IntPtr.Zero) { if (offset.X >= 0 && offset.Y >= 0) { TabbedThumbnailNativeMethods.SetPeekBitmap(taskbarWindow.WindowToTellTaskbarAbout, hBitmap, new Point((int)offset.X, (int)offset.Y), taskbarWindow.TabbedThumbnail.DisplayFrameAroundBitmap); } else { TabbedThumbnailNativeMethods.SetPeekBitmap(taskbarWindow.WindowToTellTaskbarAbout, hBitmap, taskbarWindow.TabbedThumbnail.DisplayFrameAroundBitmap); } } // If the bitmap we have is not coming from the user (i.e. we created it here), // then make sure we delete it as we don't need it now. if (taskbarWindow.TabbedThumbnail.CurrentHBitmap == IntPtr.Zero) { ShellNativeMethods.DeleteObject(hBitmap); } return(true); } else { // Else (no parent specified), just set the bitmap. It would take over the entire // application window (would work only if you are a MDI app) // Only set the peek bitmap if it's not null. // If it's null (either we didn't get the bitmap or size was 0), // let DWM handle it if (hBitmap != null) { TabbedThumbnailNativeMethods.SetPeekBitmap(taskbarWindow.WindowToTellTaskbarAbout, hBitmap, taskbarWindow.TabbedThumbnail.DisplayFrameAroundBitmap); } // If the bitmap we have is not coming from the user (i.e. we created it here), // then make sure we delete it as we don't need it now. if (taskbarWindow.TabbedThumbnail.CurrentHBitmap == IntPtr.Zero) { ShellNativeMethods.DeleteObject(hBitmap); } return(true); } } else if (m.Msg == (int)TabbedThumbnailNativeMethods.WM_DESTROY) { TaskbarManager.Instance.TaskbarList.UnregisterTab(taskbarWindow.WindowToTellTaskbarAbout); taskbarWindow.TabbedThumbnail.RemovedFromTaskbar = true; return(true); } else if (m.Msg == (int)TabbedThumbnailNativeMethods.WM_NCDESTROY) { // Raise the event taskbarWindow.TabbedThumbnail.OnTabbedThumbnailClosed(); // Remove the taskbar window from our internal list if (taskbarWindowList.Contains(taskbarWindow)) { taskbarWindowList.Remove(taskbarWindow); } taskbarWindow.Dispose(); taskbarWindow = null; return(true); } else if (m.Msg == (int)TabbedThumbnailNativeMethods.WM_SYSCOMMAND) { if (((int)m.WParam) == TabbedThumbnailNativeMethods.SC_CLOSE) { // Raise the event taskbarWindow.TabbedThumbnail.OnTabbedThumbnailClosed(); // Remove the taskbar window from our internal list if (taskbarWindowList.Contains(taskbarWindow)) { taskbarWindowList.Remove(taskbarWindow); } taskbarWindow.Dispose(); taskbarWindow = null; } else if (((int)m.WParam) == TabbedThumbnailNativeMethods.SC_MAXIMIZE) { // Raise the event taskbarWindow.TabbedThumbnail.OnTabbedThumbnailMaximized(); } else if (((int)m.WParam) == TabbedThumbnailNativeMethods.SC_MINIMIZE) { // Raise the event taskbarWindow.TabbedThumbnail.OnTabbedThumbnailMinimized(); } return(true); } } return(false); }
private static bool DispatchLivePreviewBitmapMessage(ref System.Windows.Forms.Message m, TaskbarWindow taskbarWindow) { if (m.Msg == (int)TaskbarNativeMethods.WmDwmSendIconicLivePreviewBitmap) { // Try to get the width/height int width = (int)(((long)m.LParam) >> 16); int height = (int)(((long)m.LParam) & (0xFFFF)); // Default size for the thumbnail Size realWindowSize = new Size(200, 200); if (taskbarWindow.TabbedThumbnail.WindowHandle != IntPtr.Zero) { TabbedThumbnailNativeMethods.GetClientSize(taskbarWindow.TabbedThumbnail.WindowHandle, out realWindowSize); } else if (taskbarWindow.TabbedThumbnail.WindowsControl != null) { realWindowSize = new Size( Convert.ToInt32(taskbarWindow.TabbedThumbnail.WindowsControl.RenderSize.Width), Convert.ToInt32(taskbarWindow.TabbedThumbnail.WindowsControl.RenderSize.Height)); } // If we don't have a valid height/width, use the original window's size if (width <= 0) { width = realWindowSize.Width; } if (height <= 0) { height = realWindowSize.Height; } // Fire an event to let the user update their bitmap // Raise the event taskbarWindow.TabbedThumbnail.OnTabbedThumbnailBitmapRequested(); // capture the bitmap for the given control // If the user has already specified us a bitmap to use, use that. IntPtr hBitmap = taskbarWindow.TabbedThumbnail.CurrentHBitmap == IntPtr.Zero ? GrabBitmap(taskbarWindow, realWindowSize) : taskbarWindow.TabbedThumbnail.CurrentHBitmap; // If we have a valid parent window handle, // calculate the offset so we can place the "peek" bitmap // correctly on the app window if (taskbarWindow.TabbedThumbnail.ParentWindowHandle != IntPtr.Zero && taskbarWindow.TabbedThumbnail.WindowHandle != IntPtr.Zero) { System.Drawing.Point offset = new System.Drawing.Point(); // if we don't have a offset specified already by the user... if (!taskbarWindow.TabbedThumbnail.PeekOffset.HasValue) { offset = WindowUtilities.GetParentOffsetOfChild(taskbarWindow.TabbedThumbnail.WindowHandle, taskbarWindow.TabbedThumbnail.ParentWindowHandle); } else { offset = new System.Drawing.Point(Convert.ToInt32(taskbarWindow.TabbedThumbnail.PeekOffset.Value.X), Convert.ToInt32(taskbarWindow.TabbedThumbnail.PeekOffset.Value.Y)); } // Only set the peek bitmap if it's not null. // If it's null (either we didn't get the bitmap or size was 0), // let DWM handle it if (hBitmap != IntPtr.Zero) { if (offset.X >= 0 && offset.Y >= 0) { TabbedThumbnailNativeMethods.SetPeekBitmap( taskbarWindow.WindowToTellTaskbarAbout, hBitmap, offset, taskbarWindow.TabbedThumbnail.DisplayFrameAroundBitmap); } } // If the bitmap we have is not coming from the user (i.e. we created it here), // then make sure we delete it as we don't need it now. if (taskbarWindow.TabbedThumbnail.CurrentHBitmap == IntPtr.Zero) { ShellNativeMethods.DeleteObject(hBitmap); } return(true); } // Else, we don't have a valid window handle from the user. This is mostly likely because // we have a WPF UIElement control. If that's the case, use a different screen capture method // and also couple of ways to try to calculate the control's offset w.r.t it's parent. else if (taskbarWindow.TabbedThumbnail.ParentWindowHandle != IntPtr.Zero && taskbarWindow.TabbedThumbnail.WindowsControl != null) { System.Windows.Point offset; if (!taskbarWindow.TabbedThumbnail.PeekOffset.HasValue) { // Calculate the offset for a WPF UIElement control // For hidden controls, we can't seem to perform the transform. GeneralTransform objGeneralTransform = taskbarWindow.TabbedThumbnail.WindowsControl.TransformToVisual(taskbarWindow.TabbedThumbnail.WindowsControlParentWindow); offset = objGeneralTransform.Transform(new System.Windows.Point(0, 0)); } else { offset = new System.Windows.Point(taskbarWindow.TabbedThumbnail.PeekOffset.Value.X, taskbarWindow.TabbedThumbnail.PeekOffset.Value.Y); } // Only set the peek bitmap if it's not null. // If it's null (either we didn't get the bitmap or size was 0), // let DWM handle it if (hBitmap != IntPtr.Zero) { if (offset.X >= 0 && offset.Y >= 0) { TabbedThumbnailNativeMethods.SetPeekBitmap( taskbarWindow.WindowToTellTaskbarAbout, hBitmap, new System.Drawing.Point((int)offset.X, (int)offset.Y), taskbarWindow.TabbedThumbnail.DisplayFrameAroundBitmap); } else { TabbedThumbnailNativeMethods.SetPeekBitmap( taskbarWindow.WindowToTellTaskbarAbout, hBitmap, taskbarWindow.TabbedThumbnail.DisplayFrameAroundBitmap); } } // If the bitmap we have is not coming from the user (i.e. we created it here), // then make sure we delete it as we don't need it now. if (taskbarWindow.TabbedThumbnail.CurrentHBitmap == IntPtr.Zero) { ShellNativeMethods.DeleteObject(hBitmap); } return(true); } else { // Else (no parent specified), just set the bitmap. It would take over the entire // application window (would work only if you are a MDI app) // Only set the peek bitmap if it's not null. // If it's null (either we didn't get the bitmap or size was 0), // let DWM handle it if (hBitmap != null) { TabbedThumbnailNativeMethods.SetPeekBitmap(taskbarWindow.WindowToTellTaskbarAbout, hBitmap, taskbarWindow.TabbedThumbnail.DisplayFrameAroundBitmap); } // If the bitmap we have is not coming from the user (i.e. we created it here), // then make sure we delete it as we don't need it now. if (taskbarWindow.TabbedThumbnail.CurrentHBitmap == IntPtr.Zero) { ShellNativeMethods.DeleteObject(hBitmap); } return(true); } } return(false); }
private static bool DispatchSendIconThumbnailMessage(ref System.Windows.Forms.Message m, TaskbarWindow taskbarWindow) { if (m.Msg == (int)TaskbarNativeMethods.WmDwmSendIconThumbnail) { int width = (int)((long)m.LParam >> 16); int height = (int)(((long)m.LParam) & (0xFFFF)); Size requestedSize = new Size(width, height); // Fire an event to let the user update their bitmap taskbarWindow.TabbedThumbnail.OnTabbedThumbnailBitmapRequested(); IntPtr hBitmap = IntPtr.Zero; // Default size for the thumbnail Size realWindowSize = new Size(200, 200); // Get the size of teh control or UIElement if (taskbarWindow.TabbedThumbnail.WindowHandle != IntPtr.Zero) { TabbedThumbnailNativeMethods.GetClientSize(taskbarWindow.TabbedThumbnail.WindowHandle, out realWindowSize); } else if (taskbarWindow.TabbedThumbnail.WindowsControl != null) { realWindowSize = new Size( Convert.ToInt32(taskbarWindow.TabbedThumbnail.WindowsControl.RenderSize.Width), Convert.ToInt32(taskbarWindow.TabbedThumbnail.WindowsControl.RenderSize.Height)); } if (realWindowSize.Height == -1 && realWindowSize.Width == -1) { realWindowSize.Width = realWindowSize.Height = 199; } // capture the bitmap for the given control // If the user has already specified us a bitmap to use, use that. if (taskbarWindow.TabbedThumbnail.ClippingRectangle != null && taskbarWindow.TabbedThumbnail.ClippingRectangle.Value != Rectangle.Empty) { if (taskbarWindow.TabbedThumbnail.CurrentHBitmap == IntPtr.Zero) { hBitmap = GrabBitmap(taskbarWindow, realWindowSize); } else { hBitmap = taskbarWindow.TabbedThumbnail.CurrentHBitmap; } // Clip the bitmap we just got. Bitmap bmp = Bitmap.FromHbitmap(hBitmap); Rectangle clippingRectangle = taskbarWindow.TabbedThumbnail.ClippingRectangle.Value; // If our clipping rect is out of bounds, update it if (clippingRectangle.Height > requestedSize.Height) { clippingRectangle.Height = requestedSize.Height; } if (clippingRectangle.Width > requestedSize.Width) { clippingRectangle.Width = requestedSize.Width; } // NOTE: Is this a memory leak? bmp = bmp.Clone(clippingRectangle, bmp.PixelFormat); // Make sure we dispose the bitmap before assigning, otherwise we'll have a memory leak if (hBitmap != IntPtr.Zero && taskbarWindow.TabbedThumbnail.CurrentHBitmap == IntPtr.Zero) { ShellNativeMethods.DeleteObject(hBitmap); } hBitmap = bmp.GetHbitmap(); bmp.Dispose(); } else { // Else, user didn't want any clipping, if they haven't provided us a bitmap, // use the screencapture utility and capture it. hBitmap = taskbarWindow.TabbedThumbnail.CurrentHBitmap; // If no bitmap, capture one using the utility if (hBitmap == IntPtr.Zero) { hBitmap = GrabBitmap(taskbarWindow, realWindowSize); } } // Only set the thumbnail if it's not null. // If it's null (either we didn't get the bitmap or size was 0), // let DWM handle it if (hBitmap != IntPtr.Zero) { Bitmap temp = TabbedThumbnailScreenCapture.ResizeImageWithAspect( hBitmap, requestedSize.Width, requestedSize.Height, true); if (taskbarWindow.TabbedThumbnail.CurrentHBitmap == IntPtr.Zero) { ShellNativeMethods.DeleteObject(hBitmap); } hBitmap = temp.GetHbitmap(); TabbedThumbnailNativeMethods.SetIconicThumbnail(taskbarWindow.WindowToTellTaskbarAbout, hBitmap); temp.Dispose(); } // If the bitmap we have is not coming from the user (i.e. we created it here), // then make sure we delete it as we don't need it now. if (taskbarWindow.TabbedThumbnail.CurrentHBitmap == IntPtr.Zero) { ShellNativeMethods.DeleteObject(hBitmap); } return(true); } return(false); }
private void ApplyNativeSettings(IFileDialog dialog) { Debug.Assert(dialog != null, "No dialog instance to configure"); if (parentWindow == IntPtr.Zero) { if (System.Windows.Application.Current != null && System.Windows.Application.Current.MainWindow != null) { parentWindow = (new WindowInteropHelper(System.Windows.Application.Current.MainWindow)).Handle; } } Guid guid = new Guid(ShellIIDGuid.IShellItem2); // Apply option bitflags. dialog.SetOptions(CalculateNativeDialogOptionFlags()); // Other property sets. if (title != null) { dialog.SetTitle(title); } if (initialDirectoryShellContainer != null) { dialog.SetFolder(((ShellObject)initialDirectoryShellContainer).NativeShellItem); } if (defaultDirectoryShellContainer != null) { dialog.SetDefaultFolder(((ShellObject)defaultDirectoryShellContainer).NativeShellItem); } if (!string.IsNullOrEmpty(initialDirectory)) { // Create a native shellitem from our path IShellItem2 initialDirectoryShellItem; ShellNativeMethods.SHCreateItemFromParsingName(initialDirectory, IntPtr.Zero, ref guid, out initialDirectoryShellItem); // If we get a real shell item back, // then use that as the initial folder - otherwise, // we'll allow the dialog to revert to the default folder. // (OR should we fail loudly?) if (initialDirectoryShellItem != null) { dialog.SetFolder(initialDirectoryShellItem); } } if (!string.IsNullOrEmpty(defaultDirectory)) { // Create a native shellitem from our path IShellItem2 defaultDirectoryShellItem; ShellNativeMethods.SHCreateItemFromParsingName(defaultDirectory, IntPtr.Zero, ref guid, out defaultDirectoryShellItem); // If we get a real shell item back, // then use that as the initial folder - otherwise, // we'll allow the dialog to revert to the default folder. // (OR should we fail loudly?) if (defaultDirectoryShellItem != null) { dialog.SetDefaultFolder(defaultDirectoryShellItem); } } // Apply file type filters, if available. if (filters.Count > 0 && !filterSet) { dialog.SetFileTypes( (uint)filters.Count, filters.GetAllFilterSpecs()); filterSet = true; SyncFileTypeComboToDefaultExtension(dialog); } if (cookieIdentifier != Guid.Empty) { dialog.SetClientGuid(ref cookieIdentifier); } // Set the default extension if (!string.IsNullOrEmpty(DefaultExtension)) { dialog.SetDefaultExtension(DefaultExtension); } // Set the default filename dialog.SetFileName(DefaultFileName); }