MouseLeave 检测不适用于 ImageForm
MouseLeave detection not working with ImageForm
我的表单中有一张较小的图片。当用户将鼠标悬停在图像上时,它会显示更大的图像视图(跟随鼠标,但与鼠标保持一定距离)。为了做到这一点,当光标悬停在图像上时,我生成了一个没有 FormBorderStyles 的表单。
我遇到的问题 运行 是第一个表单似乎不再检测到鼠标在表单激活后悬停或离开 PictureBox。表单也不跟随光标。
这是我得到的精简版:
C#
bool formOpen = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.MouseHover += pictureBox1_MouseHover;
pictureBox1.MouseLeave +=pictureBox1_MouseLeave;
}
void pictureBox1_MouseHover(object sender, EventArgs e)
{
if (formOpen == false)
{
Form form = new Form();
form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
form.BackColor = Color.Orchid;
//Show the form
form.Show();
form.Name = "imageForm";
this.AddOwnedForm(form);
//Set event handler for when the mouse leaves the image area.
//form.MouseLeave += form_MouseLeave;
//Set the location of the form and size
form.BackColor = Color.Black;
form.Dock = DockStyle.Fill;
form.Size = new Size(30, 30);
form.BackgroundImageLayout = ImageLayout.Center;
this.TopMost = true;
formOpen = true;
form.Location = new Point(Cursor.Position.X, Cursor.Position.Y);
}
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
//MessageBox.Show("Worked");
}
}
MouseHover = 当鼠标指针停留在控件上时发生。 (msdn)
改用 MouseMove。
MouseLeave
(和其他事件)未被识别,因为弹出窗口 window 的打开,特别是 topmost=true
将焦点从原始表单及其 PictureBox
.
它也没有移动,因为没有提供移动代码..
以下是一些可以使表单移动的更改:
- 您需要在
form1
级别 引用它
- 您需要在
MouseMove
活动中移动它
请注意,Hover
是一种仅限一次的事件类型。它只触发 一次 直到你离开控制..(注意:Setsu 已经从 Hover
切换到 Enter
。这工作正常,但缺少之前的短暂延迟显示第二个表格。如果你想要那个,你可以切换回 Hover
或者你可以通过 Timer
来伪造悬停延迟,这是我经常做的。)
// class level variable
Form form;
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.MouseEnter += pictureBox1_MouseEnter;
pictureBox1.MouseLeave +=pictureBox1_MouseLeave;
pictureBox1.MouseMove += pictureBox1_MouseMove; // here we move the form..
}
// .. with a little offset. The exact numbers depend on the cursor shape
void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if ((form != null) && form.Visible)
{
form.Location = new Point(Cursor.Position.X + 5, Cursor.Position.Y + 5);
}
}
void pictureBox1_MouseEnter(object sender, EventArgs e)
{
// we create it only once. Could also be done at startup!
if (form == null)
{
form = new Form();
form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
//form.BackColor = Color.Orchid;
form.Name = "imageForm";
this.AddOwnedForm(form);
form.BackColor = Color.Black;
form.Dock = DockStyle.Fill;
form.Size = new Size(30, 30);
form.BackgroundImageLayout = ImageLayout.Center;
//this.TopMost = true; // wrong! this will steal the focus!!
form.ShowInTaskbar = false;
}
// later we only show and update it..
form.Show();
form.Location = new Point(Cursor.Position.X + 5, Cursor.Position.Y + 5);
// we want the Focus to be on the main form!
Focus();
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
if (form!= null) form.Hide();
}
我的表单中有一张较小的图片。当用户将鼠标悬停在图像上时,它会显示更大的图像视图(跟随鼠标,但与鼠标保持一定距离)。为了做到这一点,当光标悬停在图像上时,我生成了一个没有 FormBorderStyles 的表单。
我遇到的问题 运行 是第一个表单似乎不再检测到鼠标在表单激活后悬停或离开 PictureBox。表单也不跟随光标。
这是我得到的精简版:
C#
bool formOpen = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.MouseHover += pictureBox1_MouseHover;
pictureBox1.MouseLeave +=pictureBox1_MouseLeave;
}
void pictureBox1_MouseHover(object sender, EventArgs e)
{
if (formOpen == false)
{
Form form = new Form();
form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
form.BackColor = Color.Orchid;
//Show the form
form.Show();
form.Name = "imageForm";
this.AddOwnedForm(form);
//Set event handler for when the mouse leaves the image area.
//form.MouseLeave += form_MouseLeave;
//Set the location of the form and size
form.BackColor = Color.Black;
form.Dock = DockStyle.Fill;
form.Size = new Size(30, 30);
form.BackgroundImageLayout = ImageLayout.Center;
this.TopMost = true;
formOpen = true;
form.Location = new Point(Cursor.Position.X, Cursor.Position.Y);
}
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
//MessageBox.Show("Worked");
}
}
MouseHover = 当鼠标指针停留在控件上时发生。 (msdn)
改用 MouseMove。
MouseLeave
(和其他事件)未被识别,因为弹出窗口 window 的打开,特别是 topmost=true
将焦点从原始表单及其 PictureBox
.
它也没有移动,因为没有提供移动代码..
以下是一些可以使表单移动的更改:
- 您需要在
form1
级别 引用它
- 您需要在
MouseMove
活动中移动它
请注意,Hover
是一种仅限一次的事件类型。它只触发 一次 直到你离开控制..(注意:Setsu 已经从 Hover
切换到 Enter
。这工作正常,但缺少之前的短暂延迟显示第二个表格。如果你想要那个,你可以切换回 Hover
或者你可以通过 Timer
来伪造悬停延迟,这是我经常做的。)
// class level variable
Form form;
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.MouseEnter += pictureBox1_MouseEnter;
pictureBox1.MouseLeave +=pictureBox1_MouseLeave;
pictureBox1.MouseMove += pictureBox1_MouseMove; // here we move the form..
}
// .. with a little offset. The exact numbers depend on the cursor shape
void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if ((form != null) && form.Visible)
{
form.Location = new Point(Cursor.Position.X + 5, Cursor.Position.Y + 5);
}
}
void pictureBox1_MouseEnter(object sender, EventArgs e)
{
// we create it only once. Could also be done at startup!
if (form == null)
{
form = new Form();
form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
//form.BackColor = Color.Orchid;
form.Name = "imageForm";
this.AddOwnedForm(form);
form.BackColor = Color.Black;
form.Dock = DockStyle.Fill;
form.Size = new Size(30, 30);
form.BackgroundImageLayout = ImageLayout.Center;
//this.TopMost = true; // wrong! this will steal the focus!!
form.ShowInTaskbar = false;
}
// later we only show and update it..
form.Show();
form.Location = new Point(Cursor.Position.X + 5, Cursor.Position.Y + 5);
// we want the Focus to be on the main form!
Focus();
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
if (form!= null) form.Hide();
}