积极答复者
关于mouse_enter 和 mouse_leave的问题

问题
答案
-
你好!
我用了一个延时的小技巧解决了你的问题,参考下面的代码:
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(); } } } }
知识改变命运,奋斗成就人生!- 已标记为答案 feiyun0112Moderator 2011年3月18日 3:03
全部回复
-
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;
}
}//我测试过能达到预期效果,或有不足,欢迎指出
-
你好!
我用了一个延时的小技巧解决了你的问题,参考下面的代码:
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(); } } } }
知识改变命运,奋斗成就人生!- 已标记为答案 feiyun0112Moderator 2011年3月18日 3:03