C # Two code snippets related to winform (multi-threaded Form Control and form fade-in and fade-out effect)

Source: Internet
Author: User

1.Multi-threaded Form Control

Delegate void SetTextCallback (string text );

Private void SetText (string text)

{

// InvokeRequired required compares the thread ID of

// Calling thread to the thread ID of the creating thread.

// If these threads are different, it returns true.

If (this. textBox1.InvokeRequired)

{

SetTextCallback d = new SetTextCallback (SetText );

This. Invoke (d, new object [] {text });

}

Else

{

This. textBox1.Text = text;

}

}

 

SetText (time. ToString ());

 

 

2.

C # several methods for implementing the form fade-in and fade-out effect (convert)

1.

Private void Form1_Load (object sender, System. EventArgs e)

For (double d = 0.01; d <1; d + = 0.02)

{

System. Threading. Thread. Sleep (1 );

Application. DoEvents ();

This. Opacity = d;

This. Refresh ();

}

 

 

2.

 

Private void timerjavastick (Object sender, system. eventargs E)

{

This. Opacity = winshow;

Winshow ++ = 0.1;

If (winshow> = 1)

{

Timer1.dispose ();

}

}

 

 

3.

 

Use a loop or timer,

FrmForm myForm = new frmForm ()

FrmForm. Opacity = 0;

FrmForm. show ();

For (int I = 0; I <100; I ++)

{

Application. DoEvents ()

FrmForm. Opacity = I/100;

}

 

 

4.

 

# Region ******** form fade-in effect function ********

Private double WinShow = 0; // variable used for window fade-in effect

Private void FormShow (System. Windows. Forms. Form Curfrm)

{

Curfrm. Opacity = WinShow;

WinShow ++ = 0.01;

If (WinShow = 1)

{

Curfrm. timerShow. Stop ();

}

}

# Endregion

# Region ******** example of function calling for form fade-in effect ********

// Implement the fade-in effect of the window

Private void timerShow_Tick (object sender, System. EventArgs e)

{

// TimerShow, which is the name of the timer control. Set timerShow. interval to 100.

FormShow (this );

}

# Endregion

 

5.

 

Using System. Runtime. InteropServices;

 

 

Public class Win32

{

Public const Int32 AW_HOR_POSITIVE = 0x00000001; // open the window from left to right

Public const Int32 AW_HOR_NEGATIVE = 0x00000002; // open the window from right to left

Public const Int32 AW_VER_POSITIVE = 0x00000004; // open the window from top to bottom

Public const Int32 AW_VER_NEGATIVE = 0x00000008; // open the window from bottom to top

Public const Int32 AW_CENTER = 0x00000010;

Public const Int32 AW_HIDE = 0x00010000; // This constant must be added if you want to use this function when uninstalling the form.

Public const Int32 AW_ACTIVATE = 0x00020000; // After the form is opened through this function, the focus will be lost by default, unless this constant is added

Public const Int32 AW_SLIDE = 0x00040000;

Public const Int32 AW_BLEND = 0x00080000; // fade-in and fade-out effect

[DllImport ("user32.dll", CharSet = CharSet. Auto)]

Public static extern bool AnimateWindow (

IntPtr hwnd, // handle to window

Int dwTime, // duration of animation

Int dwFlags // animation type

);

}

 

 

 

/* Fade-in form */

 

Private void Form_Load (object sender, EventArgs e)

{

Win32.AnimateWindow (this. Handle, 2000, Win32.AW _ BLEND );

}

 

 

 

/* Fade out form */

 

Private void Form_FormClosing (object sender, FormClosingEventArgs e)

{

Win32.AnimateWindow (this. Handle, 2000, Win32.AW _ SLIDE | Win32.AW _ HIDE | Win32.AW _ BLEND );

}

 

 

 

Related Article

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.