private void JobMessage_MouseEnter(object sender, EventArgs e)
{
this.Size = new Size(this.Size.Width, this.Size.Height + 20);
} private void JobMessage_MouseLeave(object sender, EventArgs e)
{
if (!this.Bounds.Contains(Cursor.Position))//加了这个也不行
{
this.Size = new Size(this.Size.Width, this.Size.Height - 20);
}
}我想实现鼠标进入用户控件区域内增加其高度
但发现鼠标指针移动到用户控件内的其他控件上方时也触发MousLeave事件 这时就把控件高度又变回去了
怎么办呢大家帮帮忙 我是新手~ 呵呵
{
this.Size = new Size(this.Size.Width, this.Size.Height + 20);
} private void JobMessage_MouseLeave(object sender, EventArgs e)
{
if (!this.Bounds.Contains(Cursor.Position))//加了这个也不行
{
this.Size = new Size(this.Size.Width, this.Size.Height - 20);
}
}我想实现鼠标进入用户控件区域内增加其高度
但发现鼠标指针移动到用户控件内的其他控件上方时也触发MousLeave事件 这时就把控件高度又变回去了
怎么办呢大家帮帮忙 我是新手~ 呵呵
恩!坐标转换以后就对了!
可是又有新的问题了
MouseEnter时没法判断是从用户控件外enter的还是从用户空间内的控件中enter的
就是 从用户控件中的控件移出时也触发MouseEnter事件 也会增加高度 private void JobMessage_MouseEnter(object sender, EventArgs e)
{
if (this.Bounds.Contains(this.PointToClient(Cursor.Position)))//这里的判断没有用。。
{
this.Size = new Size(this.Size.Width, this.Size.Height + 20);
}
} private void JobMessage_MouseLeave(object sender, EventArgs e)
{
// MessageBox.Show(Cursor.Position.ToString() + this.PointToClient(Cursor.Position).ToString());
if (!this.Bounds.Contains(this.PointToClient(Cursor.Position)))
{
this.Size = new Size(this.Size.Width, this.Size.Height - 20);
}
}
建议判断控件的位置和大小,将鼠标位置转换为坐标,然后判断鼠标的坐标是否在控件内,如果是触发事件,如果否触发离开事件。
你这种情况很常见,比如点击按钮显示一个隐藏的Panel,用户想让鼠标离开Panel后触发Panel的MouseLeave事件以隐藏Panel,但用户却发现当鼠标移动到panel上的控件时系统认为鼠标离开了Panel导致触发事件使Panel隐藏。
解决方法就是判断坐标。没别的好办法了
MouseHover 在鼠标指针停放在控件上时发生
MouseLeave 在鼠标指针离开控件时发生。
MouseMove 当用户将鼠标指针移至控件上方时发生。
private void groupbox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
int mouseX = e.X;
int mouseY = e.Y;
//判断位置
}
Dictionary<int,List<GroupBox>>保存
或GetCursorPos