Home

GDI+ Example: Continually Moving Lines

     

Introduction

This GDI+ example illustrates two perpendicular lines that are drawn on a form. To make it interesting (instead of two simple lines), the intersection of the lines keep moving, and consequently, the lines.

Moving Lines

ApplicationApplication: Creating the Application

  1. Start Microsoft Visual Studio or Microsoft Visual C# Express
  2. To create a new applicaiton, on the main menu, click File -> New Project...
  3. Select Windows Forms Application
  4. Change the name to ContinuallyMovingLines
  5. Click OK
  6. In the Solution Explorer, right-click Form1.cs and click Rename
  7. Type Exercise.cs and press Enter
  8. Double-click the body of the form and change the document as follows:
    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 ContinuallyMovingLines
    {
        public partial class Exercise : Form
        {
            static int x;
            static int y;
            static bool IsMovingDown;
            static bool IsMovingRight;
    
            public Exercise()
            {
                InitializeComponent();
            }
    
            private void Exercise_Load(object sender, EventArgs e)
            {
                x = 0;
                y = 0;
                IsMovingRight = false;
                IsMovingDown = false;
                
                BackColor = Color.Black;
                ClientSize = new System.Drawing.Size(745, 532);
            }
        }
    }
  9. Return to the form and click it to select it
  10. In the Properties window, click Events
  11. In the Events section, double-click Paint
  12. Implement the event as follows:
    private void Exercise_Paint(object sender, PaintEventArgs e)
    {
        // If the status of the origin indicates that it is moving right,
        // then increase the horizontal axis
        if (IsMovingRight == true)
            x++;
        else // If it's moving left, then decrease the horizontal movement
            x--;
    
        // If the status of the origin indicates that it is moving down,
        // then increase the vertical axis
        if (IsMovingDown == true)
            y++;
        else // Otherwise, decrease it
            y--;
    
        // Collision: if the axis hits the right side of the screen,
        // then set the horizontal moving status to "Right", which will be used
        // by the above code
        if ((x + 40) > ClientSize.Width)
            IsMovingRight = false;
        if (x < 0)
            IsMovingRight = true; ;
    
        if ((y + 40) > ClientSize.Height)
            IsMovingDown = false;
        if (y < 0)
            IsMovingDown = true;
    
        // Draw the new axis
        e.Graphics.DrawLine(Pens.Aqua, x + 20, 0, x + 20, ClientSize.Height);
        e.Graphics.DrawLine(Pens.Aqua, 0, y + 20, ClientSize.Width, y + 20);
    }
  13. Return to the form
  14. In the Components section of the Ribbon, click Timer
  15. Click the form
  16. In the Properties window, change the timer's characteristics as follows:
    (Name): tmrMoveLines
    Enabled: True
    Interval: 20
  17. Click the form
  18. In the Properties window, click Events
  19. In the Events section, double-click Paint
  20. Implement the event as follows:
    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 ContinuallyMovingLines
    {
        public partial class Exercise : Form
        {
            static int x;
            static int y;
            static bool IsMovingDown;
            static bool IsMovingRight;
    
            public Exercise()
            {
                InitializeComponent();
            }
    
            private void Exercise_Load(object sender, EventArgs e)
            {
                x = 0;
                y = 0;
                IsMovingRight = false;
                IsMovingDown = false;
    
                BackColor = Color.Black;
                ClientSize = new System.Drawing.Size(745, 532);
            }
    
            private void Exercise_Paint(object sender, PaintEventArgs e)
            {
                // If the status of the origin indicates that it is moving right,
                // then increase the horizontal axis
                if (IsMovingRight == true)
                    x++;
                else // If it's moving left, then decrease the horizontal movement
                    x--;
    
                // If the status of the origin indicates that it is moving down,
                // then increase the vertical axis
                if (IsMovingDown == true)
                    y++;
                else // Otherwise, decrease it
                    y--;
    
                // Collision: if the axis hits the right side of the screen,
                // then set the horizontal moving status to "Right", which will be used
                // by the above code
                if ((x + 40) > ClientSize.Width)
                    IsMovingRight = false;
                if (x < 0)
                    IsMovingRight = true; ;
    
                if ((y + 40) > ClientSize.Height)
                    IsMovingDown = false;
                if (y < 0)
                    IsMovingDown = true;
    
                // Draw the new axis
                e.Graphics.DrawLine(Pens.Aqua, x + 20, 0, x + 20, ClientSize.Height);
                e.Graphics.DrawLine(Pens.Aqua, 0, y + 20, ClientSize.Width, y + 20);
            }
    
            private void tmrMoveLines_Tick(object sender, EventArgs e)
            {
                Invalidate();
            }
        }
    }
  21. Press F5 to execute
     
    Moving Lines
  22. Close the form
 

Home Copyright © 2010-2014, FunctionX