2011-05-02 35 views
1

我創建了一個矩形列表並在PictureBox中繪製它們。在代碼的其他地方,我們從列表中刪除了一些矩形,但當我調用PictureBox.Refresh()時,它會顯示以前的結果:所有的矩形。刷新C中的圖片框#

我試圖創建一個圖片的克隆,並重新繪製所有的矩形逐一,但它有同樣的問題。

請問,你能給我一些關於如何繪製當前矩形列表的想法。

Rectangle r = lanes[i];//lanes is list of rectangles 
Pen pen = new Pen(Color.Red, 2); 
Graphics g = pictureBox1.CreateGraphics(); 
g.DrawRectangle(pen, r); 
+0

kalhari,我們需要查看更多的代碼才能準確診斷問題。如果你仍然陷於困境,考慮準備一個[SSCCE](http://sscce.org/)。話雖如此,但我們已經被告知,我懷疑喬納森伍德是正確的,你可能只需要「無效」控制......「底層」,這要求.NET的GUI處理程序「請刷新是屏幕的區域「。明確? – corlettk 2011-05-02 04:56:15

回答

1

您沒有提供足夠的信息,我不知道是什麼「它沒有給出正確的」手段。

上面繪製矩形的代碼應該放在控件的Paint事件處理函數中。並且,當您想要重新繪製圖片框時,請調用Invalidate方法(您可能還需要調用Update方法)。

1

出於興趣,我實現了這個目標,或多或少,因爲我認爲它應該完成。

這是我的代碼。

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 WindowsFormsApplication1 
{ 
    public class Form1 : Form 
    { 
     private static List<Rectangle> rectangles = new List<Rectangle> { 
      //   x,y,w,h 
      new Rectangle(0,0,10,10), 
      new Rectangle(10,10,10,10), 
      new Rectangle(10,40,10,10), 
      new Rectangle(60,20,10,10), 
      new Rectangle(90,10,10,10), 
     }; 
     private Label label1; 

     private RectanglePictureBox rectPicBox1; 

     public Form1() { 
      InitializeComponent(); 
      this.rectPicBox1.Rectangles = rectangles; 
     } 

     private void rectPicBox1_Click(object sender, EventArgs e) { 
      if (rectangles.Count <= 0) { 
       Console.Beep(); // nothing left to remove! 
      } else { 
       rectangles.RemoveAt(rectangles.Count - 1); 
       rectPicBox1.Rectangles = rectangles; 
      } 
     } 

     #region InitializeComponent (Modified Manually) 

     /// <summary> 
     /// Required method for Designer support - do not modify 
     /// the contents of this method with the code editor. 
     /// </summary> 
     private void InitializeComponent() { 
      this.rectPicBox1 = new WindowsFormsApplication1.RectanglePictureBox(); 
      this.label1 = new System.Windows.Forms.Label(); 
      ((System.ComponentModel.ISupportInitialize)(this.rectPicBox1)).BeginInit(); 
      this.SuspendLayout(); 
      // 
      // rectPicBox1 
      // 
      this.rectPicBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
         | System.Windows.Forms.AnchorStyles.Left) 
         | System.Windows.Forms.AnchorStyles.Right))); 
      this.rectPicBox1.BackColor = System.Drawing.SystemColors.ControlLightLight; 
      this.rectPicBox1.Location = new System.Drawing.Point(1, 1); 
      this.rectPicBox1.Name = "rectPicBox1"; 
      this.rectPicBox1.Size = new System.Drawing.Size(257, 131); 
      this.rectPicBox1.TabIndex = 0; 
      this.rectPicBox1.TabStop = false; 
      this.rectPicBox1.Click += new System.EventHandler(this.rectPicBox1_Click); 
      // 
      // label1 
      // 
      this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
         | System.Windows.Forms.AnchorStyles.Right))); 
      this.label1.AutoSize = true; 
      this.label1.Location = new System.Drawing.Point(2, 138); 
      this.label1.Name = "label1"; 
      this.label1.Size = new System.Drawing.Size(254, 13); 
      this.label1.TabIndex = 1; 
      this.label1.Text = "Clicking on the picture to removes the last rectangle."; 
      // 
      // Form1 
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
      this.ClientSize = new System.Drawing.Size(259, 156); 
      this.Controls.Add(this.label1); 
      this.Controls.Add(this.rectPicBox1); 
      this.Name = "Form1"; 
      this.Text = "Rectangles"; 
      ((System.ComponentModel.ISupportInitialize)(this.rectPicBox1)).EndInit(); 
      this.ResumeLayout(false); 
      this.PerformLayout(); 

     } 

     #endregion 

     #region Component Model 

     private System.ComponentModel.IContainer components = null; 

     protected override void Dispose(bool disposing) { 
      if (disposing && (components != null)) { 
       components.Dispose(); 
      } 
      base.Dispose(disposing); 
     } 

     #endregion 


    } 

    //////////////////////////////////////////////////////////////////////////// 

    class RectanglePictureBox : PictureBox 
    { 
     public static Color[] _colors = { 
      Color.Red, Color.Green, Color.Blue, Color.Orange 
     }; 

     public List<Rectangle> Rectangles { 
      set { Image = ImageOf(value); } 
     } 

     private Bitmap ImageOf(List<Rectangle> rectangles) { 
      Bitmap result = new Bitmap(Size.Height, Size.Width); 
      Graphics graphics = Graphics.FromImage(result); 
      for (int i = 0; i < rectangles.Count; ++i) { 
       Brush brush = new SolidBrush(_colors[i % _colors.Length]); 
       graphics.FillRectangle(brush, rectangles[i]); 
      } 
      return result; 
     } 

    } 


} 

此代碼發佈時沒有任何擔保(明示或暗示)。全是你的。做任何你喜歡的事情。無論發生什麼,它都不是我的問題!

乾杯。基思。

+0

你真的明白我的問題。這是我想要的解決方案。謝謝 – 2011-08-06 08:24:42