Winfrom Countdown Control

Source: Internet
Author: User
Tags set time

Recently in doing a courier cabinet project, require the user no operation time to a minute, automatically return to the homepage, I then encapsulated a countdown control, nonsense less, directly on the code

 Public partial class Remainingtimeuc:usercontrol {public Remainingtimeuc () {initializecom        Ponent ();        } Form parantform = null; private void Remainingtimeuc_load (object sender, EventArgs e) {//Get the user control's long lift parantform = thi        S.findform ();            } #region Timeruc_tick Event private void Timeruc_tick (object sender, EventArgs e) {try {int totalsecond = 60;//set time is 60s var remaintime = totalsecond-mousekeyboardoperate. Getlastinputtime ();//Total time-user not operating time This.labRT.Text = remaintime.tostring (CultureInfo.InvariantCulture);//Display off                The remaining time to close if (Remaintime = =) {This.labRT.ForeColor = Color.orange;                     } if (Remaintime <= 9) {this.labRT.ForeColor = color.red;              This.labRT.Text = "0" + remaintime;  } if (remaintime = =) This.labRT.ForeColor = Color.White;                if (Remaintime = = 0) {parantform.close (); }} catch (Exception ex) {Loghelper.writelog (this. Name + "Timeruc_tick", ex.            Message); }} #endregion}

//
This method does not know which predecessor wrote, borrow a bit
//

public class Mousekeyboardoperate
{
<summary>
Create struct to return capture time
</summary>
[StructLayout (LayoutKind.Sequential)]
struct LASTINPUTINFO
{
<summary>
Set structure block capacity
</summary>
[MarshalAs (UNMANAGEDTYPE.U4)]
public int cbsize;

<summary>
Captured by Time
</summary>
[MarshalAs (UNMANAGEDTYPE.U4)]
public UINT Dwtime;
}

[DllImport ("User32.dll")]
private static extern bool Getlastinputinfo (ref lastinputinfo plii);

<summary>
Get keyboard and mouse time without action
</summary>
<returns> the time interval that the user last used the system to now, in seconds </returns>
public static long Getlastinputtime ()
{
var vlastinputinfo = new Lastinputinfo ();
Vlastinputinfo.cbsize = marshal.sizeof (Vlastinputinfo);
if (! Getlastinputinfo (ref Vlastinputinfo))
{
return 0;
}
Else
{
var count = Environment.tickcount-(long) vlastinputinfo.dwtime;
var icount = count/1000;
Return icount;
}
}

  

Winfrom Countdown Control

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.