如何将文本框控件的行为添加到自定义控件中?

时间:2016-03-31 05:13:34

标签: c# textbox custom-controls

我有一个自定义的圆角文本框。但我无法添加文本框行为,如文本编辑,文本选择等。如果我决定自己做这些属性需要很长时间。如何将此属性添加到我的文本框中?

我的TextBox类:

public class AltoTextBox : Control
{
    public AltoTextBox()
    {
        SetStyle(ControlStyles.AllPaintingInWmPaint | 
                 ControlStyles.SupportsTransparentBackColor |
                 ControlStyles.OptimizedDoubleBuffer | 
                 ControlStyles.UserPaint, true);

        BackColor = Color.Transparent;
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        e.Graphics.SmoothingMode = SmoothingMode.HighQuality;

        RoundedRectangleF strokeRect = new RoundedRectangleF(Width, Height, 10);
        RoundedRectangleF innerRect = new RoundedRectangleF(Width - 0.5f, Height - 0.5f, 10f, 0.5f, 0.5f);


        e.Graphics.DrawPath(Pens.Black, strokeRect.Path);
        e.Graphics.FillPath(Brushes.White, innerRect.Path);

    }
 }
 public class RoundedRectangleF
{

    Point location;
    float radius;
    GraphicsPath grPath;
    float x, y;
    float width, height;
    public RoundedRectangleF(float width, float height, float radius,float x = 0,float y = 0)
    {
        location = new Point(0, 0);
        this.radius = radius;

        RectangleF upperLeftRect = new RectangleF(x, y, 2 * radius, 2 * radius);
        RectangleF upperRightRect = new RectangleF(width - 2 * radius - 1, x, 2 * radius, 2 * radius);
        RectangleF lowerLeftRect = new RectangleF(x, height - 2 * radius - 1, 2 * radius, 2 * radius);
        RectangleF lowerRightRect = new RectangleF(width - 2 * radius - 1, height - 2 * radius - 1, 2 * radius, 2 * radius);

        grPath = new GraphicsPath();
        grPath.AddArc(upperLeftRect, 180, 90);
        grPath.AddArc(upperRightRect, 270, 90);
        grPath.AddArc(lowerRightRect, 0, 90);
        grPath.AddArc(lowerLeftRect, 90, 90);
        grPath.CloseAllFigures();
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }
    public RoundedRectangleF()
    {
    }
    public GraphicsPath Path
    {
        get
        {
            return grPath;
        }
    }
    public RectangleF Rect
    {
        get
        {
            return new RectangleF(x, y, width, height);
        }
    }
    public float Radius
    {
        get
        {
            return radius;
        }
        set
        {
            radius = value;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我找到了Hazeldev's custom controls的解决方案。

在此解决方案中,我们添加了一个文本框控件作为我们的子控件。

public class AltoTextBox : Control
{
    int radius = 15;
    public TextBox box = new TextBox();
    GraphicsPath Shape;
    public AltoTextBox()
    {
        SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        SetStyle(ControlStyles.UserPaint, true);
        SetStyle(ControlStyles.ResizeRedraw, true);

        AddTextBox();
        Controls.Add(box);

        BackColor = Color.Transparent;
        ForeColor = Color.DimGray;

        Text = null;
        Font = new Font("Comic Sans MS", 11);
        Size = new Size(135, 33);
        DoubleBuffered = true;
    }
    void AddTextBox()
    {
        box.Size = new Size(Width - 2*radius, Height - 6);
        box.Location = new Point(radius, 3);
        box.BorderStyle = BorderStyle.None;
        box.TextAlign = HorizontalAlignment.Left;
        box.Multiline = true;
        box.Font = Font;
    }
    protected override void OnBackColorChanged(EventArgs e)
    {
        base.OnBackColorChanged(e);
    }
    protected override void OnTextChanged(EventArgs e)
    {
        base.OnTextChanged(e);
        box.Text = Text;
    }
    GraphicsPath innerRect;
    protected override void OnFontChanged(EventArgs e)
    {
        base.OnFontChanged(e);
        box.Font = Font;
    }
    protected override void OnResize(System.EventArgs e)
    {
        base.OnResize(e);
        Shape = new RoundedRectangleF(Width, Height, radius).Path;
        innerRect = new RoundedRectangleF(Width - 0.5f, Height - 0.5f, radius, 0.5f, 0.5f).Path;

        AddTextBox();
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        Bitmap bmp = new Bitmap(Width, Height);
        Graphics grp = Graphics.FromImage(bmp);
        grp.SmoothingMode = SmoothingMode.HighQuality;
        grp.DrawPath(Pens.Gray, Shape);
        grp.FillPath(Brushes.White, innerRect);
        e.Graphics.DrawImage((Image)bmp.Clone(), 0, 0);

        base.OnPaint(e);
    }

}