& Lt; WinForm_2 & gt; a simple and practical small Application -- desktop clock

Source: Internet
Author: User

 

 

 

 

 

 

: Window title

: Window background color

: Retrieving or setting will indicate the color of the transparent area of the form-this attribute is relatively important. Let's take a look at the description in msdn:

When the Color is assigned to the TransparencyKey attribute, the form area with the same BackColor is transparent. Any mouse operations (such as mouse clicking) performed in the transparent area of the form are transmitted to the window in the transparent area. (If You Want To transparent the window client area, you only need to set this attribute to the background color BackColor of the window)

All options are provided (For details, refer to msdn)

 

// Window title Text = "Export topclock"; // ResizeRedraw = true; BackColor = SystemColors. window; // set the color of the transparent area of the Window (the same as the background color) TransparencyKey = BackColor; // remove the border FormBorderStyle = FormBorderStyle. none; // The form ShowInTaskbar = false is not displayed in the taskbar; // the actual position of the window StartPosition = FormStartPosition. centerScreen; // maximize the display of WindowState = FormWindowState. maximized;


 

These two API functions start and destroy a timer, and respond to the operation when the timer message arrives in the message or the specified function. In WinForm, these functions are encapsulated in this class, specifically the class and method attributes to install event delegation-how to respond to timer messages

 

// Set the timer Timer = new timer (); timer. Interval = 1000; timer. Tick + = new EventHandler (TimerOnTick); timer. Start ();
// Timer RESPONSE event public void TimerOnTick (object obj, EventArgs ea) {Invalidate ();}


But don't forget to destroy the timer (although C # has a GC garbage collection mechanism, we 'd better solve it ourselves. Oh, Win32 SDK habits)

// When the window is closed, Stop the timer protected override void OnFormClosing (FormClosingEventArgs fcea) {base. OnFormClosing (fcea); timer. Stop ();}


 

Method.

 

// Draw the digital clock protected override void OnPaint (PaintEventArgs pea) {base. onPaint (pea); Graphics grfx = pea. graphics; String str = DateTime. now. toString ("T"); // obtain the local time Font font = new Font (Font. fontFamily, 100); // set the new font SizeF sizef = grfx. measureString (str, font); // obtain the size of the time string (for positioning) // draw a digital clock grfx in the upper right corner of the desktop. drawString (str, font, Brushes. orange, (ClientSize. width-sizef. width), 0 );}


 

// Define topclock. csusing System; using System. drawing; using System. windows. forms; class extends topclock: Form {private Timer timer; public extends topclock () {// window title Text = "extends topclock"; // ResizeRedraw = true; BackColor = SystemColors. window; // set the color of the transparent area of the Window (the same as the background color) TransparencyKey = BackColor; // remove the border FormBorderStyle = FormBorderStyle. none; // The form ShowInTaskbar = false is not displayed in the taskbar; // the actual position of the window StartPosition = FormStartPosition. centerScreen; // maximize the display of WindowState = FormWindowState. maximized; // set the timer Timer = new timer (); timer. interval = 1000; timer. tick + = new EventHandler (TimerOnTick); timer. start () ;}// timer RESPONSE event public void TimerOnTick (object obj, EventArgs ea) {Invalidate ();} // press Ctrl + Q to exit the program protected override void OnKeyDown (KeyEventArgs kea) {base. onKeyDown (kea); if (kea. modifiers = Keys. control & kea. keyCode = Keys. q) Close () ;}// draw the digital clock protected override void OnPaint (PaintEventArgs pea) {base. onPaint (pea); Graphics grfx = pea. graphics; String str = DateTime. now. toString ("T"); Font font = new Font (Font. fontFamily, 100); SizeF sizef = grfx. measureString (str, font); // draw the digital clock grfx in the upper right corner of the desktop. drawString (str, font, Brushes. orange, (ClientSize. width-sizef. width), 0);} // when the window is closed, stop the timer protected override void OnFormClosing (FormClosingEventArgs fcea) {base. onFormClosing (fcea); timer. stop ();} static void Main () {Application. run (new batch topclock ());}}


 

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.