C# 绘制文本框
我需要一种方法使文本框看起来像一个平行四边形,但我不知道怎么做。我目前有以下代码:C# 绘制文本框,c#,.net,winforms,C#,.net,Winforms,我需要一种方法使文本框看起来像一个平行四边形,但我不知道怎么做。我目前有以下代码: 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), n
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是我制作的一个用户控件,它包含一个文本框。这样做对吗?如果可能,您应该使用WPF创建应用程序。WPF的设计目的正是为了完成您想要做的事情 然而,它可以在WinForms中完成,尽管并不容易。您需要创建一个继承
TextBox
WinForm控件的新类。下面是一个使文本框看起来像圆形的示例:
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);
}
}
请记住,您仍然需要做其他事情,例如剪切文本等,但这应该可以让您开始。这并不容易。你可以看看。您还可以考虑使用WPF技术使应用程序易于更改控件的设计。文本框是一个低级别的Windows组件,不能使用GDI+轻松地重新设计。对修改任何标准Windows控件的支持都是有限的,一般来说,我希望在任何尝试的解决方案中都能看到图形人工制品。正如前面的建议,如果你想摆脱标准的WinForms控件,我认为WPF是更好的选择。同样的答案是:不。问题是,我对WPF一点也不擅长,而且它看起来很凌乱,所以我只能坚持使用它。你有什么好的教程让我开始学习吗?我想通过选择一个用户控件,你给了它一个好的开始,这可能是使用实际文本框的唯一干净实用的方法。它以什么方式不起作用?它应该是什么样子?你能贴一张照片吗?另外:在绘制事件中,所有坐标可能都应该相对于UC,从(0,0)左右开始,而不是相对于光标!