我对c#表格很新,我想在特定的位置写一些字母。正如你在附图中看到的那样,我画了一条带有X和Y轴的曲线,用它来缩放。我想在水平线的边缘写字母X,在垂直线的顶边缘写Y.还有什么方法可以在线上分配值吗?
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);
答案 0 :(得分:3)
检查我的样本。我希望它有所帮助。
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);
}
}
}
}
答案 1 :(得分:0)
&#34;检查图形对象上的DrawString。 https://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawstring.aspx - dbugger&#34; 回答。