コード例 #1
0
        private static BitmapSource GetSystemIconImage(HierarchyItemIdentity item)
        {
            IVsProject project = item.NestedHierarchy as IVsProject;

            if (project != null)
            {
                string document;
                if (ErrorHandler.Succeeded(project.GetMkDocument(item.NestedItemID, out document)))
                {
                    SHFILEINFO shfi            = new SHFILEINFO();
                    uint       cbFileInfo      = (uint)Marshal.SizeOf(shfi);
                    IntPtr     systemImageList = NativeMethods.SHGetFileInfo(document, 0, ref shfi, cbFileInfo, SHGFI.SysIconIndex | SHGFI.SmallIcon);
                    if (systemImageList == IntPtr.Zero)
                    {
                        systemImageList = NativeMethods.SHGetFileInfo(document, 0, ref shfi, cbFileInfo, SHGFI.SysIconIndex | SHGFI.SmallIcon | SHGFI.UseFileAttributes);
                    }

                    if (systemImageList != IntPtr.Zero)
                    {
                        NativeImageList imageList = new NativeImageList(systemImageList);
                        return(imageList.GetImage(shfi.iIcon));
                    }
                }
            }

            return(null);
        }
コード例 #2
0
        public void ImageListStreamer_BinaryFormatter_Stream_BinaryFormatter_round_trip_equality()
        {
            // Create an ImageListStreamer via BinaryFormatter
            using ImageListStreamer streamerFromBf  = BinarySerialization.EnsureDeserialize <ImageListStreamer>(ClassicBfImageListStreamer);
            using NativeImageList nativeImageListBf = streamerFromBf.GetNativeImageList();
            Assert.NotEqual(IntPtr.Zero, nativeImageListBf.Handle);

            // Read as a memory stream
            using MemoryStream ms = new();
            streamerFromBf.GetObjectData(ms);
            string msBase64 = Convert.ToBase64String(ms.ToArray());

            // Create a new ImageListStreamer from the stream
            ms.Position = 0;
            using ImageListStreamer streamerFromMs  = new(ms);
            using NativeImageList nativeImageListMs = streamerFromMs.GetNativeImageList();
            Assert.NotEqual(IntPtr.Zero, nativeImageListMs.Handle);

            // Compare the two
            using ImageList imageListBf = new();
            imageListBf.ImageStream     = streamerFromBf;
            using ImageList imageListMs = new();
            imageListMs.ImageStream     = streamerFromMs;

            Assert.Equal(imageListBf.ColorDepth, imageListMs.ColorDepth);
            Assert.Equal(imageListBf.Images.Count, imageListMs.Images.Count);
            Assert.Equal(imageListBf.ImageSize, imageListMs.ImageSize);
        }
コード例 #3
0
        public void ImageListStreamer_RoundTripAndExchangeWithNet()
        {
            string netBlob;

            using (var imageList = new ImageList()
            {
                ImageSize = new Size(16, 16),
                TransparentColor = Color.White
            })
            {
                imageList.Images.Add(new Bitmap(16, 16));
                netBlob = BinarySerialization.ToBase64String(imageList.ImageStream);
            }

            // ensure we can deserialise NET serialised data and continue to match the payload
            ValidateResult(netBlob);
            // ensure we can deserialise NET Fx serialised data and continue to match the payload
            ValidateResult(ClassicImageListStreamer);

            void ValidateResult(string blob)
            {
                using ImageListStreamer result = BinarySerialization.EnsureDeserialize <ImageListStreamer>(blob);
                using (NativeImageList nativeImageList = result.GetNativeImageList())
                {
                    Assert.True(ComCtl32.ImageList.GetIconSize(new HandleRef(this, nativeImageList.Handle), out int x, out int y).IsTrue());
                    Assert.Equal(16, x);
                    Assert.Equal(16, y);
                    var imageInfo = new ComCtl32.IMAGEINFO();
                    Assert.True(ComCtl32.ImageList.GetImageInfo(new HandleRef(this, nativeImageList.Handle), 0, ref imageInfo).IsTrue());
                    Assert.False(imageInfo.hbmImage.IsNull);
                }
            }
        }
