GDI+ Example: Continually Moving Lines
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…
3 min read663 words
Introduction#
This GDI+ tutorial demonstrates how to draw two lines that cross perpendicularly on a form. Rather than being static, the intersection point moves constantly, which creates the effect of moving lines.
Step-by-Step: Building the Application
- Start Microsoft Visual Studio or Microsoft Visual C# Express
- To create a new applicaiton, on the main menu, click File -> New Project...
- Select Windows Forms Application
- Change the name to ContinuallyMovingLines
- Click OK
- In the Solution Explorer, right-click Form1.cs and click Rename
- Type Exercise.cs and press Enter
- 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); } } }
- Return to the form and click it to select it
- In the Properties window, click Events
- In the Events section, double-click Paint
- 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); }
- Return to the form
- In the Components section of the Ribbon, click Timer
- Click the form
- In the Properties window, change the timer's characteristics as follows: (Name): tmrMoveLines Enabled: True Interval: 20
- Click the form
- In the Properties window, click Events
- In the Events section, double-click Paint
- 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(); } } }
- Press F5 to execute
- Close the form
Home Copyright © 2010-2011 C# Key

