protected virtual async Task <bool> CreateImages(IntPtr handle) { Logger.Write(this, LogLevel.Debug, "Creating taskbar button image list."); var source = HwndSource.FromHwnd(handle); var width = WindowsImageList.GetSystemMetrics( WindowsImageList.SystemMetric.SM_CXSMICON ); var height = WindowsImageList.GetSystemMetrics( WindowsImageList.SystemMetric.SM_CYSMICON ); Logger.Write(this, LogLevel.Debug, "Taskbar buttom image dimentions: {0}x{1}", width, height); var imageList = WindowsImageList.ImageList_Create( width, height, WindowsImageList.ILC_COLOR32, 4, 0 ); if (IntPtr.Zero.Equals(imageList)) { Logger.Write(this, LogLevel.Warn, "Failed to create button image list."); this.Windows[handle] |= TaskbarButtonsWindowFlags.Error; return(false); } else { this.ImageLists[handle] = imageList; } for (var a = 0; a < 4; a++) { using (var bitmap = this.GetImage(a, width, height)) { if (!this.AddImage(handle, imageList, bitmap, width, height)) { return(false); } } } var result = default(WindowsTaskbarList.HResult); await this.Invoke( source.Dispatcher, () => result = WindowsTaskbarList.Instance.ThumbBarSetImageList(handle, imageList) ).ConfigureAwait(false); if (result != WindowsTaskbarList.HResult.Ok) { Logger.Write(this, LogLevel.Warn, "Failed to create button image list: {0}", Enum.GetName(typeof(WindowsTaskbarList.HResult), result)); this.Windows[handle] |= TaskbarButtonsWindowFlags.Error; return(false); } else { Logger.Write(this, LogLevel.Debug, "Taskbar button image list created."); this.Windows[handle] |= TaskbarButtonsWindowFlags.ImagesCreated; return(true); } }
protected virtual bool DestroyImages(IntPtr handle) { var imageList = default(IntPtr); if (!this.ImageLists.TryGetValue(handle, out imageList)) { //There was no image list to destroy. return(true); } var result = WindowsImageList.ImageList_Destroy(imageList); if (!result) { Logger.Write(this, LogLevel.Warn, "Failed to destroy image list: {0}", Enum.GetName(typeof(WindowsTaskbarList.HResult), result)); this.Windows[handle] |= TaskbarButtonsWindowFlags.Error; return(false); } else { Logger.Write(this, LogLevel.Debug, "Taskbar button image list destroyed."); this.Windows[handle] &= ~TaskbarButtonsWindowFlags.ImagesCreated; return(true); } }
protected virtual bool AddImage(IntPtr handle, IntPtr imageList, Bitmap bitmap, int width, int height) { var bitmapBits = default(IntPtr); var bitmapInfo = new WindowsImageList.BITMAPINFO() { biSize = 40, biBitCount = 32, biPlanes = 1, biWidth = width, biHeight = height }; var bitmapSection = WindowsImageList.CreateDIBSection( IntPtr.Zero, bitmapInfo, 0, out bitmapBits, IntPtr.Zero, 0 ); if (IntPtr.Zero.Equals(bitmapSection)) { Logger.Write(this, LogLevel.Warn, "Failed to create DIB."); this.Windows[handle] |= TaskbarButtonsWindowFlags.Error; return(false); } var bitmapData = bitmap.LockBits( new Rectangle( 0, 0, bitmap.Width, bitmap.Height ), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb ); { var result = WindowsImageList.RtlMoveMemory( bitmapBits, bitmapData.Scan0, bitmap.Height * bitmapData.Stride ); if (!result) { Logger.Write(this, LogLevel.Warn, "Call to RtlMoveMemory reports failure."); this.Windows[handle] |= TaskbarButtonsWindowFlags.Error; return(false); } } bitmap.UnlockBits(bitmapData); { var result = WindowsImageList.ImageList_Add( imageList, bitmapSection, IntPtr.Zero ); if (result < 0) { Logger.Write(this, LogLevel.Warn, "Failed to add image to ImageList."); this.Windows[handle] |= TaskbarButtonsWindowFlags.Error; return(false); } } return(true); }