繁体   English   中英

画一个文本框

[英]Painting a TextBox

我需要一种使TextBox看起来像平行四边形的方法,但是我不知道该怎么做。 我目前有以下代码:

private void IOBox_Paint(object sender, PaintEventArgs e)
{
    Graphics g = e.Graphics;
    Point cursor = PointToClient(Cursor.Position);
    Point[] points = { cursor, new Point(cursor.X + 50, cursor.Y), new Point(cursor.X + 30, cursor.Y - 20),
                         new Point(cursor.X - 20, cursor.Y - 20) };
    Pen pen = new Pen(SystemColors.MenuHighlight, 2);
    g.DrawLines(pen, points);
}

但是显然它没有用。 我放错了地方/误用了它,或者我没有做正确的事。 这是我用来添加它的方法。

int IOCounter = 0;
private void inputOutput_Click(object sender, EventArgs e)
{
    IOBox box = new IOBox();
    box.Name = "IOBox" + IOCounter;
    IOCounter++;
    box.Location = PointToClient(Cursor.Position);
    this.Controls.Add(box);
}

任何想法我该如何解决? IOBox是我制作的UserControl,其中包含一个TextBox。 这样做是正确的吗?

如果可能,您应该使用WPF制作应用程序。 WPF旨在完全按照您的意图进行。

但是,尽管不容易,但可以在WinForms中完成。 您将需要创建一个继承TextBox WinForm控件的新类。 这是使TextBox看起来像圆形的示例:

public class MyTextBox : TextBox
{
    public MyTextBox() : base()
    {
        SetStyle(ControlStyles.UserPaint, true);
        Multiline = true;
        Width = 130;
        Height = 119;
    }

    public override sealed bool Multiline
    {
        get { return base.Multiline; }
        set { base.Multiline = value; }
    }

    protected override void OnPaintBackground(PaintEventArgs e)
    {
        var buttonPath = new System.Drawing.Drawing2D.GraphicsPath();
        var newRectangle = ClientRectangle;

        newRectangle.Inflate(-10, -10);
        e.Graphics.DrawEllipse(System.Drawing.Pens.Black, newRectangle);
        newRectangle.Inflate(1, 1);
        buttonPath.AddEllipse(newRectangle);
        Region = new System.Drawing.Region(buttonPath);

        base.OnPaintBackground(e);
    }      
}

请记住,您仍然必须做其他事情,例如剪切文本等。但这应该可以帮助您入门。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM