标签多边形

问题描述:

enter image description here我有,我用它来createan点阵列的功能,这样我就可以绘制一个多边形,我想这个多边形内部的标签,我知道它可以使用束带或标签控制来完成,但我想要确保我有相同的方向的多边形在我的情况下,他们是矩形也控制字体大小取决于形状的大小。标签多边形

我试图创建的拉绳内部一个矩形没有工作,任何想法

private void BuildImage() 
    { 
     Graphics refGraph = this.CreateGraphics(); 
     IntPtr hdc = refGraph.GetHdc(); 
     Metafile image = new Metafile(hdc, EmfType.EmfOnly, "Shapes"); 
     using (var g = Graphics.FromImage(image)) 
     { 
      g.SmoothingMode = SmoothingMode.AntiAlias; 
      SolidBrush myBrush = new SolidBrush(Color.Black); 
      Pen p = new Pen(Color.Red, 0.2f); 
      foreach (CircuitData.ResistorRow resistorRow in ResistorData.Resistor) 
      { 
       RectangleF rec = new RectangleF((float)(resistorRow.CenterX - resistorRow.Length/2), (float)(resistorRow.CenterY - resistorRow.Width/2), (float)resistorRow.Length, (float)resistorRow.Width); 
       float orientation = 360 - (float)resistorRow.Orientation; 
       PointF center = new PointF((float)resistorRow.CenterX, (float)resistorRow.CenterY); 
       PointF[] points = CreatePolygon(rec, center, orientation); 
       if (!Double.IsNaN(resistorRow.HiX) && !Double.IsNaN(resistorRow.HiY)) 
       { 
        g.FillEllipse(myBrush, (float)resistorRow.HiX - 0.5f, (float)resistorRow.HiY - 0.5f, 1, 1); 
        g.DrawLine(p, new PointF((float)resistorRow.HiX, (float)resistorRow.HiY), center); 
       } 

       g.FillPolygon(myBrush, points); 

       Font drawFont = new Font("cursor", 1); 
       SolidBrush textBrush = new SolidBrush(Color.Blue); 
       //g.Transform = matrix; 
       g.DrawString(resistorRow.ComponentName, drawFont, textBrush, points[0]); 
      } 
     } 
     refGraph.ReleaseHdc(hdc); 
     refGraph.Dispose(); 
     Image = image; 

    } 


private PointF[] CreatePolygon(RectangleF rec, PointF center, float orientation) 
     { 
      PointF TL = new PointF(rec.Left, rec.Top); 
      PointF TR = new PointF(rec.Right, rec.Top); 
      PointF BL = new PointF(rec.Left, rec.Bottom); 
      PointF BR = new PointF(rec.Right, rec.Bottom); 
      PointF[] points = new PointF[] { BL, TL, TR, BR, BL }; 
      matrix = new System.Drawing.Drawing2D.Matrix(); 
      matrix.RotateAt(orientation, center); 
      matrix.TransformPoints(points); 
      return points; 
     } 
+0

强烈建议您使用WPF为这个而不是WinForms的。 –

应用的旋转矩阵的图形上下文与您合作:

 e.Graphics.Transform = matrix; 
     e.Graphics.FillPolygon(textBrush, points); 
     e.Graphics.DrawString(resistorRow.ComponentName, drawFont, textBrush2, TL); 

enter image description here

编辑:部分样本代码:

public class ResistorWithLabel 
{ 
    public string ComponentName { get; set; } 
    public RectangleF Rect { get; set; } 
    public float Orientation { get; set; } 
    public Color BackgroundColor { get; set; } 
    public Color ForegroundColor { get; set; } 
    public int FontSize { get; set; } 

    public void Draw(Graphics g) 
    { 
     Matrix contextMatrix = g.Transform; 
     Matrix matrix = new Matrix(); 
     matrix.RotateAt(Orientation, new PointF((Rect.Left+Rect.Right)/2, (Rect.Top+Rect.Bottom)/2)); 

     SolidBrush polygonBrush = new SolidBrush(BackgroundColor); 
     SolidBrush textBrush = new SolidBrush(ForegroundColor); 
     Font font = new Font("Courier", FontSize); 

     PointF TL = new PointF(Rect.Left, Rect.Top); 
     PointF TR = new PointF(Rect.Right, Rect.Top); 
     PointF BL = new PointF(Rect.Left, Rect.Bottom); 
     PointF BR = new PointF(Rect.Right, Rect.Bottom); 
     PointF[] points = new PointF[] { BL, TL, TR, BR }; 

     g.Transform = matrix; 
     g.FillPolygon(polygonBrush, points); 
     g.DrawString(ComponentName, font, textBrush, TL); 
     g.Transform = contextMatrix; 
    } 
} 

    private void Form3_Paint(object sender, PaintEventArgs e) 
    { 
     ResistorWithLabel r1 = new ResistorWithLabel(); 
     r1.ComponentName = "Resistor 1"; 
     r1.Rect = new RectangleF(50, 100, 100, 50); 
     r1.Orientation = 25; 
     r1.BackgroundColor = Color.Blue; 
     r1.ForegroundColor = Color.Yellow; 
     r1.FontSize = 16; 
     r1.Draw(e.Graphics); 

     ResistorWithLabel r2 = new ResistorWithLabel(); 
     r2.ComponentName = "Resistor 2"; 
     r2.Rect = new RectangleF(200, 100, 200, 100); 
     r2.Orientation = 75; 
     r2.BackgroundColor = Color.Gray; 
     r2.ForegroundColor = Color.Orange; 
     r2.FontSize = 32; 
     r2.Draw(e.Graphics); 
    } 

enter image description here

+0

我没有工作的我,我会更新我的代码,也许我做错事,我的多边形已经转化时,我,M试图改变字符串只是我的整个形状序列得到搞砸 –

+0

什么没有奏效? – jsanalytics

+0

当我在我的DrawString之前放入g.tranform = matrix时,我的字符串遍布整个地方。我为你更新了代码也是我的多边形改变了位置 –