コード例 #4
0
ファイル: FrmStatus.cs プロジェクト: ErtyHackward/livedc
        public void UpdateAndShow(IStartItem startItem)
        {
            _startItem = startItem;

            if (!string.IsNullOrEmpty(startItem.Magnet.FileName))
            {
                iconPicture.Image = NativeImageList.TryGetLargeIcon(Path.GetExtension(startItem.Magnet.FileName));
                nameLabel.Text    = startItem.Magnet.FileName;

                if (startItem.Magnet.Size != 0)
                {
                    sizeLabel.Text = Utils.FormatBytes(startItem.Magnet.Size);
                }
                else
                {
                    sizeLabel.Text = null;
                }
            }
            else
            {
                iconPicture.Image = null;
                nameLabel.Text    = null;
                sizeLabel.Text    = null;
            }

            progressBar.Enabled = true;
            progressBar.Style   = ProgressBarStyle.Marquee;

            statusLabel.Text = "";
            UpdateStartButton();
            startButton.Enabled = false;
            Show();
            Activate();
        }
コード例 #5
0
        /// <include file='doc\ImageList.uex' path='docs/doc[@for="ImageList.CreateHandle"]/*' />
        /// <devdoc>
        ///     Creates the underlying HIMAGELIST handle, and sets up all the
        ///     appropriate values with it.  Inheriting classes overriding this method
        ///     should not forget to call base.createHandle();
        /// </devdoc>
        private void CreateHandle()
        {
            // so we don't reinit while we're doing this...
            nativeImageList = null;
            SafeNativeMethods.InitCommonControls();

            int flags = NativeMethods.ILC_MASK;

            switch (colorDepth)
            {
            case ColorDepth.Depth4Bit:
                flags |= NativeMethods.ILC_COLOR4;
                break;

            case ColorDepth.Depth8Bit:
                flags |= NativeMethods.ILC_COLOR8;
                break;

            case ColorDepth.Depth16Bit:
                flags |= NativeMethods.ILC_COLOR16;
                break;

            case ColorDepth.Depth24Bit:
                flags |= NativeMethods.ILC_COLOR24;
                break;

            case ColorDepth.Depth32Bit:
                flags |= NativeMethods.ILC_COLOR32;
                break;

            default:
                Debug.Fail("Unknown color depth in ImageList");
                break;
            }

            nativeImageList = new NativeImageList(SafeNativeMethods.ImageList_Create(imageSize.Width, imageSize.Height, flags, INITIAL_CAPACITY, GROWBY));

            if (Handle == IntPtr.Zero)
            {
                throw new InvalidOperationException(SR.GetString(SR.ImageListCreateFailed));
            }
            SafeNativeMethods.ImageList_SetBkColor(new HandleRef(this, Handle), NativeMethods.CLR_NONE);

            Debug.Assert(originals != null, "Handle not yet created, yet original images are gone");
            for (int i = 0; i < originals.Count; i++)
            {
                Original original = (Original)originals[i];
                if (original.image is Icon)
                {
                    AddIconToHandle(original, (Icon)original.image);
                }
                else
                {
                    Bitmap bitmapValue = CreateBitmap(original);
                    AddToHandle(original, bitmapValue);
                }
            }
            originals = null;
        }
コード例 #6
0
 // Don't merge this function into Dispose() -- that base.Dispose() will damage the design time experience
 private void DestroyHandle()
 {
     if (HandleCreated)
     {
         // NOTE! No need to call ImageList_Destroy. It will be called in the NativeImageList finalizer.
         nativeImageList = null;
         originals       = new ArrayList();
     }
 }
コード例 #7
0
 private void DestroyHandle()
 {
     if (this.HandleCreated)
     {
         this.nativeImageList.Dispose();
         this.nativeImageList = null;
         this.originals       = new ArrayList();
     }
 }
コード例 #8
0
        public void NativeImageList_Dispose_releases_native_handle()
        {
            using ImageListStreamer result = BinarySerialization.EnsureDeserialize <ImageListStreamer>(ClassicImageListStreamer);

            NativeImageList nativeImageList = result.GetNativeImageList();

            Assert.NotEqual(IntPtr.Zero, nativeImageList.Handle);

            nativeImageList.Dispose();
            Assert.Equal(IntPtr.Zero, nativeImageList.Handle);
        }
