C# 鼠标移动检测不适用于ImageForm

C# 鼠标移动检测不适用于ImageForm,c#,forms,cursor,mouseleave,mousehover,C#,Forms,Cursor,Mouseleave,Mousehover,在我的表格中有一个较小的图像。当用户将鼠标悬停在图像上时,会显示图像的较大视图(该视图跟随鼠标,但与鼠标保持一定距离)。为此,当光标悬停在图像上时,我生成了一个没有FormBorderStyles的表单 我遇到的问题是,第一个窗体在激活后似乎不再检测到鼠标悬停或离开PictureBox。表单也不跟随光标 以下是我得到的精简版本: C# MouseHover=鼠标指针停留在控件上时发生 请尝试MouseMove。无法识别MouseLeave(和其他事件),因为弹出窗口的打开,尤其是使其最顶端=tr

在我的表格中有一个较小的图像。当用户将鼠标悬停在图像上时,会显示图像的较大视图(该视图跟随鼠标,但与鼠标保持一定距离)。为此,当光标悬停在图像上时,我生成了一个没有FormBorderStyles的表单

我遇到的问题是,第一个窗体在激活后似乎不再检测到鼠标悬停或离开PictureBox。表单也不跟随光标

以下是我得到的精简版本:

C#


MouseHover=鼠标指针停留在控件上时发生


请尝试MouseMove。

无法识别
MouseLeave
(和其他事件),因为弹出窗口的打开,尤其是使其
最顶端=true
会将焦点从原始表单及其
图片框中移除

它也没有移动,因为没有提供移动代码

以下是一些将使表单移动的更改:

  • 您需要在
    form1
    级别对其进行引用
  • 您需要在
    MouseMove
    事件中移动它
请注意,
悬停
是一种仅限一次的事件类型。在您离开控件之前,它只触发一次。。(注意:Setsu已从
悬停
切换到
输入
。这可以正常工作,但在显示第二个表单之前没有短暂的延迟。如果您想要返回,您可以切换回
悬停
,或者您可以通过
定时器
模拟悬停延迟,这是我经常做的。)


当鼠标离开picturebox时,是否将
formOpen
重置为
false
?是的,这样,当您悬停图像时,它不会持续打开表单。不确定您为什么首先使用第二个表单。但问题似乎来自最高层,默认情况下,他们窃取了焦点。看,我真的不知道我在做什么,我只是开始编写应用程序。您是否建议另一种方式来显示窗体外部或悬停在窗体上的图像的大视图?@TaW需要另一个窗体,因为这样可以使放大的视图超出父窗体。我也尝试了MouseMove,同样的问题似乎也出现了。一旦另一个窗体打开,应用程序将无法检测鼠标的移动或边界的位置。只有在创建第二个窗体时,才会设置该窗体的位置,请在if之后写入此行,因此,每次鼠标移动时都会执行此操作。顺便说一句,新窗体应放置在偏移位置,而不是直接放置在鼠标所在的位置。我以前遇到过这个问题,1)鼠标向右移动2)鼠标在重新定位之前进入第二个形状3)形状不再移动,因为鼠标现在处于子形状此解决方案实际上运行得很好,但可能会撕裂,具体取决于OP想要显示的内容。将表单设置为双缓冲将有助于更好的用户体验,但需要实现单独的表单类。将需要实现单独的表单类。这不会是一个问题,imo。一种更干净的编码方式,但这取决于OP做出的更改。@Taw您能为我设置实现双缓冲的正确方向吗?(有用的代码或链接)其实非常简单:不在上面的代码中创建表单,而是在设计器中创建表单。称它为Form2(或者更有用的东西!!),这样它就自动成为了自己的类。在属性编辑器中设置您在上述代码中设置的所有属性,此外还将其
双缓冲
属性设置为true。-因此,上面的创建代码简化为
if(form==null){form=newform2();this.AddOwnedForm(form);}
-Done。
    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");
    }
}
// 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();
}