我用 C# 为 Winforms 编写自己的文本框控件。
我想不通的一件事:如何绘制各种尺寸的文本位置标志?
尝试这个。
我创建了一个方法,该方法旨在从您正在绘制的任何控件的绘制处理程序中调用。为简单起见,我只是在我的表单中使用了表单本身。您可能有一个面板或其他控件。
该方法接受图形对象、光标的比例和开始绘图的上/左位置。比例只是高度,但所有数学运算都是相对于高度执行的。您可以随意调整这些数字。
private void Form1_Paint(object sender, PaintEventArgs e)
{
DrawCaret(e.Graphics, 30, new Point(20, 20));
DrawCaret(e.Graphics, 50, new Point(100, 100));
}
private static void DrawCaret(Graphics g, int scale, Point loc)
{
g.SmoothingMode = SmoothingMode.HighQuality;
int height = scale;
int width = scale/10;
int rectBase = scale/5;
g.FillRectangle(Brushes.Black, loc.X, loc.Y, width, height);
var path = new GraphicsPath();
path.AddPolygon(new[]
{
new Point(loc.X+width, loc.Y),
new Point(loc.X+width+rectBase/2, loc.Y+rectBase/2),
new Point(loc.X+width, loc.Y+rectBase),
});
g.FillPath(Brushes.Black, path);
}
此示例产生以下输出: