/* * Process Hacker - * ProcessHacker Taskbar Extensions * * Copyright (C) 2009 dmex * * This file is part of Process Hacker. * * Process Hacker is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Process Hacker is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Process Hacker. If not, see . * */ using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Windows.Forms; using TaskbarLib.Interop; namespace TaskbarLib { /// /// Manages a set of taskbar thumbnail buttons in an application. /// public sealed class ThumbButtonManager : IDisposable { private sealed class MessageFilter : IMessageFilter { private ThumbButtonManager _manager; public MessageFilter(ThumbButtonManager manager) { _manager = manager; } public bool PreFilterMessage(ref Message m) { if (m.Msg == (int)Windows7Taskbar.TaskbarButtonCreatedMessage) { _manager.OnTaskbarButtonCreated(); return true; } else if (m.Msg == (int)ProcessHacker.Native.Api.WindowMessage.Command) { _manager.OnCommand(m.WParam); } return false; } } public event EventHandler TaskbarButtonCreated; private Form _form; private MessageFilter _filter; private bool _disposed; /// /// Initializes a new manager on the specified form. /// /// The form. public ThumbButtonManager(Form form) { _form = form; Application.AddMessageFilter(_filter = new MessageFilter(this)); } public void Dispose() { if (!_disposed) { Application.RemoveMessageFilter(_filter); _disposed = true; } } /// /// Creates a new taskbar thumbnail button. /// /// The button's id. /// The button's icon. /// The button's tooltip. /// An object of type /// representing the newly created thumbnail button. public ThumbButton CreateThumbButton(int id, Icon icon, string tooltip) { return new ThumbButton(this, id, icon, tooltip); } /// /// Adds the specified taskbar thumbnail buttons to the application's /// thumbnail toolbar. /// /// /// Thumbnail buttons can only be added once - after being added, /// they cannot be removed or deleted. However, they can be shown, /// hidden, enabled and disabled. /// /// The buttons to add. public void AddThumbButtons(params ThumbButton[] buttons) { Array.ForEach(buttons, b => _thumbButtons.Add(b.Id, b)); RefreshThumbButtons(); } /// /// Gets a specific thumbnail button by its id. /// /// The thumbnail button's id. /// An object of type /// with the specified id. public ThumbButton this[int id] { get { return _thumbButtons[id]; } } internal void OnCommand(IntPtr wParam) { if (((wParam.ToInt32() >> 16) & 0xffff) == SafeNativeMethods.THBN_CLICKED) { _thumbButtons[wParam.ToInt32() & 0xffff].OnClick(); } } internal void OnTaskbarButtonCreated() { if (this.TaskbarButtonCreated != null) this.TaskbarButtonCreated(this, new EventArgs()); _buttonsLoaded = false; this.RefreshThumbButtons(); } #region Implementation private bool _buttonsLoaded; internal void RefreshThumbButtons() { THUMBBUTTON[] win32Buttons = (from thumbButton in _thumbButtons.Values select thumbButton.Win32ThumbButton).ToArray(); if (_buttonsLoaded) { Windows7Taskbar.TaskbarList.ThumbBarUpdateButtons(_form.Handle, win32Buttons.Length, win32Buttons); } else //First time { Windows7Taskbar.TaskbarList.ThumbBarAddButtons(_form.Handle, win32Buttons.Length, win32Buttons); _buttonsLoaded = true; } } private Dictionary _thumbButtons = new Dictionary(); #endregion } }