using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using TaskbarLib.Interop;
using System.Collections;
namespace TaskbarLib.DesktopIntegration
{
///
/// Manages a set of taskbar thumbnail buttons in an application.
///
public sealed class ThumbButtonManager
{
///
/// Initializes a new manager with the specified window handle.
///
/// The window handle.
public ThumbButtonManager(IntPtr hwnd)
{
_hwnd = hwnd;
}
///
/// 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(uint id, Icon icon, string tooltip)
{
return new ThumbButton(this, id, icon, tooltip);
}
///
/// Dispatches a Windows message to the thumbnail button event
/// handlers if appropriate.
///
///
/// This method is intended for infrastructure use only.
///
/// The message.
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();
}
}
///
/// 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[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 _thumbButtons = new Dictionary();
private IntPtr _hwnd;
#endregion
}
}