コード例 #9
0
        private void ValidateImageListStreamer(string blob)
        {
            var result = BinarySerialization.EnsureDeserialize <ImageListStreamer>(blob);

            using (NativeImageList nativeImageList = result.GetNativeImageList())
            {
                Assert.True(SafeNativeMethods.ImageList_GetIconSize(new HandleRef(this, nativeImageList.Handle), out int x, out int y));
                Assert.Equal(16, x);
                Assert.Equal(16, y);
                NativeMethods.IMAGEINFO imageInfo = new NativeMethods.IMAGEINFO();
                Assert.True(SafeNativeMethods.ImageList_GetImageInfo(new HandleRef(this, nativeImageList.Handle), 0, imageInfo));
                Assert.True(IntPtr.Zero != imageInfo.hbmImage);
            }
        }
コード例 #10
0
ファイル: FrmStatus.cs プロジェクト: ErtyHackward/livedc
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (_startItem == null)
            {
                return;
            }

            startButton.Enabled = _startItem.ReadyToStart;
            statusLabel.Text    = _startItem.StatusMessage;

            if (float.IsNaN(_startItem.Progress))
            {
                progressBar.Enabled = false;
            }
            else if (float.IsPositiveInfinity(_startItem.Progress))
            {
                progressBar.Enabled = true;
                progressBar.Style   = ProgressBarStyle.Marquee;
            }
            else
            {
                progressBar.Enabled = true;
                progressBar.Style   = ProgressBarStyle.Continuous;
                progressBar.Value   = (int)(100 * _startItem.Progress);
            }

            if (nameLabel.Text != _startItem.Magnet.FileName && !string.IsNullOrEmpty(_startItem.Magnet.FileName))
            {
                iconPicture.Image = NativeImageList.TryGetLargeIcon(Path.GetExtension(_startItem.Magnet.FileName));
                nameLabel.Text    = _startItem.Magnet.FileName;
            }

            if (string.IsNullOrEmpty(sizeLabel.Text) && _startItem.Magnet.Size != 0)
            {
                sizeLabel.Text = Utils.FormatBytes(_startItem.Magnet.Size);
            }

            if (!_startItem.Closed)
            {
                _startItem.MainThreadAction(this);
            }

            if (_startItem.Closed)
            {
                Close();
                _startItem = null;
            }
        }
コード例 #11
0
        private void ResultsDataGridViewCellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
        {
            ISearchResult result;

            lock (_results)
            {
                result = _list[e.RowIndex];
            }

            var hsr       = result as HubSearchResult;
            var dcsResult = result as WebSearchResult;

            if (e.ColumnIndex == IconColumn.Index)
            {
                if (dcsResult != null)
                {
                    e.Value = dcsResult.Poster;
                }
                else
                {
                    e.Value = NativeImageList.TryGetLargeIcon(Path.GetExtension(result.Name));
                }
            }
            else if (e.ColumnIndex == FileNameColumn.Index)
            {
                e.Value = result.Name;
            }
            else if (e.ColumnIndex == SourcesColumn.Index)
            {
                if (hsr != null)
                {
                    e.Value = hsr.Sources.Count;
                }
                else
                {
                    e.Value = 0;
                }
            }
            else if (e.ColumnIndex == SizeColumn.Index)
            {
                e.Value = result.Size;
            }
        }
