C# 判断鼠标是否在控件内
我是想在鼠标移过控件时,用ToolTip显示控件内容。用了MouseMove事件
editor.MouseMove += new MouseEventHandler(showTip);
private void showTip(object sener, MouseEventArgs e)
{
if (e.X > control.Location.X && e.X < control.Location.X + control.Width && e.Y > control.Location.Y && e.Y < control.Location.Y + control.Height)
toolTip1.Show("AAAAAAAAAAAAAAAAAA",control);
}
private void text_ShowTooltips()
{
Panel panel = new Panel()
{
Location = new Point(0, 0),
Size = this.ClientSize
};
this.Controls.Add(panel);
panel.Controls.AddRange(new Control[] {
new Button() { Location=new Point(100,100), Text="Button1", AccessibleDescription="This is Button1" },
new Button() { Location=new Point(100,150), Text="Button2", AccessibleDescription="This is Button2" },
new Button() { Location=new Point(100,200), Text="Button3", AccessibleDescription="This is Button3" },
new Button() { Location=new Point(100,250), Text="Button4", AccessibleDescription="This is Button4" },
new Button() { Location=new Point(100,300), Text="Button5", AccessibleDescription="This is Button5" },
});
ToolTip tips = new ToolTip();
foreach (Control ctrl in panel.Controls)
{
if (ctrl is Button && ctrl.AccessibleDescription != "")
{
tips.SetToolTip(ctrl, ctrl.AccessibleDescription);
}
}
panel.BringToFront();
}