The Timer component is also a Winform component, and the biggest difference between other WinForm components is that the Timer component is invisible, while most components are all visible and can be designed. The Timer component is also packaged in the namespace system.windows.form, which is the main function to trigger the same event while the Timer component is started. Timer components are a relatively common component in programming, although attributes, events are small, but in some places use it will have an ideaful effect. The procedure introduced in this article is a process that is fluttering with Visual C #, which uses a large number of Timer components. Let's take a look at the design and running the program. One. This article is designed and running software environment: (1). Microsoft Window 2000 Server Edition (2) .. Net Framework SDK Beta 2. The idea of programming and the solution to key steps: actually to make the form of the program float, actually thinking is relatively simple. The first is when the form is loaded, set the initial position of the display to the form. Then by the two Timer components defined in the form, one of which is called Timer1, the action is to control the form from left to right (of course, if you like, you can change it from top to float, or other floating The way.), Another Timer2 is the control form to float from right left (like you can also change to other fluttering). Of course, these two TIMER components cannot be started at the same time. In this program, it is set to set the Timer1 component. When this time is started, every 0.01 seconds, will give the form of the upper left corner of the form in the triggered event. The coordinates are plus "1". At this time, the result we see is that the form is constantly moving from left to right. When moving to a certain position, Timer1 stops. Timer2 starts, every 0.01 second, the horizontal coordinates of the upper left corner of the form in the trigger defined event, then the result we see is that the form is constantly moving from right from right. When moving to a certain position, Timer1 starts, Timer2 stops, so that the form is fluttering. To achieve the above ideas, you must solve the following questions. (1). How to set the initial position of the form: set the initial position of the form, which is done in the event form1_load (). This event is triggered when the form is loaded. The Form has a DesktopLocation property that is a two-dimensional location in the upper left corner of the form.
In the program, the value of this property is set by the POINT structure variable. The specifically: // set the form of the form that is initially floating, the position is the coordinates of the screen (0,240) Private Void Form1_Load (Object Sender, System. Eventargs e) {POINT P = New Point (0, 240); this.desktoplocation = p;} (2). How to implement the form from left to right floating: Set Timer1's interval value "10", is when Timer1 After starting, the event that triggered every 0.01 second is Timer1_Tick (), which is written in this event to the upper left corner of the upper left corner. The code is constantly adding "1" code, it is, the specific: private void Timer1_Tick (Object Sender, SYSTEM.EVENTARGS E) {{// Forms of the upper corner horizontal horizontal with Timer1 constantly adds a point p = new point (this.desktoplocation.x 1, this.desktoplocation.y); this.desktoplocation = p; if (PX == 550) {Timer1.enabled = false; Timer2.enabled = true;}} (3). How to implement the form from right away: code design and from left to right, the main difference is minus "1" instead of adding "1", as follows: // When the horizontal position of the upper left corner of the form is -150, Timer2 stops, Timer1 launches Private Void Timer2_Tick (Object Sender, System.EventArgs E) {file: // The upper corner horizontal of the form continues to reduce one POINT P = new point (this.desktoplocation.x - 1, this.desktoplocation.x; this.desktoplocation = p; if (px == - 150) {Timer1.enabled = true; Timer2.enabled = false;}} Sourcecodes of the Form Floating Program with Visual C #: By introducing the above, it is not difficult to write the source code of the form floating.
As follows: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace floatingForm {public class Form1: Form {private Timer timer1; private Timer timer2; private Label Label1; Private Button Button1; Private System.com; Public Form1 () {file: // Initialization InitializeComponent ();} file: // Clear the resources used in the program protected override void Dispose (bool disposing) {if (disposing) {if (components = null!) {components.Dispose ();}} base.Dispose (disposing);} private void InitializeComponent () {this.components = new System.ComponentModel .Container (); this.timer1 = new time (this.components); this.timer2 = new Timer (this.components); this.Label1 = new label (); this.button1 = new button (); this.suspendlayout (); this.timer1.enabled = true; this.timer1.interval = 10; this.timer1.tick = new system.eventhandler (this.timer1_tick); this.timer2.enabled = false; this.timer2.inter VAL = 10; this.timer2.tick = new system.EventHandler (this.timer2_tick); this.button1.font = new font ("Song", 10); this.button1.Location = New Point (1, 8) This.Button1.name = "button1"; this.button1.size = new size (80, 25); this.button1.tabindex = 0; this.button1.text = "Stop floating"; this.button1.click = New System.EventHandler (this.Button1_Click); this.label1.font = New font ("Song", 22F, FontStyle.Bold, GraphicsUnit.Point, (System.byte)); this.label1. Location = New Point (8, 38); this.label1.name = "label1"; this.label1.size = new size (344, 40);