none
关于mouse_enter 和 mouse_leave的问题 RRS feed

  • 问题

  • 我有一个form1,其上有一个btn1,现在我的需求是当鼠标在form1上时,btn1显示,当鼠标移出form1上时,btn1隐藏。 但是出现一个问题,当鼠标移至btn1上时,触发了mouse_leave事件,以至于把btn1隐藏了。 请问该问题如何解决?
    2011年3月15日 7:20

答案

  • 你好!

    我用了一个延时的小技巧解决了你的问题,参考下面的代码:

    using System.ComponentModel;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
      public partial class Form1 : Form
      {
        public Form1()
        {
          InitializeComponent();
          InitializeDemo();
        }
    
        bool HasLeveButton = true;
        void InitializeDemo()
        {
          Button btn = new Button() { Text = "Sample Button", Visible = false, Dock = DockStyle.Fill };
    
          this.Controls.Add(btn);
          this.Padding = new Padding(20);
    
          this.MouseEnter += (o, e) => { btn.Visible = true; };
          this.MouseLeave += (o, e) => { DelayHide(btn); };
    
          btn.MouseEnter += (o, e) => { HasLeveButton = false; };
          btn.MouseLeave += (o, e) => { HasLeveButton = true; };
        }
    
        /// <summary>
        /// 延时隐藏
        /// </summary>
        void DelayHide(Control control)
        {
          using (BackgroundWorker bg = new BackgroundWorker())
          {
            bg.DoWork += (o, e) => { System.Threading.Thread.Sleep(5); };
            bg.RunWorkerCompleted += (o, e) =>
            {
              if (HasLeveButton)
                control.Visible = false;
            };
            bg.RunWorkerAsync();
          }
        }
      }
    }
    
    


    知识改变命运,奋斗成就人生!
    2011年3月16日 14:24
    版主

全部回复

  •  public partial class Form1 : Form
        {
            Point p; int h, w;
            public Form1()
            {
                InitializeComponent();
               
            }
          
                      private void Form1_MouseLeave(object sender, EventArgs e)
            {

                        if (MousePosition.X > PointToScreen(button1.Location).X && MousePosition.X < PointToScreen(button1.Location).X + w && MousePosition.Y > PointToScreen(button1.Location).Y
                    && MousePosition.Y < PointToScreen(button1.Location).Y + h)
                {
                    button1.Visible = true;
                }
                else { button1.Visible = false; }
            }

            private void Form1_MouseEnter(object sender, EventArgs e)
            {
                button1.Visible = true;
            }

               private void Form1_Load(object sender, EventArgs e)
            {
                p = button1.Location; h = button1.Height; w = button1.Width;
               
            }
        }

     

    //我测试过能达到预期效果,或有不足,欢迎指出

    2011年3月15日 14:42
  • 你好!

    我用了一个延时的小技巧解决了你的问题,参考下面的代码:

    using System.ComponentModel;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
      public partial class Form1 : Form
      {
        public Form1()
        {
          InitializeComponent();
          InitializeDemo();
        }
    
        bool HasLeveButton = true;
        void InitializeDemo()
        {
          Button btn = new Button() { Text = "Sample Button", Visible = false, Dock = DockStyle.Fill };
    
          this.Controls.Add(btn);
          this.Padding = new Padding(20);
    
          this.MouseEnter += (o, e) => { btn.Visible = true; };
          this.MouseLeave += (o, e) => { DelayHide(btn); };
    
          btn.MouseEnter += (o, e) => { HasLeveButton = false; };
          btn.MouseLeave += (o, e) => { HasLeveButton = true; };
        }
    
        /// <summary>
        /// 延时隐藏
        /// </summary>
        void DelayHide(Control control)
        {
          using (BackgroundWorker bg = new BackgroundWorker())
          {
            bg.DoWork += (o, e) => { System.Threading.Thread.Sleep(5); };
            bg.RunWorkerCompleted += (o, e) =>
            {
              if (HasLeveButton)
                control.Visible = false;
            };
            bg.RunWorkerAsync();
          }
        }
      }
    }
    
    


    知识改变命运,奋斗成就人生!
    2011年3月16日 14:24
    版主