简体   繁体   English

用C#格式写字母和数字

[英]Writing Letters and Numbers in C# form

在此处输入图片说明 I am very new to c# form and i would like to write some letters in a specific location. 我对C#表单非常陌生,我想在特定位置写一些字母。 As you can see in the picture i have attached I've drawn a curved Line with a X and Y axis to scale it with. 正如您在图片中看到的那样,我已附加了一条带有X和Y轴的曲线以对其进行缩放。 I would like to write the letter X on the edge of the horizontal line and Y on the top Edge of the vertical line. 我想在水平线的边缘写字母X,在垂直线的顶部写字母Y。 Also is there any possible ways to assign values on the line as well? 还有任何可能的方法也可以在行上分配值?

protected override void OnPaint(PaintEventArgs e)
    {


        float a = 1, b = 5, c = -4;
        double x1, x2, x3, x4, x5, x6, y1, y2, y3, y4, y5, y6, x7, y7, delta;
        delta = (b * b) - (4 * a * c);
        x1 = ((b * (-1)) + Math.Sqrt(delta)) / (2 * a);
        x6 = ((b * (-1)) - Math.Sqrt(delta)) / (2 * a);
        y6 = a * (x6 * x6) + (b * (x6)) + c;
        y1 = a * (x1 * x1) + (b * (x1)) + c;
        x2 = 3;
        y2 = a * (x2 * x2) + (b * (x2)) + c;
        x3 = -3;
        y3 = a * (x3 * x3) + (b * (x3)) + c;
        x4 = 5;
        y4 = a * (x4 * x4) + (b * (x4)) + c;
        x5 = -10;
        y5 = a * (x5 * x5) + (b * (x5)) + c;
        x7 = 0;
        y7 = a * (x7 * x7) + (b * (x7)) + c;

        int cx1 = Convert.ToInt32(x1);
        int cx2 = Convert.ToInt32(x2);
        int cx3 = Convert.ToInt32(x3);
        int cy1 = Convert.ToInt32(y1);
        int cy2 = Convert.ToInt32(y2);
        int cy3 = Convert.ToInt32(y3);
        int cx4 = Convert.ToInt32(x4);
        int cy4 = Convert.ToInt32(y4);
        int cx5 = Convert.ToInt32(x5);
        int cy5 = Convert.ToInt32(y5);
        int cx6 = Convert.ToInt32(x6);
        int cy6 = Convert.ToInt32(y6);
        int cx7 = Convert.ToInt32(x7);
        int cy7 = Convert.ToInt32(x7);
        Graphics g = e.Graphics;
        int deltaX = 300;
        int deltaY = 300;
        g.TranslateTransform(deltaX, deltaY);
        float factor = 2.5f;
        Matrix m = new Matrix();
        m.Scale(factor, factor);
        g.MultiplyTransform(m);
        Pen aPen = new Pen(Color.Blue, 1);
        aPen.DashStyle = DashStyle.DashDot;
        Pen bPen = new Pen(Color.Green, 1);
        bPen.EndCap = LineCap.ArrowAnchor;
        Pen cPen = new Pen(Color.Green, 1);
        cPen.StartCap = LineCap.DiamondAnchor;
        Point point1 = new Point(cx1, -cy1);
        Point point2 = new Point(cx2, -cy2);
        Point point3 = new Point(cx3, -cy3);
        Point point4 = new Point(cx4, -cy4);
        Point point5 = new Point(cx5, -cy5);
        Point point6 = new Point(cx6, -cy6);
        Point pointa = new Point(20, -50);
        Point pointb = new Point(40, -30);
        Point pointc = new Point(60, -70);

        Point[] Points = { point5, point3, point1, point2, point4 };
        Point[] Pointss = { pointa, pointb,pointc };
        g.DrawCurve(new Pen(Color.Red, 1), Pointss);

        g.DrawCurve(aPen, Points);
        g.DrawLine((cPen), new Point(cx7, -100), new Point(cx7, 100));
        g.DrawLine((bPen), -100, 0, 100, 0);

check my sample. 检查我的样品。 I hope it helps. 希望对您有所帮助。

在此处输入图片说明

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace GraphicsForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            Invalidate();
        }

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

            var g = e.Graphics;

            var w = ClientRectangle.Width;
            var h = ClientRectangle.Height;
            var midY = h/2;
            var midX = w/2;

            var linePen = new Pen(Brushes.Red, 1)
            {
                StartCap = LineCap.DiamondAnchor,
                EndCap = LineCap.DiamondAnchor
            };

            //horizontal line
            g.DrawLine(linePen, 0, midY, w, midY);
            var font = Font;
            var measureStringX = g.MeasureString("x", font);
            g.DrawString("x", font, Brushes.Black, w - measureStringX.Width - 2, midY + 2);

            //vertical line
            g.DrawLine(linePen, midX, 0, midX, h);
            g.DrawString("y", font, Brushes.Black, midX + 2, 2);


            //horizontals&vertical marks
            const float marksCount = 12f;
            var wx = w / marksCount;
            var hx = h / marksCount;
            var markPen = new Pen(Brushes.Red, 1);
            for (int i = 1; i < marksCount; i++)
            {
                g.DrawLine(markPen, i * wx, midY, i * wx, midY + 5);
                g.DrawLine(markPen, midX, hx * i, midX + 5, hx * i);
            }
        }
    }
}

“检查图形对象上的DrawString。https : //msdn.microsoft.com/zh-cn/library/system.drawing.graphics.drawstring.aspx –调试器”回答。

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

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