C#实现WinForm窗体逐渐显示效果,这个博客园里面已经有其它人已经实现了,原理很简单,就是通过定时改变窗体的透明度(从0到1,即透明度从完全透明到不透明),我这里也是按照这个思路来实现的,但是我做的这个窗体是可复用的,即其它窗体继承自它后,就能实现渐显效果,代码如下:
using System; using System.ComponentModel; using System.Windows.Forms; namespace TEMS.Forms { public partial class FormBase : Form { private Timer formTimer = null; /// <summary> /// 获取Opacity属性 /// </summary> [DefaultValue(0)] [Browsable(false)] public new double Opacity { get { return base.Opacity; } set { base.Opacity = 0; } } public FormBase() { InitializeComponent(); formTimer = new Timer() { Interval = 100 }; formTimer.Tick += new EventHandler(formTimer_Tick); base.Opacity = 0; } private void formTimer_Tick(object sender, EventArgs e) { if (this.Opacity >= 1) { formTimer.Stop(); } else { base.Opacity += 0.2; } } private void FormBase_Shown(object sender, EventArgs e) { formTimer.Start(); } } }
以下是自动生成的代码:
namespace TEMS.Forms { partial class FormBase { /// <summary> /// Required designer variable. /// </summary> private System.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 void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.SuspendLayout(); // // FormBase // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(284, 262); this.Name = "FormBase"; this.Text = "FormBase"; this.Shown += new System.EventHandler(this.FormBase_Shown); this.ResumeLayout(false); } #endregion } }