我定义了托盘的鼠标移动事件,即当用户鼠标一移至托盘就会出现气球提示,但用户鼠标离开了notifyIcon,这个气球提示不会消失,而且在window其它地方点击也不会消失,必须要点击这个气球提示内的任何一个地方才能关闭。因为其它工具的气球提示是离开了就会自动消失,但托盘notifyIcon出现的BalloonTip就不会,有什么办法可以让托盘的气球提示和其它tooltip一样,鼠标离开托盘区域就自动消失了,向大家请教,谢谢大家。。

解决方案 »

  1.   

    notifyIcon  的 text属性是一般的tooltip.鼠标移动过去了就没了.
    this.notifyIcon.ShowBalloonTip(1000); 这句话,貌似不管后面参数写多少毫秒,消失的时间都差不多..要等大概10秒才会自动消失.
      

  2.   

    托盘notifyIcon.ShowBalloonTip 是不是没有方法让他在鼠标离开notifyIcon托盘时自动消失?那能不能用语句控制他几秒后消失或者马上消失???
      

  3.   

    没有办法吗?让鼠标离开notifyIcon托盘时ShowBalloonTip自动消失?还有控制他几秒后消失或马上消失。
      

  4.   

    自己写控件库!
    我给出一个气泡的代码!using System;
    using System.Drawing;
    using System.Windows.Forms;namespace TigerLeqWindowsControlLibrary.Controls
    {
        public class BalloonHelper
        {
            private static ImpsBalloon _balloon;        private BalloonHelper()
            {
            }        public static void RemoveAll()
            {
                if (_balloon != null)
                {
                    _balloon.Close();
                    _balloon.Dispose();
                    _balloon = null;
                }
            }        public static void ShowBallon(Control c, string message)
            {
                ShowBallon(c, message, -1);
            }        public static void ShowBallon(Control c, string message, Point location)
            {
                ShowBallon(c, message, string.Empty, location);
            }        public static void ShowBallon(Control c, string message, int duration)
            {
                ShowBallon(c, message, string.Empty, duration);
            }        public static void ShowBallon(Control c, string message, string title, Point location)
            {
                ShowBallon(c, message, title, ToolTipIcon.Info, location);
            }        public static void ShowBallon(Control c, string message, string title, int duration)
            {
                ShowBallon(c, message, title, ToolTipIcon.Info, duration);
            }        public static void ShowBallon(Control c, string message, string title, ToolTipIcon icon)
            {
                ShowBallon(c, message, title, icon, Point.Empty, -1);
            }        public static void ShowBallon(Control c, string message, string title, ToolTipIcon icon, Point location)
            {
                ShowBallon(c, message, title, icon, location, -1);
            }        public static void ShowBallon(Control c, string message, string title, ToolTipIcon icon, int duration)
            {
                ShowBallon(c, message, title, icon, Point.Empty, duration);
            }        public static void ShowBallon(Control c, string message, string title, ToolTipIcon icon, Point location, int duration)
            {
                RemoveAll();
                _balloon = new ImpsBalloon();
                _balloon.ParentControl = c;
                _balloon.Title = title;
                _balloon.Text = message;
                if (duration > 0)
                {
                    _balloon.Duration = duration;
                }
                switch (icon)
                {
                    case ToolTipIcon.None:
                        _balloon.Icon = BalloonIcon.None;
                        break;                case ToolTipIcon.Info:
                        _balloon.Icon = BalloonIcon.Info;
                        break;                case ToolTipIcon.Warning:
                        _balloon.Icon = BalloonIcon.Warning;
                        break;                case ToolTipIcon.Error:
                        _balloon.Icon = BalloonIcon.Error;
                        break;
                }
                _balloon.Alignment = BalloonAlignment.BottomMiddle;
                _balloon.IsPositionAbsolute = true;
                _balloon.IsStemCentered = true;
                try
                {
                    _balloon.Show();
                }
                catch (Exception)
                {
                }
            }        public static void ShowInputErrorBallon(Control c, string message)
            {
                ShowInputErrorBallon(c, message, string.Empty, 0x7d0);
            }        public static void ShowInputErrorBallon(Control c, string message, string title)
            {
                ShowInputErrorBallon(c, message, title, 0x7d0);
            }        public static void ShowInputErrorBallon(Control c, string message, string title, int duration)
            {
                if (c is TextBoxBase)
                {
                    ((TextBoxBase)c).SelectAll();
                }
                else if (c is ComboBox)
                {
                    ((ComboBox)c).SelectAll();
                }
                c.Focus();
                ShowBallon(c, message, title, ToolTipIcon.Error, duration);
            }
        }
    }using System;
    using System.Collections.Generic;
    using System.Text;namespace TigerLeqWindowsControlLibrary.Controls
    {
        public enum BalloonIcon
        {
            None,
            Info,
            Warning,
            Error
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Text;namespace TigerLeqWindowsControlLibrary.Controls
    {
        public enum BalloonPosition
        {
            Absolute,
            Track
        }
    }using System;
    using System.Runtime.CompilerServices;
    using System.Windows.Forms;namespace TigerLeqWindowsControlLibrary.Controls
    {
        internal class BalloonWindow : NativeWindow
        {
            private const int WM_LBUTTONDOWN = 0x201;        public event BalloonWindowClosedEventHandler WindowClosed;        protected override void WndProc(ref Message m)
            {
                if ((m.Msg == 0x201) && (this.WindowClosed != null))
                {
                    this.WindowClosed();
                }
                base.WndProc(ref m);
            }
        }
    }using System;
    using System.Runtime.CompilerServices;namespace TigerLeqWindowsControlLibrary.Controls
    {
        public delegate void BalloonWindowClosedEventHandler();
    }
    using System;
    using System.Collections.Generic;
    using System.Text;namespace TigerLeqWindowsControlLibrary.Controls
    {
        public enum BalloonAlignment
        {
            TopLeft,
            TopMiddle,
            TopRight,
            LeftMiddle,
            RightMiddle,
            BottomLeft,
            BottomMiddle,
            BottomRight
        }
    }
    适用与任何控件显示气泡列子:
    BalloonHelper.ShowBallon(this.tb_sell_bill_id, "提示内容", "提示标题!", ToolTipIcon.Info, 0x5dc);
      

  5.   

    //补充
    using System;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    using TigerLeqClassLibrary.Utils.Win32;namespace TigerLeqWindowsControlLibrary.Controls
    {
        public class ImpsBalloon : IDisposable
        {
            private BalloonAlignment _alignment = BalloonAlignment.BottomMiddle;
            private BalloonWindow _balloonWindow = new BalloonWindow();
            private bool _disposed;
            private int _duration = 0xbb8;
            private BalloonIcon _icon;
            private bool _isPositionAbsolute;
            private bool _isStemCentered;
            private int _maxWidth = 250;
            private Control _parentControl;
            private BalloonPosition _position;
            private string _text;
            private Timer _timer;
            private const int _timerInterval = 100;
            private string _title;
            private TOOLINFO _toolInfo;        public ImpsBalloon()
            {
                this._balloonWindow.WindowClosed += new BalloonWindowClosedEventHandler(this.Close);
                this._timer = new Timer();
                this._timer.Interval = 100;
                this._timer.Tick += new EventHandler(this._timer_Tick);
            }        private void _timer_Tick(object sender, EventArgs e)
            {
                this.Duration -= 100;
                if (this.Duration <= 0)
                {
                    this.Close();
                }
            }        private void BaseForm_Deactivate(object sender, EventArgs e)
            {
                this.Close();
            }        public void Close()
            {
                IntPtr zero = IntPtr.Zero;
                try
                {
                    this._timer.Stop();
                    if (!this._balloonWindow.Handle.Equals(IntPtr.Zero))
                    {
                        zero = Marshal.AllocHGlobal(Marshal.SizeOf(this._toolInfo));
                        Marshal.StructureToPtr(this._toolInfo, zero, false);
                        ImpsNativeMethods.SendMessage(this._balloonWindow.Handle, 0x411, 0, zero);
                    }
                }
                catch (Exception)
                {
                }
                finally
                {
                    if (zero != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(zero);
                    }
                    this._balloonWindow.DestroyHandle();
                }
            }        private void Control_HandleDestroyed(object sender, EventArgs e)
            {
                this.Close();
            }        public void Dispose()
            {
                this.Dispose(true);
                GC.SuppressFinalize(this);
            }        protected virtual void Dispose(bool disposing)
            {
                if (!this._disposed)
                {
                    this.Close();
                }
                this._disposed = true;
            }      
      

  6.   

    //接上
      private void SetBalloonPosition(RECT rect)
            {
                int loWord = 0;
                int hiWord = 0;
                switch (this._alignment)
                {
                    case BalloonAlignment.TopLeft:
                        loWord = rect.left;
                        hiWord = rect.top;
                        break;                case BalloonAlignment.TopMiddle:
                        loWord = rect.left + (rect.right / 2);
                        hiWord = rect.top;
                        break;                case BalloonAlignment.TopRight:
                        loWord = rect.left + rect.right;
                        hiWord = rect.top;
                        break;                case BalloonAlignment.LeftMiddle:
                        loWord = rect.left;
                        hiWord = rect.top + (rect.bottom / 2);
                        break;                case BalloonAlignment.RightMiddle:
                        loWord = rect.left + rect.right;
                        hiWord = rect.top + (rect.bottom / 2);
                        break;                case BalloonAlignment.BottomLeft:
                        loWord = rect.left;
                        hiWord = rect.top + rect.bottom;
                        break;                case BalloonAlignment.BottomMiddle:
                        loWord = rect.left + (rect.right / 2);
                        hiWord = rect.top + rect.bottom;
                        break;                case BalloonAlignment.BottomRight:
                        loWord = rect.left + rect.right;
                        hiWord = rect.top + rect.bottom;
                        break;
                }
                int num3 = ImpsNativeMethods.MAKELONG(loWord, hiWord);
                IntPtr lParam = new IntPtr(num3);
                ImpsNativeMethods.SendMessage(this._balloonWindow.Handle, 0x412, 0, lParam);
            }        public void Show()
            {
                this.Close();
                if (!this._timer.Enabled)
                {
                    this._timer.Start();
                }
                CreateParams cp = new CreateParams();
                cp.ClassName = "tooltips_class32";
                cp.Style = -2147483453;
                this._balloonWindow.CreateHandle(cp);
                this._toolInfo = new TOOLINFO();
                this._toolInfo.cbSize = Marshal.SizeOf(this._toolInfo);
                this._toolInfo.uFlags = 0x1131;
                if (this.IsPositionAbsolute)
                {
                    this._toolInfo.uFlags |= 0x80;
                }
                if (this._isStemCentered)
                {
                    this._toolInfo.uFlags |= 2;
                }
                this._toolInfo.uId = this._balloonWindow.Handle;
                this._toolInfo.lpszText = this.Text;
                this._toolInfo.hwnd = this._parentControl.Handle;
                ImpsNativeMethods.GetClientRect(this._parentControl.Handle, ref this._toolInfo.rect);
                ImpsNativeMethods.ClientToScreen(this._parentControl.Handle, ref this._toolInfo.rect);
                ImpsNativeMethods.SetWindowPos(this._balloonWindow.Handle, ImpsNativeMethods.HWND_TOPMOST, 0, 0, 0, 0, 0x13);
                IntPtr zero = IntPtr.Zero;
                IntPtr lParam = IntPtr.Zero;
                IntPtr ptr = IntPtr.Zero;
                try
                {
                    zero = Marshal.AllocHGlobal(Marshal.SizeOf(this._toolInfo));
                    Marshal.StructureToPtr(this._toolInfo, zero, false);
                    ImpsNativeMethods.SendMessage(this._balloonWindow.Handle, 0x432, 0, zero);
                    this._toolInfo = (TOOLINFO)Marshal.PtrToStructure(zero, typeof(TOOLINFO));
                    ImpsNativeMethods.SendMessage(this._balloonWindow.Handle, 0x418, 0, new IntPtr(this._maxWidth));
                    lParam = Marshal.StringToHGlobalAuto(this.Title);
                    ImpsNativeMethods.SendMessage(this._balloonWindow.Handle, 0x421, (int)this.Icon, lParam);
                    this.SetBalloonPosition(this._toolInfo.rect);
                    ptr = Marshal.AllocHGlobal(Marshal.SizeOf(this._toolInfo));
                    Marshal.StructureToPtr(this._toolInfo, ptr, false);
                    ImpsNativeMethods.SendMessage(this._balloonWindow.Handle, 0x411, -1, ptr);
                }
                finally
                {
                    if (zero != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(zero);
                    }
                    if (lParam != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(lParam);
                    }
                    if (ptr != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(ptr);
                    }
                }
                GC.KeepAlive(this);
            }        public BalloonAlignment Alignment
            {
                get
                {
                    return this._alignment;
                }
                set
                {
                    this._alignment = value;
                }
            }        public int Duration
            {
                get
                {
                    return this._duration;
                }
                set
                {
                    if (value >= 0)
                    {
                        this._duration = value;
                    }
                }
            }        public BalloonIcon Icon
            {
                get
                {
                    return this._icon;
                }
                set
                {
                    this._icon = value;
                }
            }        public bool IsPositionAbsolute
            {
                get
                {
                    return this._isPositionAbsolute;
                }
                set
                {
                    this._isPositionAbsolute = value;
                }
            }        public bool IsStemCentered
            {
                get
                {
                    return this._isStemCentered;
                }
                set
                {
                    this._isStemCentered = value;
                }
            }        public Control ParentControl
            {
                get
                {
                    return this._parentControl;
                }
                set
                {
                    if ((value != null) && value.IsHandleCreated)
                    {
                        value.HandleDestroyed += new EventHandler(this.Control_HandleDestroyed);
                        if (value.FindForm() != null)
                        {
                            value.FindForm().Deactivate += new EventHandler(this.BaseForm_Deactivate);
                            value.FindForm().Move += new EventHandler(this.BaseForm_Deactivate);
                        }
                    }
                    if (this._parentControl != null)
                    {
                        this._parentControl.HandleDestroyed -= new EventHandler(this.Control_HandleDestroyed);
                        if (value.FindForm() != null)
                        {
                            this._parentControl.FindForm().Deactivate -= new EventHandler(this.BaseForm_Deactivate);
                            this._parentControl.FindForm().Move += new EventHandler(this.BaseForm_Deactivate);
                        }
                    }
                    this._parentControl = value;
                }
            }        public BalloonPosition Position
            {
                get
                {
                    return this._position;
                }
                set
                {
                    this._position = value;
                }
            }        public string Text
            {
                get
                {
                    return this._text;
                }
                set
                {
                    this._text = value;
                }
            }        public string Title
            {
                get
                {
                    return this._title;
                }
                set
                {
                    this._title = value;
                }
            }
        }
    }