コード例 #12
0
        private static BitmapSource GetImageFromHierarchy(HierarchyItemIdentity item, int iconIndexProperty, int iconHandleProperty)
        {
            int          iconIndex;
            IntPtr       iconHandle;
            IntPtr       iconImageList;
            BitmapSource iconBitmapSource = null;

            IVsHierarchy iconSourceHierarchy = item.Hierarchy;
            uint         iconSourceItemid    = item.ItemID;

            if (item.IsNestedItem)
            {
                bool useNestedHierarchyIconList;
                if (TryGetHierarchyProperty(item.Hierarchy, item.ItemID, (int)__VSHPROPID2.VSHPROPID_UseInnerHierarchyIconList, out useNestedHierarchyIconList) && useNestedHierarchyIconList)
                {
                    iconSourceHierarchy = item.NestedHierarchy;
                    iconSourceItemid    = item.NestedItemID;
                }
            }

            if (TryGetHierarchyProperty(iconSourceHierarchy, iconSourceItemid, iconIndexProperty, out iconIndex) &&
                TryGetHierarchyProperty(iconSourceHierarchy, VSConstants.VSITEMID_ROOT, (int)__VSHPROPID.VSHPROPID_IconImgList, UnboxAsIntPtr, out iconImageList))
            {
                NativeImageList imageList = new NativeImageList(iconImageList);
                iconBitmapSource = imageList.GetImage(iconIndex);
            }
            else if (TryGetHierarchyProperty(item.Hierarchy, item.ItemID, iconHandleProperty, UnboxAsIntPtr, out iconHandle))
            {
                // Don't call DestroyIcon on iconHandle, as it's a shared resource owned by the hierarchy
                iconBitmapSource = Imaging.CreateBitmapSourceFromHIcon(iconHandle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
                iconBitmapSource.Freeze();
            }

            if (iconBitmapSource == null)
            {
                iconBitmapSource = GetSystemIconImage(item);
            }

            return(iconBitmapSource);
        }
コード例 #13
0
        public FrmTorrentFiles(Torrent torrent)
        {
            _torrent = torrent;
            InitializeComponent();

            _ao = AsyncOperationManager.CreateOperation(null);

            NativeImageList.SetListViewIconIndex(listView1.Handle);
            NativeImageList.SmallExtensionImageLoaded += NativeImageList_SmallExtensionImageLoaded;

            foreach (var torrentFile in _torrent.Files)
            {
                var lvi = new ListViewItem();

                lvi.Text = torrentFile.Path;
                lvi.SubItems.Add(Utils.FormatBytes(torrentFile.Length));
                lvi.Tag        = torrentFile;
                lvi.ImageIndex = NativeImageList.TryFileIconIndex(torrentFile.Path);

                listView1.Items.Add(lvi);
            }

            listView1.Sort();
        }
コード例 #14
0
        public void AddItem(Magnet item, DateTime createDate)
        {
            historyLabel.Visible = false;

            if (flowLayoutPanel1.Controls.Count > 2)
            {
                flowLayoutPanel1.Controls.RemoveAt(2);
            }

            var dcItem = new DcFileControl()
            {
                Name       = item.TTH,
                Magnet     = item,
                CreateDate = createDate
            };

            _client.UpdateFileItem(dcItem);

            dcItem.Icon             = NativeImageList.TryGetLargeIcon(Path.GetExtension(dcItem.Magnet.FileName));
            dcItem.ContextMenuStrip = contextMenuStrip1;
            dcItem.DoubleClick     += dcItem_DoubleClick;

            flowLayoutPanel1.Controls.Add(dcItem);
        }
コード例 #15
0
        public void ImageListStreamer_Stream_BinaryFormatter_compatible()
        {
            // Create a new ImageListStreamer from the stream
            byte[] bytes = Convert.FromBase64String(DevMsImageListStreamer);
            using MemoryStream ms = new(bytes);
            using ImageListStreamer streamerFromMs  = new(ms);
            using NativeImageList nativeImageListMs = streamerFromMs.GetNativeImageList();
            Assert.NotEqual(IntPtr.Zero, nativeImageListMs.Handle);

            // Create an ImageListStreamer via BinaryFormatter
            using ImageListStreamer streamerFromBf  = BinarySerialization.EnsureDeserialize <ImageListStreamer>(ClassicBfImageListStreamer);
            using NativeImageList nativeImageListBf = streamerFromBf.GetNativeImageList();
            Assert.NotEqual(IntPtr.Zero, nativeImageListBf.Handle);

            // Compare the two
            using ImageList imageListBf = new();
            imageListBf.ImageStream     = streamerFromBf;
            using ImageList imageListMs = new();
            imageListMs.ImageStream     = streamerFromMs;

            Assert.Equal(imageListBf.ColorDepth, imageListMs.ColorDepth);
            Assert.Equal(imageListBf.Images.Count, imageListMs.Images.Count);
            Assert.Equal(imageListBf.ImageSize, imageListMs.ImageSize);
        }
コード例 #16
0
ファイル: ImageList.cs プロジェクト: mind0n/hive
 // Don't merge this function into Dispose() -- that base.Dispose() will damage the design time experience
 private void DestroyHandle() {
     if (HandleCreated) {
         // Fix Dev10 TFS Bug 392946 -
         // ImageList/NativeImageList leaks HIMAGELIST until finalized
         nativeImageList.Dispose();
         nativeImageList = null;
         originals = new ArrayList();
     }
 }
コード例 #17
0
ファイル: ImageList.cs プロジェクト: mind0n/hive
        /// <include file='doc\ImageList.uex' path='docs/doc[@for="ImageList.CreateHandle"]/*' />
        /// <devdoc>
        ///     Creates the underlying HIMAGELIST handle, and sets up all the
        ///     appropriate values with it.  Inheriting classes overriding this method
        ///     should not forget to call base.createHandle();
        /// </devdoc>
        private void CreateHandle() {
            Debug.Assert(nativeImageList == null, "Handle already created, this may be a source of temporary GDI leaks");

            int flags = NativeMethods.ILC_MASK;
            switch (colorDepth) {
                case ColorDepth.Depth4Bit:
                    flags |= NativeMethods.ILC_COLOR4;
                    break;
                case ColorDepth.Depth8Bit:
                    flags |= NativeMethods.ILC_COLOR8;
                    break;
                case ColorDepth.Depth16Bit:
                    flags |= NativeMethods.ILC_COLOR16;
                    break;
                case ColorDepth.Depth24Bit:
                    flags |= NativeMethods.ILC_COLOR24;
                    break;
                case ColorDepth.Depth32Bit:
                    flags |= NativeMethods.ILC_COLOR32;
                    break;
                default:
                    Debug.Fail("Unknown color depth in ImageList");
                    break;
            }

            //VSW #123063: We enclose the imagelist handle create in a theming scope. This is a temporary solution
            //             till we tackle the bigger issue tracked by VSW #95247
            IntPtr userCookie = UnsafeNativeMethods.ThemingScope.Activate();

            try {
                SafeNativeMethods.InitCommonControls();
                nativeImageList = new NativeImageList(SafeNativeMethods.ImageList_Create(imageSize.Width, imageSize.Height, flags, INITIAL_CAPACITY, GROWBY));
            }
            finally {
                UnsafeNativeMethods.ThemingScope.Deactivate(userCookie);
            }

            if (Handle == IntPtr.Zero) throw new InvalidOperationException(SR.GetString(SR.ImageListCreateFailed));
            SafeNativeMethods.ImageList_SetBkColor(new HandleRef(this, Handle), NativeMethods.CLR_NONE);

            Debug.Assert(originals != null, "Handle not yet created, yet original images are gone");
            for (int i = 0; i < originals.Count; i++) {
                Original original = (Original) originals[i];
                if (original.image is Icon) {
                    AddIconToHandle(original, (Icon)original.image);
                    // NOTE: if we own the icon (it's been created by us) this WILL dispose the icon to avoid a GDI leak
                    // **** original.image is NOT LONGER VALID AFTER THIS POINT ***
                }
                else {
                    bool ownsBitmap = false;
                    Bitmap bitmapValue = CreateBitmap(original, out ownsBitmap);
                    AddToHandle(original, bitmapValue);
                    if(ownsBitmap)
                        bitmapValue.Dispose();
                }
            }
            originals = null;
        }
コード例 #18
0
 private void DestroyHandle()
 {
     if (this.HandleCreated)
     {
         this.nativeImageList.Dispose();
         this.nativeImageList = null;
         this.originals = new ArrayList();
     }
 }
コード例 #19
0
        private void CreateHandle()
        {
            int flags = 1;

            switch (this.colorDepth)
            {
            case System.Windows.Forms.ColorDepth.Depth16Bit:
                flags |= 0x10;
                break;

            case System.Windows.Forms.ColorDepth.Depth24Bit:
                flags |= 0x18;
                break;

            case System.Windows.Forms.ColorDepth.Depth32Bit:
                flags |= 0x20;
                break;

            case System.Windows.Forms.ColorDepth.Depth4Bit:
                flags |= 4;
                break;

            case System.Windows.Forms.ColorDepth.Depth8Bit:
                flags |= 8;
                break;
            }
            IntPtr userCookie = System.Windows.Forms.UnsafeNativeMethods.ThemingScope.Activate();

            try
            {
                System.Windows.Forms.SafeNativeMethods.InitCommonControls();
                this.nativeImageList = new NativeImageList(System.Windows.Forms.SafeNativeMethods.ImageList_Create(this.imageSize.Width, this.imageSize.Height, flags, 4, 4));
            }
            finally
            {
                System.Windows.Forms.UnsafeNativeMethods.ThemingScope.Deactivate(userCookie);
            }
            if (this.Handle == IntPtr.Zero)
            {
                throw new InvalidOperationException(System.Windows.Forms.SR.GetString("ImageListCreateFailed"));
            }
            System.Windows.Forms.SafeNativeMethods.ImageList_SetBkColor(new HandleRef(this, this.Handle), -1);
            for (int i = 0; i < this.originals.Count; i++)
            {
                Original original = (Original)this.originals[i];
                if (original.image is Icon)
                {
                    this.AddIconToHandle(original, (Icon)original.image);
                }
                else
                {
                    bool   ownsBitmap = false;
                    Bitmap bitmap     = this.CreateBitmap(original, out ownsBitmap);
                    this.AddToHandle(original, bitmap);
                    if (ownsBitmap)
                    {
                        bitmap.Dispose();
                    }
                }
            }
            this.originals = null;
        }
コード例 #20
0
        private void CreateHandle()
        {
            int flags = 1;
            switch (this.colorDepth)
            {
                case System.Windows.Forms.ColorDepth.Depth16Bit:
                    flags |= 0x10;
                    break;

                case System.Windows.Forms.ColorDepth.Depth24Bit:
                    flags |= 0x18;
                    break;

                case System.Windows.Forms.ColorDepth.Depth32Bit:
                    flags |= 0x20;
                    break;

                case System.Windows.Forms.ColorDepth.Depth4Bit:
                    flags |= 4;
                    break;

                case System.Windows.Forms.ColorDepth.Depth8Bit:
                    flags |= 8;
                    break;
            }
            IntPtr userCookie = System.Windows.Forms.UnsafeNativeMethods.ThemingScope.Activate();
            try
            {
                System.Windows.Forms.SafeNativeMethods.InitCommonControls();
                this.nativeImageList = new NativeImageList(System.Windows.Forms.SafeNativeMethods.ImageList_Create(this.imageSize.Width, this.imageSize.Height, flags, 4, 4));
            }
            finally
            {
                System.Windows.Forms.UnsafeNativeMethods.ThemingScope.Deactivate(userCookie);
            }
            if (this.Handle == IntPtr.Zero)
            {
                throw new InvalidOperationException(System.Windows.Forms.SR.GetString("ImageListCreateFailed"));
            }
            System.Windows.Forms.SafeNativeMethods.ImageList_SetBkColor(new HandleRef(this, this.Handle), -1);
            for (int i = 0; i < this.originals.Count; i++)
            {
                Original original = (Original) this.originals[i];
                if (original.image is Icon)
                {
                    this.AddIconToHandle(original, (Icon) original.image);
                }
                else
                {
                    bool ownsBitmap = false;
                    Bitmap bitmap = this.CreateBitmap(original, out ownsBitmap);
                    this.AddToHandle(original, bitmap);
                    if (ownsBitmap)
                    {
                        bitmap.Dispose();
                    }
                }
            }
            this.originals = null;
        }
コード例 #21
0
 public ShellImageList(SHIL iconSize)
 {
     _imageList        = new NativeImageList(Native.SHGetImageList(iconSize));
     _imageSourceCache = new IndexDictionary <ImageSource>();
 }