2011-11-01 67 views
1

我有繪製圖形的代碼,如果用戶嘗試調整窗體大小(通過拖動窗體的邊角)就會縮放。該表單將在文本框中獲得x和y座標。當按下按鈕時,我想繪製他們指出的點。但是,Click事件包含參數Object Sender和EventArgs e。 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方法中我可以繪製座標?

我的代碼如下:

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

查找到Control.CreateGraphics方法。這應該讓你得到你需要的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的圖形對象。 –

+0

這是正確的。但是,如果您查看「btnPlotGraph_Click」方法,則不會使用參數「PaintEventArgs」。 btnPlotGraph_Click是一個由WinForms生成的方法(當你在窗體上放置一個按鈕時 - 它沒有提供傳遞參數PaintEventArgs的選項)。 – BigBug

2

有一個更合適的方法來做到這一點。

在您的Click事件中,您應該存儲座標,然後致電this.Invalidate()

這將導致您的表單重繪自己,觸發Paint事件。

也可以手動創建圖形對象,但最好通過調用Invalidate來讓表單自行更新。

+0

謝謝。這很有幫助 – BigBug