Manually Placing the Icon on the Desktop

 

Project Spiral

The timer created in project Clock is the heart of the clock program because it forces event handler timer1_Tick to actuate 10 times per second and find the current time in DateTime.Now. This same timer device can perform elapsed time computations to trigger other events.

Project Spiral emits a tick every second and causes the spiral on the main window display to increase in size by 10 pixels each time. The complete project is located at Visual Studio Projects\DemosSourceCode\Spiral. Place Spiral in the IDE, compile it, and run it.

This is its main window:

image from book
Figure 21-2: Spiral project

A listing of the software that generates the spiral timer is shown at the end of this section. This is how the project is created in the IDE:

  1. A new project named Spiral is formed in the IDE and the main window template is set to a size of 400 x 400 . The Start Position property is set to Center Screen .

  2. This window requires painting, so, while the window template is highlighted, the programmer must move to the Properties window, click on the Events icon, and click on the Appearance | Paint entry to produce an event handler named Form1_Paint in the Form1.cs source code.

  3. A Quit button is placed at the lower-right corner of the window. Its event handler is created by double-clicking on the button, and the statement Close() is placed in that event handler.

  4. With the main window highlighted (and the main window properties present in the Properties window), the Toolbox is selected and the Timer icon is clicked with the mouse. Then the mouse pointer is moved to the window template and clicked there. This produces a timer1 rectangle at the bottom of the IDE (showing that the timer is now a part of this project). The timer rectangle is highlighted so its properties will appear in the Properties window. All you have to do to create its default event handler is double-click on the timer1 rectangle at the bottom of the IDE screen.

This completes the window template work.

This is how Spiral works:

  1. A timer is placed in the declaration of Form1 (in lines SP019-SP022). Line SP020 ties the timer (which is created in SP019) to event handler timer1_Tick. Line SP021 sets the timer interval at 1000, which means that the event handler timer1_tick will actuate once each second.

  2. Line SP022 starts the timer. This timer is turned off only when the project is exited.

  3. Once the timer starts (and calls event handler timer1_Tick once each second) the event handler (lines SP050-SP058) updates intx and inty, which are used in line SP036. The statement Invalidate(), line SP057, forces the system to compute new data in event handler Form1_paint, and the operating system repaints.

  4. Inside event handler Form1_Paint these events occur:

    1. Line SP034 sets the pen color , which is randomly set.

    2. Lines SP036-SP045 paint the spiral onto the window.

    3. Button1, line SP060, shuts down the program.

Spiral (SP) Listing

 Form1.cs: SP002:        using System; SP003:        using System.Collections.Generic; SP004:        using System.ComponentModel; SP005:        using System.Data; SP006:        using System.Drawing; SP007:        using System.Windows.Forms; SP009:        namespace Spiral SP010:        { SP011:          partial class Form1 : Form SP012:          { SP013:            public const int intRevs = 10; SP014:            public int intx = 10; SP015:            public int inty = 10; SP016:            public DateTime dt, dtNow; SP017:            public Form1() SP018:            { SP019:              Timer timer = new Timer(); SP020:              timer.Tick += new EventHandler(timer1_Tick); SP021:              timer.Interval = 1000; // 1 sample per second. SP022:              timer.Start(); SP023:              InitializeComponent(); SP024:            } //-----------------------------------------------------------------------------------------// SP030:            private void Form1_Paint(object sender, PaintEventArgs e) SP031:            { SP032:              Graphics grfx = e.Graphics; SP033:              Random rand = new Random(); SP034:              Pen myPen = new Pen(Color.FromArgb(rand.Next(256), rand.Next(256),                                                        rand.Next(256))); SP035:              float fltAngle, fltScale; SP036:              int intPoints = intRevs*2*(intx + inty); SP037:              PointF[] aptf = new PointF[intPoints]; SP038:              for (int jj = 0; jj < intPoints; jj++) SP039:              { SP040:                fltAngle = (float)(jj*2*Math.PI / (intPoints / intRevs)); SP041:                fltScale=1-(float)jj / intPoints; SP042:                aptf[jj].X = (float)(intx/2*(1+fltScale * Math.Cos(fltAngle))); SP043:                aptf[jj].Y = (float)(inty/2*(1+fltScale * Math.Sin(fltAngle))); SP044:              } SP045:              grfx.DrawLines(myPen, aptf); SP046:            } //-----------------------------------------------------------------------------------------// SP050:            private void timer1_Tick(object sender, EventArgs e) SP051:            { // Get current time. SP052:              dtNow = DateTime.Now; SP053:              dtNow = new DateTime(dtNow.Year, dtNow.Month, dtNow.Day, dtNow.Hour,                                          dtNow.Minute, dtNow.Second); SP054:              if(dtNow != dt) dt = dtNow; SP055:              intx = intx + 10; SP056:              inty = inty + 10; SP057:              Invalidate(); SP058:            } //-----------------------------------------------------------------------------------------// SP059:            private void button1_Click(object sender, EventArgs e) SP060:            { // Quit. SP061:              Close(); SP062:            } SP063:          } SP064:        } //=========================================================================================// Form1.Designer.cs: SP100:        namespace Spiral SP101:        { SP102:          partial class Form1 SP103:          {                   // Required designer variable. SP104:            private System.ComponentModel.IContainer components = null;                   // Clean up any resources being used. SP105:            protected override void Dispose(bool disposing) SP106:            { SP107:              if (disposing && (components != null)) components.Dispose(); SP108:              base.Dispose(disposing); SP109:            } SP110:            #region Windows Form Designer generated code                   // Required method for Designer support. SP111:            private void InitializeComponent() SP112:            { SP113:              this.components = new System.ComponentModel.Container(); SP114:              this.timer1 = new System.Windows.Forms.Timer(this.components); SP115:              this.button1 = new System.Windows.Forms.Button(); SP116:              this.SuspendLayout();                     //                     // timer1                     // SP117:              this.timer1.Tick += new System.EventHandler(this.timer1_Tick);                     //                     // button1                     // SP118:              this.button1.Location = new System.Drawing.Point(296, 296); SP119:              this.button1.Name = "button1"; SP120:              this.button1.TabIndex = 0; SP121:              this.button1.Text = "Quit"; SP122:              this.button1.Click += new System.EventHandler(this.button1_Click);                     //                     // Form1                     // SP123:              this.AutoScaleDimensions = new System.Drawing.SizeF(5F, 13F); SP124:              this.ClientSize = new System.Drawing.Size(384, 332); SP125:              this.Controls.Add(this.button1); SP126:              this.Name = "Form1"; SP127:              this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; SP128:              this.Text = "Spiral"; SP129:              this.Paint += new System.Windows.Forms.PaintEventHandler                                   (this.Form1_Paint); SP130:              this.ResumeLayout(false); SP131:            } SP132:            #endregion SP133:            private System.Windows.Forms.Timer timer1; SP134:            private System.Windows.Forms.Button button1; SP135:          } SP136:        } 
 


Unlocking Microsoft C# V 2.0 Programming Secrets
Unlocking Microsoft C# V 2.0 Programming Secrets (Wordware Applications Library)
ISBN: 1556220979
EAN: 2147483647
Year: 2005
Pages: 129

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net