示例#1
0
        public TaskbarItemInfo()
        {
            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                ITaskbarList taskbarList = null;
                try
                {
                    taskbarList = CLSID.CoCreateInstance <ITaskbarList>(CLSID.TaskbarList);
                    taskbarList.HrInit();

                    // This QI will only work on Win7.
                    _taskbarList = taskbarList as ITaskbarList3;

                    taskbarList = null;
                }
                finally
                {
                    Utility.SafeRelease(ref taskbarList);
                }

                _overlaySize = new Size(
                    NativeMethods.GetSystemMetrics(SM.CXSMICON),
                    NativeMethods.GetSystemMetrics(SM.CYSMICON));
            }

            // Set ThumbButtons to an empty list so callers can just use the property.
            ThumbButtonInfos = new ThumbButtonInfoCollection();
        }
示例#2
0
        private HRESULT _UpdateThumbButtons(bool attached)
        {
            var nativeButtons = new THUMBBUTTON[c_MaximumThumbButtons];

            HRESULT hr = _RegisterThumbButtons();

            if (hr.Failed)
            {
                return(hr);
            }

            ThumbButtonInfoCollection thumbButtons = ThumbButtonInfos;

            try
            {
                uint currentButton = 0;
                if (attached && null != thumbButtons)
                {
                    foreach (ThumbButtonInfo wrappedTB in thumbButtons)
                    {
                        var nativeTB = new THUMBBUTTON
                        {
                            iId    = (uint)currentButton,
                            dwMask = THB.FLAGS | THB.TOOLTIP | THB.ICON,
                        };

                        switch (wrappedTB.Visibility)
                        {
                        case Visibility.Collapsed:
                            // HIDDEN removes the button from layout logic.
                            nativeTB.dwFlags = THBF.HIDDEN;
                            break;

                        case Visibility.Hidden:
                            // To match WPF's notion of hidden, we want this not HIDDEN,
                            // but disabled, without background, and without icon.
                            nativeTB.dwFlags = THBF.DISABLED | THBF.NOBACKGROUND;
                            nativeTB.hIcon   = IntPtr.Zero;

                            break;

                        default:
                        case Visibility.Visible:

                            nativeTB.szTip = wrappedTB.Description ?? "";
                            nativeTB.hIcon = _GetHICONFromImageSource(wrappedTB.ImageSource, _overlaySize);

                            if (!wrappedTB.IsBackgroundVisible)
                            {
                                nativeTB.dwFlags |= THBF.NOBACKGROUND;
                            }

                            if (!wrappedTB.IsEnabled)
                            {
                                nativeTB.dwFlags |= THBF.DISABLED;
                            }
                            else
                            {
                                nativeTB.dwFlags |= THBF.ENABLED;
                            }

                            // This is separate from enabled/disabled
                            if (!wrappedTB.IsInteractive)
                            {
                                nativeTB.dwFlags |= THBF.NONINTERACTIVE;
                            }

                            if (wrappedTB.DismissWhenClicked)
                            {
                                nativeTB.dwFlags |= THBF.DISMISSONCLICK;
                            }
                            break;
                        }

                        nativeButtons[currentButton] = nativeTB;

                        ++currentButton;
                        if (currentButton == c_MaximumThumbButtons)
                        {
                            break;
                        }
                    }
                }

                // If we're not attached, or the list is less than the maximum number of buttons
                // then fill in the rest with collapsed, empty buttons.
                for (; currentButton < c_MaximumThumbButtons; ++currentButton)
                {
                    nativeButtons[currentButton] = new THUMBBUTTON
                    {
                        iId     = (uint)currentButton,
                        dwFlags = THBF.NOBACKGROUND | THBF.DISABLED | THBF.HIDDEN,
                        dwMask  = THB.FLAGS | THB.ICON | THB.TOOLTIP
                    };
                }

                // Finally, apply the update.
                return(_taskbarList.ThumbBarUpdateButtons(_hwndSource.Handle, (uint)nativeButtons.Length, nativeButtons));
            }
            finally
            {
                foreach (var nativeButton in nativeButtons)
                {
                    IntPtr hico = nativeButton.hIcon;
                    if (IntPtr.Zero != hico)
                    {
                        Utility.SafeDestroyIcon(ref hico);
                    }
                }
            }
        }