Sybatron

TryForMovableWinForm

Jun 19th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.69 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Drawing;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using System.Windows.Forms;
  7.  
  8.  
  9. namespace WindowsFormsApplication1
  10. {
  11.     public partial class Form1 : Form
  12.     {
  13.  
  14.         int freq = 100;
  15.         private Thread waiter = null;
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.  
  21.         private async void Form1_Paint(object sender, PaintEventArgs e)
  22.         {
  23.             await Draw(e);
  24.         }
  25.  
  26.         private async Task Draw(PaintEventArgs old)
  27.         {
  28.  
  29.             var task = Task.Factory.StartNew(() =>
  30.             {
  31.                 using (Graphics g = CreateGraphics())
  32.                 {
  33.                     var e = new PaintEventArgs(g, old.ClipRectangle);
  34.  
  35.                     try
  36.                     {
  37.                         Rectangle rect = new Rectangle(50, 50, 100, 50);
  38.                         Brush brush = new SolidBrush(Color.Black);
  39.                         for (int i = 0; i < 5; i++)
  40.                         {
  41.                             g.Clear(this.BackColor);
  42.                             g.FillRectangle(brush, rect);
  43.                             Thread.Sleep(freq);
  44.                             rect.X += 10;
  45.                             if (rect.X >= ClientSize.Width)
  46.                             {
  47.                                 rect.X = 0;
  48.                             }
  49.  
  50.                         }
  51.                         base.OnPaint(e);
  52.  
  53.                     }
  54.                     catch (Exception ex)
  55.                     {
  56.                         Debug.WriteLine(ex.Message);
  57.                     }
  58.                 }
  59.             });
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment