MouseLeave检测不适用于ImageForm



我的表单中有一个较小的图像。当用户悬停在图像上时,它会显示图像的较大视图(它跟随鼠标,但与鼠标保持一定距离)。为了做到这一点,当光标悬停在图像上时,我生成了一个没有FormBorderStyle的Form。

我遇到的问题是,一旦表单激活,第一个表单似乎不再检测到鼠标悬停或离开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");
    }
}

MouseLeave(和其他事件)未被识别,因为弹出窗口的打开,尤其是使其成为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();
}

MouseHover=当鼠标指针停留在控件上时发生。(msdn)

请尝试MouseMove。

相关内容

最新更新