2017-05-30 3 views
-2

Heute habe ich Visual Studio + WinForms ausprobiert (ich habe Xamarin verwendet) Ich verwende keinen Designer, einfach leere Projekt erstellen und benötigte Bibliotheken verknüpfen. Also, mein Timer funktioniert nicht richtig, es tickt nur in minimierter Form.Timer tickt nicht

App.cs

using System; 
using System.Windows.Forms; 

namespace Line 
{ 
    class App 
    { 
     [STAThread] 
     public static void Main() 
     { 
      Application.Run(new Window()); 
     } 
    } 
} 

Window.cs

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

namespace Line 
{ 
    class Window : Form 
    { 
     Timer timer; 
     PictureBox pictureBox; 
     Bitmap bmp; 

     public struct Circle 
     { 
      public PointF position; 
      public SizeF size; 
     }; 

     List<Circle> circles; 

     public Window() 
     { 
      this.Text = "Line"; 
      this.Size = SizeFromClientSize(new Size(640, 480)); 
      pictureBox = new PictureBox(); 
      pictureBox.Dock = DockStyle.Fill; 
      pictureBox.BackColor = Color.White; 
      pictureBox.Paint += new PaintEventHandler(pictureBox_Paint); 
      this.Controls.Add(pictureBox); 
      this.Load += new EventHandler(Window_Load); 
      this.Resize += new EventHandler(Window_Resize); 
      circles = new List<Circle>(); 
      timer = new Timer(); 
      timer.Interval = 15; 
      timer.Enabled = true; 
      timer.Tick += new EventHandler(timer_Tick); 
     } 

     private void pictureBox_Paint(object sender, PaintEventArgs e) 
     { 
      Graphics g = Graphics.FromImage(bmp); 
      g.Clear(Color.Black); 
      foreach (Circle c in circles) 
      { 
       g.DrawEllipse(Pens.White, new RectangleF(c.position, c.size)); 
      } 
      pictureBox.Image = bmp; 
     } 

     private void timer_Tick(object sender, EventArgs e) 
     { 
      Circle c; 
      Console.WriteLine("Works"); 
      for (int i = 0; i < circles.Count; i++) 
      { 
       c = circles[i]; 
       c.size.Width = (c.size.Width > 200) ? 100 : c.size.Width + 1; 
       circles[i] = c; 
      } 
      pictureBox.Invalidate(); 
     } 

     private void Window_Load(object sender, EventArgs e) 
     { 
      bmp = new Bitmap(this.Width, this.Height); 
      circles.Add(new Circle()); 
      Circle c = circles[0]; 
      c.position = new PointF(100, 100); 
      c.size = new SizeF(100, 100); 
      circles[0] = c; 
     } 

     private void Window_Resize(object sender, EventArgs e) 
     { 
      bmp = new Bitmap(this.Width, this.Height); 
      pictureBox.Invalidate(); 
     } 
    } 
} 

Same Code arbeitete in Xamarin in Ordnung.

+0

Warum würden Sie nicht den Designer verwenden? Es wird eine ganze partielle Klasse erstellt, die nicht selbst geschrieben werden soll. – krillgar

+2

Sie missbrauchen "Paint" vollständig. Zeichnen Sie auf 'e.Graphics'; Verwenden Sie keine 'PictureBox' oder' Image' oder 'Bitmap'. – SLaks

+0

Ich benutze Bitmap, weil PictureBox Double Buffer unterstützt, und wenn ich nur Graphics verwende, dann wird es "flicks" geben. – IOD

Antwort

1

1) in timer_Tick entfernen Linie: pictureBox.Invalidate();
2) cut-Code von pictureBox_Paint und fügen Sie am Ende der timer_Tick
3) fix Variablennamen Kollision c