2011-05-02 8 views
1

직사각형 목록을 만들고 PictureBox에 그립니다. 코드의 다른 곳에서 목록에서 일부 직사각형을 제거하지만, PictureBox.Refresh()을 호출하면 이전 결과 인 모든 직사각형이 표시됩니다.C에서 PictureBox 새로 고침

그림의 복제본을 만들고 모든 직사각형을 하나씩 다시 칠해 보았지만 동일한 문제가 있습니다.

제발, 현재 직사각형 목록을 그리는 방법에 대한 아이디어를 좀주세요.

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

답변

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

정말 내 문제를 이해합니다. 이것이 내가 원하는 해결책이다. 고맙습니다 –

관련 문제