using System; using System.Collections.Generic; using System.Diagnostics; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using Hardcodet.Wpf.TaskbarNotification.Interop; namespace Hardcodet.Wpf.TaskbarNotification { partial class TaskbarIcon { /// /// An action that is being invoked if the /// fires. /// private Action delayedTimerAction; /// /// A timer that is used to differentiate between single /// and double clicks. /// private readonly Timer singleClickTimer; #region SetVersion /// /// Sets the version flag for the . /// private void SetVersion() { iconData.VersionOrTimeout = (uint) NotifyIconVersion.Vista; bool status = WinApi.Shell_NotifyIcon(NotifyCommand.SetVersion, ref iconData); if (!status) { iconData.VersionOrTimeout = (uint) NotifyIconVersion.Win2000; status = Util.WriteIconData(ref iconData, NotifyCommand.SetVersion); } if (!status) { iconData.VersionOrTimeout = (uint)NotifyIconVersion.Win95; status = Util.WriteIconData(ref iconData, NotifyCommand.SetVersion); } if (!status) { Debug.Fail("Could not set version"); } } #endregion /// /// Sets tooltip settings for the class. /// private void WriteToolTipSettings() { IconDataMembers flags = IconDataMembers.Tip; iconData.ToolTipText = ToolTipText; if (messageSink.Version == NotifyIconVersion.Vista) { if (String.IsNullOrEmpty(ToolTipText) && ToolTip != null) { //if we have not tooltip text but a custom tooltip, we //need to set a dummy value iconData.ToolTipText = "ToolTip"; } else if (!String.IsNullOrEmpty(ToolTipText) && ToolTip == null) { //if a tooltip text was set but there is no custom tooltip, //we need to fall back to legacy operations flags |= IconDataMembers.UseLegacyToolTips; } } //just write the the tooltip Util.WriteIconData(ref iconData, NotifyCommand.Modify, flags); } #region Show / Hide Balloon ToolTip /// /// Displays a balloon tip with the specified title, /// text, and icon in the taskbar for the specified time period. /// /// The title to display on the balloon tip. /// The text to display on the balloon tip. /// Indicates the severity. public void ShowBalloonTip(string title, string message, BalloonIcon icon) { lock(this) { ShowBalloonTip(title, message, icon.GetBalloonFlag(), IntPtr.Zero); } } /// /// Displays a balloon tip with the specified title, /// text, and a custom icon in the taskbar for the specified time period. /// /// The title to display on the balloon tip. /// The text to display on the balloon tip. /// A custom icon. /// If /// is a null reference. public void ShowBalloonTip(string title, string message, Icon customIcon) { if (customIcon == null) throw new ArgumentNullException("customIcon"); lock(this) { ShowBalloonTip(title, message, BalloonFlags.User, customIcon.Handle); } } /// /// Invokes in order to display /// a given balloon ToolTip. /// /// The title to display on the balloon tip. /// The text to display on the balloon tip. /// Indicates what icon to use. /// A handle to a custom icon, if any, or /// . private void ShowBalloonTip(string title, string message, BalloonFlags flags, IntPtr balloonIconHandle) { EnsureNotDisposed(); iconData.BalloonText = message; iconData.BalloonTitle = title; iconData.BalloonFlags = flags; iconData.CustomBalloonIconHandle = balloonIconHandle; Util.WriteIconData(ref iconData, NotifyCommand.Modify, IconDataMembers.Info); } /// /// Hides a balloon ToolTip, if any is displayed. /// public void HideBalloonTip() { EnsureNotDisposed(); //reset balloon by just setting the info to an empty string iconData.BalloonText = iconData.BalloonTitle = String.Empty; Util.WriteIconData(ref iconData, NotifyCommand.Modify, IconDataMembers.Info); } #endregion #region Single Click Timer event /// /// Performs a delayed action if the user requested an action /// based on a single click of the left mouse.
/// This method is invoked by the . ///
private void DoSingleClickAction(object state) { if (IsDisposed) return; //run action Action action = delayedTimerAction; if (action != null) { //cleanup action delayedTimerAction = null; //switch to UI thread Application.Current.Dispatcher.Invoke(action); } } #endregion #region Show Tray Popup / Context Menu /// /// Displays the control if /// it was set. /// private void ShowTrayPopup() { if (IsDisposed) return; if (TaskbarIconPopup != null) { //raise preview event var args = RaisePreviewTaskbarIconPopupOpenEvent(); if (args.Handled) return; //open popup TaskbarIconPopup.IsOpen = true; //activate the message window to track deactivation - otherwise, the context menu //does not close if the user clicks somewhere else WinApi.SetForegroundWindow(messageSink.MessageWindowHandle); //bubble event RaiseTaskbarIconPopupOpenEvent(); } } /// /// Displays the if /// it was set. /// private void ShowContextMenu() { if (IsDisposed) return; if (ContextMenu != null) { //raise preview event var args = RaisePreviewTaskbarIconContextMenuOpenEvent(); if (args.Handled) return; //CreateActivationWindow(); ContextMenu.IsOpen = true; //activate the message window to track deactivation - otherwise, the context menu //does not close if the user clicks somewhere else WinApi.SetForegroundWindow(messageSink.MessageWindowHandle); //bubble event RaiseTaskbarIconContextMenuOpenEvent(); } } #endregion } }