Files
mirror-processhacker/trunk/ProcessHacker/Components/TaskbarLib/ThumbnailButtons/ThumbButtonManager.cs
T
dmex04 ccb1f7b402 added licence to TaskbarLib files, re-added HResult for COM calls and added initial HResult checking
git-svn-id: svn://svn.code.sf.net/p/processhacker/code@1986 21ef857c-d57f-4fe0-8362-d861dc6d29cd
2009-09-28 16:34:01 +00:00

136 lines
4.7 KiB
C#

/*
* 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 <http://www.gnu.org/licenses/>.
*
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using TaskbarLib.Interop;
using System.Collections;
namespace TaskbarLib
{
/// <summary>
/// Manages a set of taskbar thumbnail buttons in an application.
/// </summary>
public sealed class ThumbButtonManager
{
/// <summary>
/// Initializes a new manager with the specified window handle.
/// </summary>
/// <param name="hwnd">The window handle.</param>
public ThumbButtonManager(IntPtr hwnd)
{
_hwnd = hwnd;
}
/// <summary>
/// Creates a new taskbar thumbnail button.
/// </summary>
/// <param name="id">The button's id.</param>
/// <param name="icon">The button's icon.</param>
/// <param name="tooltip">The button's tooltip.</param>
/// <returns>An object of type <see cref="ThumbButton"/>
/// representing the newly created thumbnail button.</returns>
public ThumbButton CreateThumbButton(uint id, Icon icon, string tooltip)
{
return new ThumbButton(this, id, icon, tooltip);
}
/// <summary>
/// Dispatches a Windows message to the thumbnail button event
/// handlers if appropriate.
/// </summary>
/// <remarks>
/// This method is intended for infrastructure use only.
/// </remarks>
/// <param name="message">The message.</param>
public void DispatchMessage(ref Message message)
{
UInt64 wparam = (UInt64)message.WParam.ToInt64();
UInt32 wparam32 = (UInt32) (wparam & 0xffffffff); //Clear top 32 bits
if (message.Msg == (int)ProcessHacker.Native.Api.WindowMessage.Command && (wparam32 >> 16 == SafeNativeMethods.THBN_CLICKED))
{
uint id = wparam32 & 0xffff; //Bottom 16 bits
_thumbButtons[id].FireClick();
}
}
/// <summary>
/// Adds the specified taskbar thumbnail buttons to the application's
/// thumbnail toolbar.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
/// <param name="buttons">The buttons to add.</param>
public void AddThumbButtons(params ThumbButton[] buttons)
{
Array.ForEach(buttons, b => _thumbButtons.Add(b.Id, b));
RefreshThumbButtons();
}
/// <summary>
/// Gets a specific thumbnail button by its id.
/// </summary>
/// <param name="id">The thumbnail button's id.</param>
/// <returns>An object of type <see cref="ThumbButton"/>
/// with the specified id.</returns>
public ThumbButton this[uint id]
{
get
{
return _thumbButtons[id];
}
}
#region Implementation
private bool _buttonsLoaded;
internal void RefreshThumbButtons()
{
THUMBBUTTON[] win32Buttons = (from thumbButton in _thumbButtons.Values select thumbButton.Win32ThumbButton).ToArray();
if (_buttonsLoaded)
{
Windows7Taskbar.TaskbarList.ThumbBarUpdateButtons(_hwnd, (uint)win32Buttons.Length, win32Buttons);
}
else //First time
{
Windows7Taskbar.TaskbarList.ThumbBarAddButtons(_hwnd, (uint)win32Buttons.Length, win32Buttons);
_buttonsLoaded = true;
}
}
private Dictionary<uint, ThumbButton> _thumbButtons = new Dictionary<uint, ThumbButton>();
private IntPtr _hwnd;
#endregion
}
}