Einzelnen Beitrag anzeigen

Benutzerbild von nailor
nailor

Registriert seit: 12. Dez 2002
Ort: Karlsruhe
1.989 Beiträge
 
#1

c# - Paint Event

  Alt 24. Mai 2004, 19:21
Code:
using System;
using System.Drawing;
using System.Windows.Forms;

namespace debug
{
   public class WinForm : System.Windows.Forms.Form
   {
      private System.ComponentModel.Container components = null;
      private System.Windows.Forms.PictureBox pictureBox1;

      public WinForm()
      {
         InitializeComponent();
      }

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

      private void InitializeComponent()
      {
         this.pictureBox1 = new System.Windows.Forms.PictureBox();
         this.SuspendLayout();
         // 
         // pictureBox1
         // 
         this.pictureBox1.Location = new System.Drawing.Point(16, 16);
         this.pictureBox1.Name = "pictureBox1";
         this.pictureBox1.Size = new System.Drawing.Size(264, 240);
         this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
         // 
         // WinForm
         // 
         this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
         this.ClientSize = new System.Drawing.Size(292, 266);
         this.Controls.Add(this.pictureBox1);
         this.Name = "WinForm";
         this.ResumeLayout(false);
      }

      [STAThread]
      static void Main()
      {
         Application.Run(new WinForm());
      }

      private int test = 0;

      private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
      {
         test++;

         const int size       = 25;
         const int boarder = 10;
         const int gap       = 3;

         System.Drawing.SolidBrush wB = new System.Drawing.SolidBrush(Color.White);
         System.Drawing.SolidBrush bB = new System.Drawing.SolidBrush(Color.Black);

         System.Drawing.SolidBrush aB;
         System.Drawing.Rectangle r = new System.Drawing.Rectangle(0,0,size-gap,size-gap);

         for (int i = 0; i < 5; i++)
            for (int j = 0; j < 5; j++)
            {
               r.X = i * size + boarder;
               r.Y = j * size + boarder;

               if ((i+j) % 2 == 0)
                  aB = wB;
               else
                  aB = bB;

               this.pictureBox1.CreateGraphics().FillRectangle(aB, r);
            }

         this.Text = test.ToString();
      }
   }
}
waaaaarum klappt das, wenn man ein anderes fenster davor "langschiebt", aber nicht, wenn man das fenster minimiert und wiederherstellt, und auch nicht, wenn man das andere fenster, das meins verdeckt, minimiert? die caption des fensters beweist ja, dass der code eigentlich ausgeführt wird.
kann mir jemand sagen, wie ich den code hier funktionierend bekomme, oder wie ich machen kann, das ein gezeichnetes bild nicht "verloren" geht?

nailor
Michael N.
http://nailor.devzero.de/code/sharpmath/testing/ --- Tests, Feedback, Anregungen, ... aller Art sehr willkommen!
::: don't try so hard - it'll happen for a reason :::
  Mit Zitat antworten Zitat