文字未显示在自定义按钮控件上吗? [英] Text not displaying on custom button control?

查看:192
本文介绍了文字未显示在自定义按钮控件上吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为Windows形式的电影和娱乐系统制作了一个自定义控件.控件是按钮,这是基本的自定义控件,只有两种不同的颜色组合在一起并且是透明的.我将控件放在Windows窗体中,并且工作正常,但是当我将文本添加到按钮时,文本不会显示吗?我以为起初它与我使用的颜色有关,但是那没有影响吗?我更改了按钮属性中的文本,是否需要更改代码中的文本,而不仅仅是属性?

以下是控件的代码:

Hi, I''ve made a custom control for a film and entertainment system in a windows form. The control is button which is the basic custom control with just two different colours put together and are transparaent. I put the control in my windows form and it work fine but when I add text to the button the text doesnt display? I thought at first it had something to do with the colours I used, but that hasn''t affected it? I changed the text in the properties for the button, do I need to change the text in the code, instead of just the properties?

Heres the code for the control:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Button
{
    public partial class CustomControl : Control
    {
        public CustomControl()
        {
            InitializeComponent();
        }
        
        
    private Color m_color1 = Color.Green; // first color
    private Color m_color2 = Color.Red;  // second color
    private int m_color1Transparent = 100; // transparency degree 
                                             // (applies to the 1st color)
    private int m_color2Transparent = 100; // transparency degree 
                                          //  (applies to the 2nd color)

    public Color Color1
    {
        get { return m_color1; }
        set { m_color1 = value; Invalidate(); }
    }
    public Color Color2
    {
        get { return m_color2; }
        set { m_color2 = value; Invalidate(); }
    }
    public int Transparent1
    {
        get { return m_color1Transparent; }
        set { m_color1Transparent = value; Invalidate(); }
    }
    public int Transparent2
    {
        get { return m_color2Transparent; }
        set { m_color2Transparent = value; Invalidate(); }
    }

  

    protected override void OnPaint(PaintEventArgs pe)
    {
        // Calling the base class OnPaint
        base.OnPaint(pe);
        // Create two semi-transparent colors
        Color c1 = Color.FromArgb
            (m_color1Transparent , m_color1);
        Color c2 = Color.FromArgb
            (m_color2Transparent , m_color2);
        Brush b = new System.Drawing.Drawing2D.LinearGradientBrush
            (ClientRectangle, c1, c2, 10);
        pe.Graphics.FillRectangle (b, ClientRectangle);
        b.Dispose();
    }
private void f1_paint(object sender, PaintEventArgs e)
    {
        // Get Graphics Object
        Graphics g = e.Graphics;

        // Method under System.Drawing.Graphics
        g.DrawString("Download Film", new Font("Verdana", 20),
        new SolidBrush(Color.Tomato), 40, 40);
    }


}
}

推荐答案

当您通过继承Control(或UserControl)从头开始时,您需要自己做所有事情,包括绘制文本.您不是在绘画处理程序中绘画文本,因此不会显示任何文本.在绘画处理程序中添加包含g.DrawString的行.

正确地重新实现按钮并不是一件容易的事,您可能要考虑自己绘制按钮而不是创建一个全新的控件,尽管那样会影响透明度.
When you start from scratch by inheriting Control (or UserControl) you need to do everything yourself, including painting the text. You are not painting text in your paint handler, so no text appears. Add a line including g.DrawString in your paint handler.

Correctly re-implementing a button is non-trivial and you might want to consider just painting the button yourself instead of creating a whole new control, though that messes with transparency.


更改属性应该足够.

没有看到您的代码,很难确定.它可能只需要 NotifyParentProperty(true)属性 [
Changing the property should be sufficient.

Without seeing your code, it is difficult to be sure. It may just need the NotifyParentProperty(true) attribute[^] applying, or it could be more complex.


As has been said you haven''t painted the text. You need to add something like:
..................
..................
pe.Graphics.FillRectangle(b, ClientRectangle);
b.Dispose();
pe.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), Rectangle.Inflate(this.ClientRectangle, -1, -1));



我将其作为练习来安排文本位置.
[/Edit]



I''ll leave it as an exercise for you to arrange the text position.
[/Edit]


您能保守秘密吗?当然可以. :-)

在您的代码中,没有这样的行可以呈现任何文本.一点也不.基类(您调用base.OnPaint)也不会这样做.就如此容易.别担心:我不会告诉任何人...

—SA
Can you keep a secret? Of course you can. :-)

In your code, there is not such line which would render any text. Not at all. Base class (you call base.OnPaint) does not do it, too. As simple as that. Don''t worry: I won''t tell anybody…

—SA


这篇关于文字未显示在自定义按钮控件上吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