944,120 Members | Top Members by Rank

Ad:
  • C# Code Snippet
  • Views: 2168
  • C# RSS
5

Chasing lights, running ants.

by on Oct 19th, 2009
Ever wanted to know how to implement running lights? well here is your chance to find out.
Open a new Forms application enlarge the Form a bit and drop a Panel and a Timer on it. Add the class ChasingLights to the solution, see code. Implement a form Load, panel Paint and Timer Tick event. See code. I have included the result you should get(without the "lights" running ) Also added a few fancy tricks you can do with strings. Have fun!
Attached Thumbnails
Click image for larger version

Name:	Marquee.jpg
Views:	310
Size:	24.2 KB
ID:	12194  
C# Code Snippet (Toggle Plain Text)
  1. using System.Collections.Generic;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4.  
  5. // October 2009 Danny Marivoet
  6. // This class can be used anyway you want
  7. // If you misuse it, my name will be Manuel, from Barzelona, mee no noothing!
  8. namespace Marquee
  9. {
  10. class ChasingLights
  11. {
  12. public enum LightShape
  13. {
  14. Square, Round
  15. }
  16.  
  17. private struct LampState
  18. {
  19. public Point Pos;
  20. public bool On;
  21. }
  22.  
  23. private List<LampState> Lamps = new List<LampState>();
  24. private LampState bulb = new LampState();
  25. private bool reverse = false;
  26.  
  27. public ChasingLights()
  28. {
  29. LampSize = new Size(8, 8);
  30. DisplayRect = new Rectangle(0, 0, 64, 64);
  31. LampOnColor = Color.Yellow;
  32. LampOffColor = Color.Red;
  33. LampShape = LightShape.Round;
  34. bulb.On = false;
  35. }
  36.  
  37. public Size LampSize { get; set; }
  38. public Rectangle DisplayRect { get; set; }
  39. public Color LampOnColor { get; set; }
  40. public Color LampOffColor { get; set; }
  41. public LightShape LampShape { get; set; }
  42.  
  43. public void Draw(Graphics G)
  44. {
  45. G.SmoothingMode = SmoothingMode.AntiAlias;
  46. reverse = !reverse;
  47. DrawLightFrame(G, reverse);
  48. DrawText(G);
  49. }
  50.  
  51. public void MakeLightFrame(int rows, int cols)
  52. {
  53. bulb.On = false;
  54. bulb.Pos.Y = 0;
  55. for (int c = 0; c < cols; c++)
  56. {
  57. bulb.Pos.X = c * LampSize.Width;
  58. bulb.On = !bulb.On;
  59. Lamps.Add(bulb);
  60. }
  61. for (int r = 1; r < rows; r++)
  62. {
  63. bulb.Pos.Y = r * LampSize.Height;
  64. bulb.On = !bulb.On;
  65. Lamps.Add(bulb);
  66. }
  67. for (int c = cols - 2; c > 0; c--)
  68. {
  69. bulb.Pos.X = c * LampSize.Width;
  70. bulb.On = !bulb.On;
  71. Lamps.Add(bulb);
  72. }
  73. bulb.Pos.X = 0;
  74. for (int r = rows - 1; r > 0; r--)
  75. {
  76. bulb.Pos.Y = r * LampSize.Height;
  77. bulb.On = !bulb.On;
  78. Lamps.Add(bulb);
  79. }
  80. }
  81.  
  82. private void DrawLightFrame(Graphics G, bool reverse)
  83. {
  84. foreach (LampState item in Lamps)
  85. {
  86. SolidBrush B = new SolidBrush(item.On == reverse ? LampOnColor : LampOffColor);
  87. Rectangle R = new Rectangle(item.Pos, LampSize);
  88. switch (LampShape)
  89. {
  90. case LightShape.Square:
  91. G.FillRectangle(B, R);
  92. break;
  93. case LightShape.Round:
  94. G.FillEllipse(B, R);
  95. break;
  96. default:
  97. break;
  98. }
  99. }
  100. }
  101.  
  102. private void DrawText(Graphics G)
  103. {
  104. string strText = "Scott";
  105. Font font = new Font("Times New Roman", 72);
  106. Brush hB = new HatchBrush(HatchStyle.HorizontalBrick, Color.White, Color.Blue);
  107. G.DrawString(strText, font, hB, 50, 10);
  108. strText = "The C# coder!";
  109. Font Af = new Font("Arial Black", 24);
  110. GraphicsPath path = new GraphicsPath();
  111. // I know: all the numbers that follow is NOT good coding practice
  112. path.AddString(strText, Af.FontFamily, (int)Af.Style, 36, new Point(30, 150), new StringFormat());
  113. PointF[] ptDest = { new PointF(60, 100), new PointF(280 , 100),
  114. new PointF(20, 200), new PointF(330, 200) };
  115. RectangleF Rbnds = path.GetBounds();
  116. path.Warp(ptDest, Rbnds);
  117. G.FillPath(new SolidBrush(Color.Blue), path);
  118. }
  119.  
  120. }
  121. }
  122.  
  123. /////////////////////////////////////////////////////////////////////////////////////////
  124.  
  125. using System;
  126. using System.Windows.Forms;
  127.  
  128. namespace Marquee
  129. {
  130. public partial class Form1 : Form
  131. {
  132. private ChasingLights Marquee = new ChasingLights();
  133.  
  134. public Form1()
  135. {
  136. InitializeComponent();
  137. }
  138.  
  139. private void panel1_Paint(object sender, PaintEventArgs e)
  140. {
  141. Marquee.Draw(e.Graphics);
  142. }
  143.  
  144. private void Form1_Load(object sender, EventArgs e)
  145. {
  146. panel1.Size = new System.Drawing.Size(360, 220);
  147. Marquee.DisplayRect = panel1.ClientRectangle;
  148. Marquee.LampShape = ChasingLights.LightShape.Round;
  149. Marquee.LampSize = new System.Drawing.Size(10, 10);
  150. int Ncols = Marquee.DisplayRect.Width / Marquee.LampSize.Width;
  151. int Nrows = Marquee.DisplayRect.Height / Marquee.LampSize.Height;
  152. Marquee.MakeLightFrame(Nrows, Ncols);
  153. timer1.Start();
  154. timer1.Interval = 500;
  155. }
  156.  
  157. private void timer1_Tick(object sender, EventArgs e)
  158. {
  159. this.Refresh();
  160. }
  161. }
  162. }
Comments on this Code Snippet
Apr 11th, 2011
0

Re: Chasing lights, running ants.

Excellent Boss, its executing with Lighting Effects.
Junior Poster in Training
Saikalyankumar is offline Offline
91 posts
since Mar 2011
Message:
Previous Thread in C# Forum Timeline: C# window form TreeView problem.
Next Thread in C# Forum Timeline: TryParse vs Exception





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC