Let the form float-c# Timer component usage

Source: Internet
Author: User

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:

//设定窗体起初飘动的位置,位置为屏幕的坐标的(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)
{
{ //窗体的左上角横坐标随着timer1不断加一
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:

//当窗体左上角位置的横坐标为-150时,timer2停止,timer1启动
private void timer2_Tick(object sender, System.EventArgs e)
{ file://窗体的左上角横坐标随着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 #:

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.