| | |
Chasing lights, running ants.
Please support our C# advertiser: Intel Parallel Studio Home
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!
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!
using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; // October 2009 Danny Marivoet // This class can be used anyway you want // If you misuse it, my name will be Manuel, from Barzelona, mee no noothing! namespace Marquee { class ChasingLights { public enum LightShape { Square, Round } private struct LampState { public Point Pos; public bool On; } private List<LampState> Lamps = new List<LampState>(); private LampState bulb = new LampState(); private bool reverse = false; public ChasingLights() { LampSize = new Size(8, 8); DisplayRect = new Rectangle(0, 0, 64, 64); LampOnColor = Color.Yellow; LampOffColor = Color.Red; LampShape = LightShape.Round; bulb.On = false; } public Size LampSize { get; set; } public Rectangle DisplayRect { get; set; } public Color LampOnColor { get; set; } public Color LampOffColor { get; set; } public LightShape LampShape { get; set; } public void Draw(Graphics G) { G.SmoothingMode = SmoothingMode.AntiAlias; reverse = !reverse; DrawLightFrame(G, reverse); DrawText(G); } public void MakeLightFrame(int rows, int cols) { bulb.On = false; bulb.Pos.Y = 0; for (int c = 0; c < cols; c++) { bulb.Pos.X = c * LampSize.Width; bulb.On = !bulb.On; Lamps.Add(bulb); } for (int r = 1; r < rows; r++) { bulb.Pos.Y = r * LampSize.Height; bulb.On = !bulb.On; Lamps.Add(bulb); } for (int c = cols - 2; c > 0; c--) { bulb.Pos.X = c * LampSize.Width; bulb.On = !bulb.On; Lamps.Add(bulb); } bulb.Pos.X = 0; for (int r = rows - 1; r > 0; r--) { bulb.Pos.Y = r * LampSize.Height; bulb.On = !bulb.On; Lamps.Add(bulb); } } private void DrawLightFrame(Graphics G, bool reverse) { foreach (LampState item in Lamps) { SolidBrush B = new SolidBrush(item.On == reverse ? LampOnColor : LampOffColor); Rectangle R = new Rectangle(item.Pos, LampSize); switch (LampShape) { case LightShape.Square: G.FillRectangle(B, R); break; case LightShape.Round: G.FillEllipse(B, R); break; default: break; } } } private void DrawText(Graphics G) { string strText = "Scott"; Font font = new Font("Times New Roman", 72); Brush hB = new HatchBrush(HatchStyle.HorizontalBrick, Color.White, Color.Blue); G.DrawString(strText, font, hB, 50, 10); strText = "The C# coder!"; Font Af = new Font("Arial Black", 24); GraphicsPath path = new GraphicsPath(); // I know: all the numbers that follow is NOT good coding practice path.AddString(strText, Af.FontFamily, (int)Af.Style, 36, new Point(30, 150), new StringFormat()); PointF[] ptDest = { new PointF(60, 100), new PointF(280 , 100), new PointF(20, 200), new PointF(330, 200) }; RectangleF Rbnds = path.GetBounds(); path.Warp(ptDest, Rbnds); G.FillPath(new SolidBrush(Color.Blue), path); } } } ///////////////////////////////////////////////////////////////////////////////////////// using System; using System.Windows.Forms; namespace Marquee { public partial class Form1 : Form { private ChasingLights Marquee = new ChasingLights(); public Form1() { InitializeComponent(); } private void panel1_Paint(object sender, PaintEventArgs e) { Marquee.Draw(e.Graphics); } private void Form1_Load(object sender, EventArgs e) { panel1.Size = new System.Drawing.Size(360, 220); Marquee.DisplayRect = panel1.ClientRectangle; Marquee.LampShape = ChasingLights.LightShape.Round; Marquee.LampSize = new System.Drawing.Size(10, 10); int Ncols = Marquee.DisplayRect.Width / Marquee.LampSize.Width; int Nrows = Marquee.DisplayRect.Height / Marquee.LampSize.Height; Marquee.MakeLightFrame(Nrows, Ncols); timer1.Start(); timer1.Interval = 500; } private void timer1_Tick(object sender, EventArgs e) { this.Refresh(); } } }
Similar Threads
- laptop screen turned black but laptop still running (Monitors, Displays and Video Cards)
- (remote control over the WAN,to host in LAN,"server")network and hardware programing (Java)
- BIOS fails to start (Troubleshooting Dead Machines)
- assembly language (Assembly)
- Presario 2100 won't start, lights blink (Troubleshooting Dead Machines)
- Dead Notebook - Lights On, Nothing Happens (Cases, Fans and Power Supplies)
- Running too hot? (Cases, Fans and Power Supplies)
- Fan will not stop running after computer is shut down (Cases, Fans and Power Supplies)
- Dim Lights (Cases, Fans and Power Supplies)
| Thread Tools | Search this Thread |
Tag cloud for drawing, gdi+, string
2d access animation anime applicaions array art assembly assign binary bitmap brush bulletin button c# c++ calculator challenge char character color contorl convert count custom data date datetime drag drawing dynamic encryption file filename form format forms fstream ftp function game gdi+ geometry getline graphics gui hash ifstream input int java line list listbox loop mandelbrot math memory method microsoft mouse namevaluepairs parse parsing patch path pattern perl php picturebox plotting program python recursion regex region remove reverse rotation saving search security single site slicenotation sql string subscript superscript timer transform transparency tree unicode user usercontrol usercontrols validation vulnerability year




