如何在按钮上写入文本,但在每个按钮上写入不同的文本?

问题描述 投票:0回答:1

我有一个类类型

UserControl
,因为类很大很长,超过800行,我会尝试只添加重要的部分。

我创建了一个公共文本属性:

private string _buttonText = "Button";  // Default text
public string ButtonText
{
    get { return _buttonText; }
    set
    {
        _buttonText = value;
        Invalidate();  // Redraw the button when text changes
    }
}

然后在

OnPaint
事件中调用方法名
DrawButtonText
:

protected override void OnPaint(PaintEventArgs e)
{
    DrawButton();
    DrawButtonText(e.Graphics);
    base.OnPaint(e);
}

还有

DrawButtonText
方法

private void DrawButtonText(Graphics graphics)
{
    using (Font font = new Font("Arial", 12, FontStyle.Bold))
    {
        SizeF textSize = graphics.MeasureString(_buttonText, font);
        PointF locationToDraw = new PointF((this.Width / 2) - (textSize.Width / 2), (this.Height / 2) - (textSize.Height / 2));

        using (SolidBrush brush = new SolidBrush(Color.White))  // Ensure text color contrasts well with button
        {
            graphics.DrawString(_buttonText, font, brush, locationToDraw);
        }
    }
}

然后是在

form1
构造函数中使用它的示例:

public frmMain()
{
    InitializeComponent();

    Image originalImage = Image.FromFile(@"D:\Csharp Projects\Screen Recorder\Resources\pngimg.com - buttons_PNG152.png");
    Image resizedImage = ResizeImage(originalImage, new Size(100, 100));

    GlowButton.GlowButton glowButton1 = new GlowButton.GlowButton
    {
        ButtonText = "REC",  // Set the specific text for this button
        Image = resizedImage,
        Size = new Size(100, 100),
        Location = new Point(10, (this.ClientSize.Height - 100) / 2),
        ApplyDarkEffect = true  // Assuming you have this property to toggle the dark effect
    };

    this.Controls.Add(glowButton1);

    SetButtonBehavior(glowButton1, ButtonBehavior.Default);
}

以及DrawButton方法代码:

/// <summary>Drawing hub</summary>
private void DrawButton()
{
    if (this.Image == null)
        return;

    Rectangle bounds = new Rectangle(0, 0, this.Width, this.Height);
    Rectangle imageBounds = GetImageBounds(bounds, this.Image);

    // draw into a buffer
    using (Graphics g = Graphics.FromImage(_bmpBuffer))
    {
        g.SmoothingMode = SmoothingMode.HighQuality;
        DrawBackGround(g, bounds);
        if (this.ImageMirror)
            DrawMirror(g, imageBounds);

        if (this.CheckStyle != CheckedStyle.None && this.Checked == true)
            _eButtonState = ButtonState.Checked;
        else if (this.CheckStyle != CheckedStyle.None && this.Checked == false && _eButtonState == ButtonState.Focused)
            _eButtonState = ButtonState.Normal;
        switch (_eButtonState)
        {
            case ButtonState.Checked:
                {
                    if (this.CheckStyle == CheckedStyle.Border)
                    {
                        DrawBorder(g, bounds, this.CheckedBorderColor);
                        DrawColoredImage(g, this.Image, imageBounds, this.ImageFocusedColor);
                    }
                    else if (this.CheckStyle == CheckedStyle.ColorChange)
                    {
                        DrawColoredImage(g, this.Image, imageBounds, this.ImageCheckedColor);
                    }
                    else
                    {
                        DrawColoredImage(g, this.Image, imageBounds, this.ImageFocusedColor);
                    }
                    break;
                }
            case ButtonState.Disabled:
                {
                    if (this.ImageDisabledColor == Color.Transparent)
                        DrawFadedImage(g, this.Image, imageBounds);
                    else
                        DrawColoredImage(g, this.Image, imageBounds, this.ImageDisabledColor);
                    break;
                }
            case ButtonState.Focused:
                {
                    DrawColoredImage(g, this.Image, imageBounds, this.ImageFocusedColor);
                    if (this.FocusedMask)
                    {
                        DrawMask(g, bounds);
                        DrawBorder(g, bounds, Color.DarkGray);
                    }
                    break;
                }
            case ButtonState.Hover:
                {
                    DrawGlow(g, this.Image, imageBounds);
                    DrawColoredImage(g, this.Image, imageBounds, this.ImageHoverColor);
                    break;
                }
            case ButtonState.Normal:
                {
                    DrawImage(g, this.Image, imageBounds);
                    break;
                }
            case ButtonState.Pressed:
                {
                    DrawColoredImage(g, this.Image, imageBounds, this.ImagePressedColor);
                    break;
                }
        }
    }
    // draw the buffer
    using (Graphics g = Graphics.FromHwnd(this.Handle))
        DrawImage(g, _bmpBuffer, bounds);
}

结果和问题是第一次运行应用程序后,大红色按钮上应该有一个文本“REC”。但底部的所有其他按钮上也添加了“REC”文本。

另一个问题是,当我单击按钮时,大红色按钮中的文本“REC”消失了。

红色大按钮是

glowButton1

我希望“REC”文本仅显示在大红色

glowingButton1
上,如果未添加文本并将文本设置为任何其他按钮,也不要在其上写入文本。单击按钮时,文本将保留。

on the left in the screenshot image the application after first time running.  on the right after i clicked the big red button and the text is gone.

c# winforms
1个回答
0
投票

我认为你应该将绘制操作视为原子操作,而不是调用绘制背景和调用绘制字母,因此不要单独调用

DrawButton
DrawButtonText
,只需简单地
Invalidate
控制任何时间任何与控件变化相关的事情。例如,更改此:

public Image BackgroundImage
{
    get { return _imgBackgroundImage; }
    set
    {
        _imgBackgroundImage = value;
        if (_imgBackgroundImage != null)
            DrawButton(); //Avoid calling the DrawButton method on its own
    }
}

对此:

public Image BackgroundImage
{
    get { return _imgBackgroundImage; }
    set
    {
        _imgBackgroundImage = value;
        if (_imgBackgroundImage != null)
            Invalidate(); //Use this instead
    }
}

另外,更改调用顺序,以便首先调用基类的

OnPaint
方法,然后在其上进行绘制:

protected override void OnPaint(PaintEventArgs e)
{            
    base.OnPaint(e);
    DrawButton();
    DrawButtonText(e.Graphics);
}

至于为什么其他按钮以这种方式绘制,请提供连接您从工具箱中删除的按钮的代码。正如所写,这不应影响其他按钮。

© www.soinside.com 2019 - 2024. All rights reserved.