Example #1
0
 public static extern short Shell_NotifyIcon(
     int dwMessage, ref NOTIFYICONDATA lpData);
 public static extern short Shell_NotifyIcon(
     int dwMessage, ref NOTIFYICONDATA lpData);
Example #3
0
        /// <summary>
        /// Displays a Balloon ToolTip for the current NotifyIcon.
        /// </summary>
        /// <param name="iconStyle">The balloon ToolTip's icon to use. This icon is placed to the 
        /// left of the balloon ToolTip's title.</param>
        /// <param name="text">The text to display for the balloon ToolTip. The length of the text can not
        /// exceed 254 characters.</param>
        /// <param name="title">The text to display as a title for the balloon ToolTip. 
        /// This title appears in boldface above the text. It can have a maximum of 63 characters.
        /// Pass null or String.Empty to prevent the balloon from displaying a title.</param>
        /// <param name="timeout">The number of milliseconds the balloon ToolTip should remain visible
        /// before it is hidden. The system enforces minimum and maximum timeout values. Timeout values that 
        /// are too large are set to the maximum value and values that are too small default to the 
        /// minimum value. The system minimum and maximum timeout values are currently set at 10 seconds 
        /// and 30 seconds, respectively.</param>
        /// <exception cref="ArgumentNullException">Text is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException">Text is longer than 254 characters.</exception>
        /// <exception cref="ArgumentOutOfRangeException">Title is longer than 254 characters.</exception>
        public void ShowBalloon(BalloonIconStyle iconStyle, string text, 
            string title, int timeout)
        {
            if(null == text)
                throw new ArgumentNullException("Text is not optional.", "text");
            if(null != text && text.Length > MaxBaloonTextLength)
                throw new ArgumentOutOfRangeException("The text is too long. Please provide a string with " +
                    "fewer than 255 characters.", "text");
            if(null == title)
                title = string.Empty;
            if(null != title && title.Length > MaxBaloonTitleLength)
                throw new ArgumentOutOfRangeException("The title is too long. Please provide a string with " +
                    "fewer than 64 characters.", "title");

            _VisibleBeforeBalloon = Visible;

            NOTIFYICONDATA nid;

            nid = new NOTIFYICONDATA();
            nid.uCallbackMessage = Win32.WM_USER_TRAY;
            nid.cbSize = Marshal.SizeOf(typeof(NOTIFYICONDATA));
            nid.uFlags = Win32.NIF_MESSAGE;
            nid.hwnd = this._Messages.Handle;
            nid.uID = 0;

            nid.hIcon = IntPtr.Zero;

            nid.uFlags = nid.uFlags | Win32.NIF_ICON;
            nid.hIcon = this.Icon.Handle;

            nid.uFlags = nid.uFlags | Win32.NIF_INFO;

            // Timeout value is sent in version
            nid.uVersion = timeout;

            nid.szInfo = text;
            nid.szInfoTitle = title;
            nid.dwInfoFlags = Convert.ToInt32(iconStyle);

            // Make sure our Visible property indicates true.
            if(!this.Visible)
                this._Visible = true;

            if (!this._Added)
            {
                Win32.Shell_NotifyIcon(Win32.NIM_ADD, ref nid);
                this._Added = true;
            }
            else if(this._Added)
                Win32.Shell_NotifyIcon(Win32.NIM_MODIFY, ref nid);
        }
Example #4
0
        /// <summary>
        /// Private helper function that updates the System Tray Icon. 
        /// </summary>
        /// <param name="showIcon">If true the icon is shown in the System Tray based on the 
        /// current property values. If false the icon is removed from the System Tray.</param>
        private void UpdateIcon(bool showIcon)
        {
            if (this.DesignMode)
                return;

            //IntSecurity.UnrestrictedWindows.Demand();

            // Initialize the structure
            NOTIFYICONDATA nid = new NOTIFYICONDATA();
            nid.uCallbackMessage = Win32.WM_USER_TRAY;
            nid.uVersion = Win32.NOTIFYICON_VERSION;
            nid.cbSize = Marshal.SizeOf(typeof(NOTIFYICONDATA));
            nid.uFlags = Win32.NIF_MESSAGE;
            nid.hwnd = this._Messages.Handle;
            nid.uID = 0;
            nid.hIcon = IntPtr.Zero;
            nid.szTip = null;

            // If we have a valid icon, make sure we send it.
            if (this.Icon != null)
            {
                nid.uFlags = nid.uFlags | Win32.NIF_ICON;
                nid.hIcon = this.Icon.Handle;
            }

            // Pass our Text property as the tooltip
            nid.uFlags = nid.uFlags | Win32.NIF_TIP;
            nid.szTip = this.Text;

            if (showIcon && this.Icon != null)
            {	// Show the icon
                if (!this._Added)
                { // We don't already have an Icon so we need to creat one.
                    Win32.Shell_NotifyIcon(Win32.NIM_ADD, ref nid);
                    this._Added = true;
                    //return;
                }
                else	// We already have an Icon so we'll just modify it.
                    Win32.Shell_NotifyIcon(Win32.NIM_MODIFY, ref nid);
            }
            else if(this._Added)
            {	// The caller wants to remove the icon and we have an existing icon to remove
                Win32.Shell_NotifyIcon(Win32.NIM_DELETE, ref nid);
                this._Added = false;
            }
        }
        public void HideBalloon()
        {
            NOTIFYICONDATA nid;

            nid = new NOTIFYICONDATA();
            nid.uCallbackMessage = Win32.WM_USER_TRAY;
            nid.cbSize = Marshal.SizeOf(typeof(NOTIFYICONDATA));
            nid.uFlags = Win32.NIF_MESSAGE;
            nid.hwnd = this._Messages.Handle;
            nid.uID = 0;

            nid.hIcon = IntPtr.Zero;
            nid.hIcon = this.Icon.Handle;

            nid.uFlags = Win32.NIF_INFO;

            nid.szInfo = "\0";
            nid.szInfoTitle = "\0";
            Win32.Shell_NotifyIcon(Win32.NIM_MODIFY, ref nid);
        }