C # Implementation of the WinForm form gradually show the effect, this blog park has already been implemented by others, the principle is very simple, is to change the transparency of the form by timing (from 0 to 1, that is, transparency from completely transparent to opaque), I am here to follow this idea, but I do this form is reusable , that is, the other form inherits from it, it can achieve fade effect, the code is as follows:
usingSystem;usingSystem.ComponentModel;usingSystem.Windows.Forms;namespaceTEMS. forms{ Public Partial classFormbase:form {PrivateTimer Formtimer =NULL; /// <summary> ///Get Opacity Property/// </summary>[DefaultValue (0)] [browsable (false)] Public New DoubleOpacity {Get{return Base. Opacity; } Set{Base. Opacity =0; } } PublicFormbase () {InitializeComponent (); Formtimer=NewTimer () {Interval = - }; Formtimer.tick+=NewEventHandler (Formtimer_tick); Base. Opacity =0; } Private voidFormtimer_tick (Objectsender, EventArgs e) { if( This. Opacity >=1) {formtimer.stop (); } Else { Base. Opacity + =0.2; } } Private voidFormbase_shown (Objectsender, EventArgs e) {Formtimer.start (); } }}
The following are the auto-generated code:
namespaceTEMS. forms{Partial classFormbase {/// <summary> ///Required designer variable. /// </summary> PrivateSystem.ComponentModel.IContainer components =NULL; /// <summary> ///Clean up any resources being used. /// </summary> /// <param name= "disposing" >true if managed resources should be disposed; otherwise, false.</param> protected Override voidDispose (BOOLdisposing) { if(Disposing && (Components! =NULL) ) {components. Dispose (); } Base. Dispose (disposing); } #regionWindows Form Designer generated code/// <summary> ///Required method for Designer support-do not modify///The contents of this method with the code Editor. /// </summary> Private voidInitializeComponent () { This. SuspendLayout (); // //Formbase// This. Autoscaledimensions =NewSystem.Drawing.SizeF (6F, 12F); This. AutoScaleMode =System.Windows.Forms.AutoScaleMode.Font; This. ClientSize =NewSystem.Drawing.Size (284,262); This. Name ="Formbase"; This. Text ="Formbase"; This. Shown + =NewSystem.EventHandler ( This. Formbase_shown); This. ResumeLayout (false); } #endregion }}View Code
In the code, I overwrite the Opacity property in the form class with the new keyword, making it read-only and non-editable, and one might say that the read-only code for this property is not well-written, that it should be stripped of the set accessor or set to private, and yes, the standard should be, and why don't I do it? The reason is that if you really set the property to private, then when the other forms inherit it, because we generally first build a standard form, the standard form at the time of creation when the form's properties have default values will automatically generate the initialization defaults, the standard form is created before the base class is changed to the Formbase class, This will cause error: Opacity is read-only, can not be assigned, so we can only let the outside see is read and write, but the actual sub-form assignment will not take effect, play a read-only effect, of course, if you do not feel trouble, you can set by the standard properties, and then after each build a form, It is also possible to clear the opacity code before changing the inheriting class.
Use is very simple, the same as the normal form, here is not described, you can copy the above code into their own projects, you can directly use.
In fact, through the idea of the above code, we can design a common window shutter switch effect form base class, I will try to achieve these functions, I hope you can support, thank you!
C # implement WinForm form to show effect gradually