Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在鼠标悬停或单击时重新绘制按钮图形_C#_.net_Winforms_Button - Fatal编程技术网

C# 在鼠标悬停或单击时重新绘制按钮图形

C# 在鼠标悬停或单击时重新绘制按钮图形,c#,.net,winforms,button,C#,.net,Winforms,Button,我创建了一个自定义按钮(CloseButton),如下所示: 当我将鼠标移到按钮上时,它会将颜色变为红色,如下所示: 我想在鼠标悬停在按钮上时,将按钮上的“X”重新绘制为白色,使其看起来像这样(我已经用油漆修改了上面的图像,以显示我希望它的外观): 我不知道该怎么做 我的自定义按钮基本上继承了常规按钮控件,并带有一些自定义设置。代码如下: public class CloseButton : Button { public CloseButton() { t

我创建了一个自定义按钮(CloseButton),如下所示:

当我将鼠标移到按钮上时,它会将颜色变为红色,如下所示:

我想在鼠标悬停在按钮上时,将按钮上的“X”重新绘制为白色,使其看起来像这样(我已经用油漆修改了上面的图像,以显示我希望它的外观):

我不知道该怎么做

我的自定义按钮基本上继承了常规按钮控件,并带有一些自定义设置。代码如下:

public class CloseButton : Button
{
    public CloseButton()
    {
        this.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
        this.FlatAppearance.BorderSize = 0;
        this.FlatAppearance.MouseDownBackColor = System.Drawing.Color.IndianRed;
        this.FlatAppearance.MouseOverBackColor = System.Drawing.Color.DarkRed;
        this.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
        this.Size = new System.Drawing.Size(50, 30);
        this.UseVisualStyleBackColor = true;
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        GraphicsPath gPath;
        Pen gPen;
        gPen = new Pen(System.Drawing.SystemColors.ControlText);
        gPath = new GraphicsPath();
        gPath.AddLine(20, 10, 30, 20);
        gPath.CloseFigure();
        gPath.AddLine(20, 20, 30, 10);
        gPath.CloseFigure();
        e.Graphics.DrawPath(gPen, gPath);
    }
}
if ((this.BackColor.Equals(System.Drawing.Color.DarkRed) || this.BackColor.Equals(System.Drawing.Color.IndianRed))
{
    gPen = new Pen(System.Drawing.SystemColors.ControlLightLight;
}
else
{
    gPen = new Pen(System.Drawing.SystemColors.ControlText;
}
_BtnClose.Invalidate();
_BtnClose.Refresh();
我尝试更改OnPaint()方法,使笔的颜色由控件的背景色决定,如下所示:

public class CloseButton : Button
{
    public CloseButton()
    {
        this.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
        this.FlatAppearance.BorderSize = 0;
        this.FlatAppearance.MouseDownBackColor = System.Drawing.Color.IndianRed;
        this.FlatAppearance.MouseOverBackColor = System.Drawing.Color.DarkRed;
        this.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
        this.Size = new System.Drawing.Size(50, 30);
        this.UseVisualStyleBackColor = true;
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        GraphicsPath gPath;
        Pen gPen;
        gPen = new Pen(System.Drawing.SystemColors.ControlText);
        gPath = new GraphicsPath();
        gPath.AddLine(20, 10, 30, 20);
        gPath.CloseFigure();
        gPath.AddLine(20, 20, 30, 10);
        gPath.CloseFigure();
        e.Graphics.DrawPath(gPen, gPath);
    }
}
if ((this.BackColor.Equals(System.Drawing.Color.DarkRed) || this.BackColor.Equals(System.Drawing.Color.IndianRed))
{
    gPen = new Pen(System.Drawing.SystemColors.ControlLightLight;
}
else
{
    gPen = new Pen(System.Drawing.SystemColors.ControlText;
}
_BtnClose.Invalidate();
_BtnClose.Refresh();
然后在我的MouseEnter和Click handlers中,按如下方式使控件无效并刷新:

public class CloseButton : Button
{
    public CloseButton()
    {
        this.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
        this.FlatAppearance.BorderSize = 0;
        this.FlatAppearance.MouseDownBackColor = System.Drawing.Color.IndianRed;
        this.FlatAppearance.MouseOverBackColor = System.Drawing.Color.DarkRed;
        this.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
        this.Size = new System.Drawing.Size(50, 30);
        this.UseVisualStyleBackColor = true;
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        GraphicsPath gPath;
        Pen gPen;
        gPen = new Pen(System.Drawing.SystemColors.ControlText);
        gPath = new GraphicsPath();
        gPath.AddLine(20, 10, 30, 20);
        gPath.CloseFigure();
        gPath.AddLine(20, 20, 30, 10);
        gPath.CloseFigure();
        e.Graphics.DrawPath(gPen, gPath);
    }
}
if ((this.BackColor.Equals(System.Drawing.Color.DarkRed) || this.BackColor.Equals(System.Drawing.Color.IndianRed))
{
    gPen = new Pen(System.Drawing.SystemColors.ControlLightLight;
}
else
{
    gPen = new Pen(System.Drawing.SystemColors.ControlText;
}
_BtnClose.Invalidate();
_BtnClose.Refresh();

这没什么用。我如何才能实现我正在努力做的事情?如果我卡住了,我总是可以创建另一个自定义按钮,上面画一个白色的X,然后根据需要显示/隐藏此按钮,但如果可能,我希望避免这样做,我会这样写,然后:

public class CloseButton : Button
{

    GraphicsPath gPath;

    public CloseButton()
    {
        this.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
        this.FlatAppearance.BorderSize = 0;
        this.FlatAppearance.MouseDownBackColor = System.Drawing.Color.IndianRed;
        this.FlatAppearance.MouseOverBackColor = System.Drawing.Color.DarkRed;
        this.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
        this.Size = new System.Drawing.Size(50, 30);
        this.UseVisualStyleBackColor = true;

        gPath = new GraphicsPath();
        gPath.AddLine(20, 10, 30, 20);
        gPath.CloseFigure();
        gPath.AddLine(20, 20, 30, 10);
        gPath.CloseFigure();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        bool cursorInControl = this.ClientRectangle.Contains(this.PointToClient(Cursor.Position));
        using (Pen gPen = new Pen(cursorInControl ? System.Drawing.SystemColors.ControlLightLight : System.Drawing.SystemColors.ControlText))
        {
            e.Graphics.DrawPath(gPen, gPath);
        }                
    }
}

GraphicsPath从不更改,因此它在控制级别声明并保留以供将来使用,而不是每次重新创建它。此外,无论何时创建笔,都必须将其丢弃,从而使用
块。最后,我们将电流转换为,以便确定它是否在控制范围内。这用于在绘制图形时切换颜色。另一个绘制十字和更改十字颜色的简单选项是:

  • 您可以使用
    用于绘制十字的字符。您可以通过字体大小轻松更改其大小
  • 更改
    颜色,足够处理
    MouseEnter
    MouseLeave
    并分配
    ForeColor
    。它将重新绘制按钮
  • 您可以使用
    最大化字符,
    用于恢复和
    用于最小化:

通过向构造函数中添加以下行,可以使按钮不可选择,以防止鼠标或键盘选择:

SetStyle(ControlStyles.Selectable, false);
通过添加以下代码,可以将关闭行为添加到按钮
ForeColor

protected override void OnClick(EventArgs e)
{
    base.OnClick(e);
    FindForm()?.Close();
}
如果您想要一个带有较大十字的较大按钮,请更改按钮大小及其字体大小:


MouseOverBackColor在按钮渲染器中用于绘制按钮,它不会更改背景色。考虑一个简单的BOOL字段,在OnMousEnter()中设置为true,并返回OnMouSeLeavee()中的“false”。