位置标志怎么画?

发布于 2024-11-04 19:01:52 字数 138 浏览 10 评论 0原文

我用 C# 为 Winforms 编写了自己的文本框控件。

我无法弄清楚的一件事:如何绘制各种尺寸的文本位置标志?

文本位置符号

I writing my own textbox control in C# for Winforms.

One thing i can't figure out: how to draw the text position sign in various sizes?

Text position sign

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

握住我的手 2024-11-11 19:01:52

它被称为“插入符号”。 winapi 函数没有被 winform 封装,您必须 pinvoke 它们。从开始阅读。您可以在我的答案此处<找到代码/a>.

It is called the 'caret'. The winapi functions are not wrapped by winforms, you'll have to pinvoke them. Start reading here. You'll find code in my answer here.

不美如何 2024-11-11 19:01:52

试试这个。

我创建了一个方法,该方法旨在从您正在绘制的任何控件的绘画处理程序中调用。为简单起见,我只在我的表单中使用了表单本身。您可能有一个面板或其他一些控件。

该方法接受图形对象、光标的比例以及开始绘制的上/左位置。比例只是高度,但所有数学运算都是相对于高度进行的。您可以按照您想要的方式调整这些数字。

    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);
    }

此示例产生以下输出:

sample

Try this.

I created a method which is meant to be called from the paint handler of whichever control you're drawing in. For simplicity I just used the form itself in mine. You probably have a panel or some other control.

The method accepts the graphics object, the scale of the cursor and the upper/left position of where to start drawing. The scale is just the height but all the math is performed relative to the height. You can tweak those numbers any way you want.

    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);
    }

This sample produces the following output:

sample

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文