2013-12-21 1 views
0

WinForms를 사용하여 시뮬레이션을위한 시각화 프로그램을 작성하고 있습니다. 시각화에는 그리드를 따라 움직이는 다양한 객체가 포함됩니다.크기 조정/변환 된 그래픽 객체를 가져올 수 있습니까?

지금까지 필자는 Panel을 확장하고 Paint 이벤트 중에 Graphics 클래스를 사용하여 사용자 지정 그리기를 수행하는 사용자 지정 컨트롤을 사용하고 있습니다. 그러나 한 가지 염려는 내가 계속 그리드 좌표에서 컨트롤로 물건을 확장해야한다는 것입니다 .DisplayRectangle 좌표 (즉, 그리드에서 2 개의 셀을 차지하는 객체는 2 * (control.DisplayRectangle.Width/horizontalGridWidth) 픽셀을 그릴 때).

그리드 좌표에서 내 그림을 표현할 수 있고 물리적 좌표로 자동 매핑되도록 그래픽 객체에서 이러한 변환을 수행하는 방법이 있습니까?

실제로 매트릭스가 핵심 이었음이 밝혀졌습니다 (수락 된 답변 참조). 다음은 작동하는 코드입니다.

public SimulationPanel() { 
    this.DoubleBuffered = true; 
    this.SizeChanged += (o, e) => this.Invalidate(); 
    this.Paint += this.PaintPanel; 
} 

private void Paint(object sender, PaintEventArgs e) { 
       e.Graphics.Clear(Color.Black); 

      var fromRectangle = GetSimulationWorldCoordinates(); 
      var toRectangle = ScaleToFit(fromRectangle, this.DisplayRectangle); 

      using (var matrix = new Matrix(
          fromRectangle, 
          new[] { 
           toRectangle.Location, 
           new Point(toRectangle.Right, toRectangle.Top), 
           new Point(toRectangle.Left, toRectangle.Bottom), 
         })) 
      { 
           // draw the simulation stuff here using simulation coordinates 
       e.Graphics.Transform = matrix; 
       e.Graphics.FillRectangle(Brushes.LightBlue, toRectangle); 
       e.Graphics.DrawLine(Pens.Red, toRectangle.Location, new Point(toRectangle.Right, toRectangle.Bottom)); 
      } 
} 
+0

Graphics.ScaleTransform() 만 사용하면됩니다. ScaleTransform의 두 번째 인수에 음수 값이있는 TranslateTransform()을 사용하면 좌표계를 반전하고 왼쪽 하단 구석에서 원점을 얻을 수 있습니다. –

답변

2

이 코드는 어떻습니까?

그리드를 알 수 없기 때문에 격자 대신 레이블을 사용합니다.

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

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     GraphicsPath path = new GraphicsPath(); 
     RectangleF pathRect; 

     public Form1() 
     { 
      InitializeComponent(); 

      Location = new Point(0, 0); 
      Size = new System.Drawing.Size(500, 500); 

      Label lbl1 = new Label(); 
      lbl1.Location = new Point(100, 100); 
      lbl1.Size = new System.Drawing.Size(200, 100); 
      lbl1.BorderStyle = BorderStyle.FixedSingle; 
      lbl1.Paint += new PaintEventHandler(lbl_Paint); 

      Label lbl2 = new Label(); 
      lbl2.Location = new Point(300, 200); 
      lbl2.Size = new System.Drawing.Size(100, 200); 
      lbl2.BorderStyle = BorderStyle.FixedSingle; 
      lbl2.Paint += new PaintEventHandler(lbl_Paint); 

      Controls.Add(lbl1); 
      Controls.Add(lbl2); 

      path.AddRectangle(new Rectangle(50, 50, 150, 150)); 
      path.AddEllipse(new Rectangle(25, 50, 25, 50)); 
      pathRect = path.GetBounds(); 
     } 

     void lbl_Paint(object sender, PaintEventArgs e) 
     { 
      var rect = ((Control)sender).DisplayRectangle; 

      PointF[] plgpts = new PointF[] { 
       new PointF(rect.Left, rect.Top), 
       new PointF(rect.Right - 1, rect.Top), 
       new PointF(rect.Left, rect.Bottom - 1), 
      }; 

      Graphics g = e.Graphics; 
      g.Clear(SystemColors.Control); 
      using (Matrix matrix = new Matrix(path.GetBounds(), plgpts)) 
      { 
       g.Transform = matrix; 
       g.DrawPath(Pens.Red, path); 
      } 
     } 
    } 
} 
+0

* using * 문을 사용하여 매트릭스를 처리하십시오. –

+0

내 기사에 대한 귀하의 의견에 감사드립니다. – user3093781