コード例 #1
0
        /// <summary>
        /// Add a new button to the taskbar
        /// </summary>
        /// <param name="process">Window wrapper</param>
        /// <param name="tooltipManager">Reference to the tooltip manager</param>
        /// <param name="processMenu">Button's context menu</param>
        public void AddButton(SecondDisplayProcess process, ToolTipManager tooltipManager, ContextMenuStrip processMenu)
        {
            // multiple windows messages call this method, in the same thread.
            // so we need to ignore same message when repeated
            if (_lastAddButtonHandle == process.Handle)
            {
                return;
            }

            _lastAddButtonHandle = process.Handle;

            TaskbarButton button = _taskbarButtons.Find(tb =>
            {
                var sdp = (tb.Tag as SecondDisplayProcess);
                return(sdp.Handle == process.Handle);
            });

            if (button != null)
            {
                _lastAddButtonHandle = IntPtr.Zero;
                return;
            }

            // can't use locks since the thread is the same
            //if (_syncEvent.WaitOne(500))
            {
                button = _taskbarButtons.Find(tb =>
                {
                    var sdp = (tb.Tag as SecondDisplayProcess);
                    return(sdp.Handle == process.Handle);
                });
                if (button != null)
                {
                    _lastAddButtonHandle = IntPtr.Zero;
                    //_syncEvent.Release();
                    return;
                }

                button     = new TaskbarButton(tooltipManager);
                button.Tag = process;
                _taskbarButtons.Add(button);

                //_syncEvent.Release();
            }

            /*
             * else
             * {
             *  _lastAddButtonHandle = IntPtr.Zero;
             *  return;
             * }
             */
            _lastAddButtonHandle = IntPtr.Zero;

            this.SuspendLayout();

            try
            {
                this.Controls.Add(button);
            }
            catch (Win32Exception e)
            {
                var p = System.Diagnostics.Process.GetCurrentProcess();

                if (MessageBox.Show(string.Format("Err: {1}{0}Thread Count: {2}{0}Handle Count: {3}", Environment.NewLine, e.Message, p.Threads.Count, p.HandleCount),
                                    "Error", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    Debugger.Break();
                }

                _taskbarButtons.Remove(button);
                this.ResumeLayout();
                return;
            }

            button.AutoSize = false;
            button.Text     = process.Title;
            if (process.Icon != null)
            {
                button.Image = (IsBig ? process.Icon : process.SmallIcon).ConvertToBitmap(IsBig);
            }
            button.Padding   = new System.Windows.Forms.Padding(0);
            button.Margin    = new System.Windows.Forms.Padding(0);
            button.ShowLabel = TaskbarPropertiesManager.Instance.Properties.ShowLabels;
            button.SetDefaultHeight();

            button.ContextMenuStrip = processMenu;

            _groups.AddToGroup(process.Path, button);

            ArrangeButtons();
            this.ResumeLayout();

            button.Refresh();
        }