C# 我有一个带背景图像的按钮。如何在特定条件下触发鼠标删除事件?

C# 我有一个带背景图像的按钮。如何在特定条件下触发鼠标删除事件?,c#,winforms,C#,Winforms,我有一个有背景的按钮。我创建了mouseenter和mouseleave事件。在mouseleave事件中,如果鼠标光标位于2个坐标之外,它将触发mouseleave事件 private void Button_SpanMouseEnter(object sender, EventArgs e) { Button x = sender as Button; x.Size = new Size(500, 250); x.Location =

我有一个有背景的按钮。我创建了mouseenter和mouseleave事件。在mouseleave事件中,如果鼠标光标位于2个坐标之外,它将触发mouseleave事件

private void Button_SpanMouseEnter(object sender, EventArgs e)
    {
        Button x = sender as Button;
        x.Size = new Size(500, 250);
        x.Location = new Point(0, 0);
    }

    private void Button_SpanMouseLeave(object sender, EventArgs e)
    {
        Button x = sender as Button;

        //If cursor is outside of this coordinates(0,0) & (250,125)
        //it will trigger this size
        x.Size = new Size(250,125);
        x.Location = new Point(0, 0);
    }

我的问题是,当我离开500X250矩形时,它会触发鼠标移动。我希望它在250X125矩形中触发。

要归档您正在尝试的效果,您需要使用
MouseMove
事件,而不是
按钮\u MouseLeave
事件。因此,请删除您的
按钮\u SpanMouseLeave
事件并添加此事件:

private void button1_MouseMove(object sender, MouseEventArgs e)
{
    Button x = sender as Button;
    Point p = PointToClient(System.Windows.Forms.Control.MousePosition);
    if (p.X > 250|| p.Y >125)
    {
        button1.Size = new Size(250, 125);
        button1.Location = new Point(0, 0);
    }
}
编辑

事实上,您根本不需要PointToClient方法。所以代码是这样的:

private void button1_MouseMove(object sender, MouseEventArgs e)
{
    Button x = sender as Button;
    if (e.X > 250|| e.Y >125)
    {
        x.Size = new Size(250, 125);
        x.Location = new Point(0, 0);
    }
}
private void button1_MouseMove(object sender, MouseEventArgs e)
{
    Button x = sender as Button;
    Point p = PointToClient(System.Windows.Forms.Control.MousePosition);
    this.label1.Text = p.X.ToString() + " " + p.Y.ToString();
    if (p.X > x.Location.X + 250 || p.Y > x.Location.Y+125)
    {
        button1.Size = new Size(250, 125);
    }
}
编辑2

好的,如果按钮不在
0,0
中,那么最好像这样使用
PointToClient

private void button1_MouseMove(object sender, MouseEventArgs e)
{
    Button x = sender as Button;
    if (e.X > 250|| e.Y >125)
    {
        x.Size = new Size(250, 125);
        x.Location = new Point(0, 0);
    }
}
private void button1_MouseMove(object sender, MouseEventArgs e)
{
    Button x = sender as Button;
    Point p = PointToClient(System.Windows.Forms.Control.MousePosition);
    this.label1.Text = p.X.ToString() + " " + p.Y.ToString();
    if (p.X > x.Location.X + 250 || p.Y > x.Location.Y+125)
    {
        button1.Size = new Size(250, 125);
    }
}

要归档您正在尝试的效果,您需要使用
MouseMove
事件,而不是
按钮\u MouseLeave
事件。因此,请删除您的
按钮\u SpanMouseLeave
事件并添加此事件:

private void button1_MouseMove(object sender, MouseEventArgs e)
{
    Button x = sender as Button;
    Point p = PointToClient(System.Windows.Forms.Control.MousePosition);
    if (p.X > 250|| p.Y >125)
    {
        button1.Size = new Size(250, 125);
        button1.Location = new Point(0, 0);
    }
}
编辑

事实上,您根本不需要PointToClient方法。所以代码是这样的:

private void button1_MouseMove(object sender, MouseEventArgs e)
{
    Button x = sender as Button;
    if (e.X > 250|| e.Y >125)
    {
        x.Size = new Size(250, 125);
        x.Location = new Point(0, 0);
    }
}
private void button1_MouseMove(object sender, MouseEventArgs e)
{
    Button x = sender as Button;
    Point p = PointToClient(System.Windows.Forms.Control.MousePosition);
    this.label1.Text = p.X.ToString() + " " + p.Y.ToString();
    if (p.X > x.Location.X + 250 || p.Y > x.Location.Y+125)
    {
        button1.Size = new Size(250, 125);
    }
}
编辑2

好的,如果按钮不在
0,0
中,那么最好像这样使用
PointToClient

private void button1_MouseMove(object sender, MouseEventArgs e)
{
    Button x = sender as Button;
    if (e.X > 250|| e.Y >125)
    {
        x.Size = new Size(250, 125);
        x.Location = new Point(0, 0);
    }
}
private void button1_MouseMove(object sender, MouseEventArgs e)
{
    Button x = sender as Button;
    Point p = PointToClient(System.Windows.Forms.Control.MousePosition);
    this.label1.Text = p.X.ToString() + " " + p.Y.ToString();
    if (p.X > x.Location.X + 250 || p.Y > x.Location.Y+125)
    {
        button1.Size = new Size(250, 125);
    }
}

如果不在预期边界内,您可以处理MouseLeave和return。如果不在预期边界内,您可以处理MouseLeave和return。如果坐标是P(0,0)到P(250125),那么它在我的一个例子中起作用。如果坐标是从P(100200)到P(350325)呢?谢谢。但如果光标位于按钮的右侧和底部,则会触发mousemove。如果光标位于按钮的上方和左侧,则不会执行。需要明确的是,如果0,则不会触发≤x≤100 & 0≤Y≤谢谢在我的一个例子中如果坐标是P(0,0)到P(250125。如果坐标是从P(100200)到P(350325)呢?谢谢。但如果光标位于按钮的右侧和底部,则会触发mousemove。如果光标位于按钮的上方和左侧,则不会执行。需要明确的是,如果0,则不会触发≤x≤100 & 0≤Y≤200