2011-11-01 2 views
1

사용자가 양식의 모서리를 드래그하여 크기를 조정하려고하면 크기가 조정되는 그래프를 그릴 코드가 있습니다. 양식은 텍스트 상자에서 x 및 y 좌표를 가져옵니다. 버튼이 눌려지면, 나는 그들이 지적한 점을 그려보고 싶다. 그러나 Click 이벤트에는 Object Sender 및 EventArgs 매개 변수가 있습니다. OnPaint 메서드 (그래프를 그리기 위해 재정의)에는 PaintEventArgs 매개 변수가 있습니다.점을 그려야합니다.

g.DrawString("♫", new Font("Calibri", 12), new SolidBrush(Color.HotPink), (PlotArea.X + (7 - xMin)* PlotArea.Width/(xMax - xMin)), (PlotArea.Bottom - (6 - yMin) * PlotArea.Height/(yMax - yMin))); 

이 "g"는 타입있는 PaintEventArgs입니다 때문에 :

이로 인해 버튼을 클릭하면 다음 코드를 할 수 없습니다. onClick 메서드에서 좌표를 플롯 할 수 있도록이 문제를 어떻게 해결합니까? Control.CreateGraphics 방법에

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace PlotIt 
{ 
    public partial class Form1 : Form 
    { 
     public static List<TheList> GraphPoints = new List<TheList>(); 

     //Define the drawing area 
     private Rectangle PlotArea; 
     //Unit defined in world coordinate system: 
     private float xMin = 0f; 
     private float xMax = 10f; 
     private float yMin = 0f; 
     private float yMax = 10f; 
     //Define the offset in pixel: 
     private int offset = 150; 
     Graphics g; 

     Boolean buttonPressed = false; 
     public Form1() 
     { 
      InitializeComponent(); 
      this.SetStyle(ControlStyles.ResizeRedraw, true); 
      this.BackColor = Color.White; 

     } 

     protected override void OnPaint(PaintEventArgs e) 
     { 
      g = e.Graphics; 

      //Calculate the location and size of the drawing area 
      //within which we want to draw the graphics: 
      Rectangle rect = ClientRectangle; 

      PlotArea = new Rectangle(rect.Location, rect.Size); 
      PlotArea.Inflate(-offset, -offset); 

      g.DrawRectangle(Pens.Black, PlotArea); 

      Pen aPen = new Pen(Color.Green, 3); 
      g.DrawLine(aPen, Point2D(new PointF(5, 0)), Point2D(new PointF(5, 10))); 
      g.DrawLine(aPen, Point2D(new PointF(0, 5)), Point2D(new PointF(10, 5))); 

      aPen.Dispose(); 
      g.Dispose(); 


     } 



     private PointF Point2D(PointF ptf) 
     { 
      PointF aPoint = new PointF(); 

      aPoint.X = PlotArea.X + (ptf.X - xMin) * PlotArea.Width/(xMax - xMin); 

      aPoint.Y = PlotArea.Bottom - (ptf.Y - yMin) * PlotArea.Height/(yMax - yMin); 

      return aPoint; 
     } 



     private void btnPlotGraph_Click(object sender, EventArgs e) 
     { 



      g.DrawString("♫", new Font("Calibri", 12), new SolidBrush(Color.HotPink), (PlotArea.X + (7 - xMin)* PlotArea.Width/(xMax - xMin)), (PlotArea.Bottom - (6 - yMin) * PlotArea.Height/(yMax - yMin))); 
     } 



    } 

} 

답변

2

봐를 다음과 같이

내 코드입니다. 그러면 필요한 Graphic 개체를 얻을 수 있습니다.

Graphics g = this.CreateGraphics(); 
g.DrawString("♫", new Font("Calibri", 12), new SolidBrush(Color.HotPink), (PlotArea.X + (7 - xMin)* PlotArea.Width/(xMax - xMin)), (PlotArea.Bottom - (6 - yMin) * PlotArea.Height/(yMax - yMin))); 
g.Dispose(); 
+0

흠 ... 이건 매개 변수를 전달하려고하는 것입니다.하지만 생각합니다. 클릭 이벤트가 PaintEventArgs (오히려 EventArgs)에서 전달되지 않기 때문에 작동하지 않을 것이라고 생각합니다 .... 감사합니다. – BigBug

+0

구체적으로 말하고있는 매개 변수는 무엇입니까? PaintEventArg에서 유일하게 사용하는 모양은 Form의 Graphics 객체입니다. –

+0

맞습니다. 그러나 "btnPlotGraph_Click"메서드를 보면 "PaintEventArgs"매개 변수를 사용하지 않습니다. btnPlotGraph_Click은 WinForms에 의해 생성 된 메서드입니다 (폼에 단추를 놓을 때 - PaintEventArgs 매개 변수를 전달하는 옵션이 제공되지 않음). – BigBug

2

더 적절한 방법이 있습니다.

Click 이벤트에는 좌표를 저장하고 this.Invalidate()을 호출해야합니다.

양식이 다시 그려져 Paint 이벤트가 발생합니다.

그래픽 개체를 수동으로 만들 수도 있지만 양식을 새로 고치려면 Invalidate을 호출하여 양식을 요청하는 것이 좋습니다.

+0

감사합니다. 이것은 상당히 도움이되었습니다. – BigBug

관련 문제