Let the form float--c# Timer component usage

Source: Internet
Author: User
Tags implement
The timer component is also a WinForm component, and the biggest difference from the other WinForm components is that the timer component is invisible, and most of the other components are visible and designed. The timer component is also encapsulated in the namespace System.Windows.Forms, and its main function is to trigger the same event every other fixed period of time after the timer component is started. The timer component is a more commonly used component in program design, and although there are few properties and events, it can have unintended effects in some places.

The program described in this article is a form-waving program made with Visual C #, which uses a large number of timer components. Here is a brief introduction to the design and operation of this program environment.

A The software environment for the design and operation of this article:

(1) Microsoft company Windows 2000 Server Edition

(2). Net FrameWork SDK Beta 2

Two The idea of programming and the solution of the key steps:

In fact, to make the form of the program to float up, in fact, the idea is relatively simple. First, when the form is loaded, set a display initial position for the form. Then by using the two timer components defined in the form, one of which is called Timer1, the function is to control the form from left to right (of course, if you want to, you can also change from top to bottom, or another way of flapping.) Another Timer2 is to control the form from right to left (and you can also change to other modes of flapping). Of course, these two timer components cannot be started at the same time, in this article's program, is to set the Timer1 component to start, when this Timer1 starts, every 0.01 seconds, will in the triggering event to the form's upper left corner horizontal axis all adds "1", when we see the result is the form from left to right continuously moves , Timer1 stops when it is moved to a certain position. Timer2 starts, every 0.01 seconds, in the trigger defined event to the form's upper left corner of the horizontal axis minus "1", when we see the result is the form from right to left constantly moving. When moved to a certain position, the Timer1 starts, the Timer2 stops, and so on, so the form floats. To achieve the above ideas, we must solve the following problems.

(1). How to set the initial position of the form:

Setting the initial position of the form is done in the event Form1_Load (). This event is triggered when the form is loaded. The form has a Desktoplocation property, which is the two-dimensional position of the upper-left corner of the form. The value of this property is set by the point structure variable in the program, as follows:

Sets the position at which the form first flutters, and 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 a form that floats from left to right:

Set Timer1 interval value is "10", that is, when the Timer1 boot, every 0.01 seconds triggered event is Timer1_Tick (), in this event to write to the top left corner of the horizontal axis and constantly add "1" code, you can, specifically as follows:

private void Timer1_Tick (object sender, System.EventArgs e)
{
{//The top-left corner of the form with timer1 constantly adding a
Point P = new Point (this. Desktoplocation.x + 1, this. DESKTOPLOCATION.Y);
This. Desktoplocation = p;
if (p.x = 550)
{
Timer1. Enabled = false;
Timer2. Enabled = true;
}
}

(3). How to implement a form that floats from right to left:

Code design is similar to moving from left to right, the main difference is to subtract "1" instead of adding "1", specifically as follows:

When the horizontal coordinate of the top left corner of the form is-150, Timer2 stops, Timer1 starts
private void Timer2_tick (object sender, System.EventArgs e)
{The horizontal axis of the upper left corner of the file://form continues to decrease with timer2
Point P = new Point (this. Desktoplocation.x-1, this. DESKTOPLOCATION.Y);
This. Desktoplocation = p;
if (p.x = = 150)
{
Timer1. Enabled = true;
Timer2. Enabled = false;
}
}
Three To write the source code for a form flare program in Visual C #:

Through the above introduction, it is not difficult to write the form of the program source code. 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.ComponentModel.IContainer components;
Public Form1 ()
{
FILE://Initialize individual components in a form
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 Timer (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.Interval = 10;
This.timer2.Tick + = new System.EventHandler (This.timer2_tick);

This.button1.Font = new Font ("Song Body", 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 flapping";
This.button1.Click + = new System.EventHandler (This.button1_click);

This.label1.Font = new Font ("XXFarEastFont-Arial", 22F, FontStyle.Bold, Graphicsunit.point, ((System.Byte) (0));
This.label1.Location = new Point (8, 38);
This.label1.Name = "Label1";
This.label1.Size = new Size (344, 40);
This.label1.TabIndex = 1;
This.label1.Text = "fluttering form with Visual C #!" " ;

This. AutoScaleBaseSize = new Size (5, 13);
This. ClientSize = new Size (352, 70);
This. Controls.Add (THIS.LABEL1);
This. Controls.Add (This.button1);
This. Name = "Form1";
This. Text = "fluttering form with Visual C #!" ";
This. Load + = new System.EventHandler (this. Form1_Load);
This. ResumeLayout (FALSE);
}
static void Main ()
{
Application.Run (New Form1 ());
}
file://set the position where the form first fluttered
private void Form1_Load (object sender, System.EventArgs e)
{
Point P = new Point (0, 240);
This. Desktoplocation = p;
}
file://when the horizontal coordinate of the top left corner of the form is 550, timer1 stops, Timer2 starts
private void Timer1_Tick (object sender, System.EventArgs e)
{
The horizontal axis of the upper left corner of the file://form with timer1 continuously adds a
Point P = new Point (this. Desktoplocation.x + 1, this. DESKTOPLOCATION.Y);
This. Desktoplocation = p;
if (p.x = 550)
{
Timer1. Enabled = false;
Timer2. Enabled = true;
}
}
file://when the horizontal coordinate of the top left corner of the form is-150, Timer2 stops, Timer1 starts
private void Timer2_tick (object sender, System.EventArgs e)
{The horizontal axis of the upper left corner of the file://form continues to decrease with timer2
Point P = new Point (this. Desktoplocation.x-1, this. DESKTOPLOCATION.Y);
This. Desktoplocation = p;
if (p.x = = 150)
{
Timer1. Enabled = true;
Timer2. Enabled = false;
}
}
file://Stop all the timer
private void Button1_Click (object sender, System.EventArgs e)
{
Timer1. Stop ();
Timer2. Stop ();
}
}
}


Four Summarize:

Just the right use of the timer component can have an unexpected effect. Because the main purpose of this article is to introduce the use of Timer components, the program function is not very powerful, interested readers, you can try to modify the following ideas to see if you can let the form up and down, so that the form of the floating rules. Of course, if you are more interested, you can also take the border of the form and maximize, minimize and so on the button to hide, put a good-looking picture filled with the entire form, and then let him float up, this effect is even more surprising.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.