写一个form窗体渐变显示的方法!让一个窗体慢慢的显示出来!!
没写方法的时候我是这样写的    
  private bool m_showForm = true;private void timer1_Tick(object sender, EventArgs e)
        {
            if (m_showForm)
            {
                double d = 0.04;
                if (Opacity + d >= 1.0)
                {
                    Opacity = 1.0;
                    timer1.Stop();
                }
                else
                {
                    Opacity += d;
                }
            }
        }我现在想把这个方法写成一个方法。。不知道怎么写
我想达到的目的就是。想让很多窗体都可以 有渐变显示的效果!!
写成一个方法哪个窗体想渐变显示。就在这个窗体里引用这个方法就可以实现。!!哪问能帮我

解决方案 »

  1.   

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;namespace Example002
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Timer timer1;
    private System.ComponentModel.IContainer components; public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent();
    //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows Form Designer generated code
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.components = new System.ComponentModel.Container();
    this.timer1 = new System.Windows.Forms.Timer(this.components);
    // 
    // timer1
    // 
    this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(256, 141);
    this.Name = "Form1";
    this.Opacity = 0.5;
    this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void Form1_Load(object sender, System.EventArgs e)
    {
    this.timer1.Enabled=true;
    this.Opacity=0;
    } private void timer1_Tick(object sender, System.EventArgs e)
    {
    if(this.Opacity<1)
    {
    this.Opacity=this.Opacity+0.05;
    }
    else
    {
    this.timer1.Enabled=false;
    }
    }
    }
    }
      

  2.   

    弄一个Timer,定时改变Form的Opacity属性。
      

  3.   

    怎么都把我的代码抄一边?
    我是要把修改Opacity属性提取出来!!写成一个方法!!!
    让别的窗体也可以共用这个方法。。!!应该怎么写?
      

  4.   

    类!!
    using System;
    using System.Collections.Generic;
    using System.Text;namespace DVDList.Commom
    {
       public class OpacityForms
        {
           System.Windows.Forms.Form ChangeOpacityForms;
           bool M_ShowForm;
           /// <summary>
           /// 窗体渐变效果
           /// </summary>
           /// <param name="FormName">需渐变的窗体</param>
           /// <param name="B">是否开启渐变</param>
           /// <returns></returns>
           public void OpacityForms(System.Windows.Forms.Form FormName, bool B)
           {
               this.ChangeOpacityForms = FormName;
               this.m_showform = B;
           }
           public void ShowForm()
           {
               this.ChangeOpacityForms.Opacity = 0;
               if (m_showform)
               {
                   double d = 0.04;
                   if (ChangeOpacityForms.Opacity + d >= 1.0)
                   {
                       ChangeOpacityForms.Opacity = 1.0;
                       break;
                   }
                   else
                   {
                       ChangeOpacityForms.Opacity += d;
                   }
               }
           }
        }
    }
    调用的窗体
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using DVDList.DataAccess;
    using System.Collections;namespace DVDList
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                //Opacity = 0.0;
                timer1.Start();
            }        private void timer1_Tick(object sender, EventArgs e)
            {
                Commom.OpacityForms OF = new DVDList.Commom.OpacityForms(Form1,true);
                OF.ShowForm();
            }    }
    }
    提示错误
    错误 1 “OpacityForms”: 成员名称不能与它们的封闭类型相同 C:\Documents and Settings\Administrator\桌面\DVDList\DVDList\DVDList\DVDList\Commom\OpacityForms.cs 15 20 DVDList怎么解决啊
      

  5.   

    就出错的问题:public void OpacityForms(System.Windows.Forms.Form FormName, bool B)改为 -----------------------------------------------------------------
    public OpacityForms(System.Windows.Forms.Form FormName, bool B)
      

  6.   

    有个API--AnimateWindow,就是实现这个效果的。
      

  7.   

    做一个BaseForm,所有需要实现功能的Form继承一下就行了
      

  8.   

    类代码
    using System;namespace model
    {
    /// <summary>
    /// Class1 的摘要说明。
    /// </summary>
    public class OpacityForms
    {
    System.Windows.Forms.Form ChangeOpacityForms;
      
    bool M_ShowForm;
    /// <summary>
    /// 窗体渐变效果
    /// </summary>
    /// <param name="FormName">需渐变的窗体</param>
    /// <param name="B">是否开启渐变</param>
    /// <returns></returns>
    public  OpacityForms(System.Windows.Forms.Form FormName, bool B)
    {
    this.ChangeOpacityForms = FormName;
      this.ChangeOpacityForms.Opacity = 0;
    this.M_ShowForm = B;
    }
    public void ShowForm()
    {

    if (M_ShowForm)
    {
    double d = 0.04;
    if (ChangeOpacityForms.Opacity + d >= 1.0)
    {
    ChangeOpacityForms.Opacity = 1.0;
    M_ShowForm=false;
    }
    else
    {
    ChangeOpacityForms.Opacity += d;
    }
    }
    }
    }
    }
      

  9.   

    调用代码using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;namespace model
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Timer timer1;
    private System.ComponentModel.IContainer components;
    OpacityForms temp; public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    }
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if(components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.components = new System.ComponentModel.Container();
    this.timer1 = new System.Windows.Forms.Timer(this.components);
    // 
    // timer1
    // 
    this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(376, 273);
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load); }
    #endregion private void timer1_Tick(object sender, System.EventArgs e)
    {
      temp.ShowForm();
    } private void Form1_Load(object sender, System.EventArgs e)
    {
    temp=new OpacityForms(this,true);
    this.timer1.Enabled=true;
    this.timer1.Start();
    }
    }
    }
      

  10.   

    public  OpacityForms(System.Windows.Forms.Form FormName, bool B)这里这样写是错的!!!
    你代码和我的没区别就上面这个少个void~!!没有写void必须写返回类型!!!
      

  11.   


    我的代码和你的有很多区别啊
    1.this.ChangeOpacityForms.Opacity = 0;
    不能在函数据中写
    2.OpacityForms temp;
    不能在Tick事件中定义
    还有一些,你细看看吧,我的CODE编不过去么》?
      

  12.   

    public  OpacityForms(System.Windows.Forms.Form FormName, bool B)
    是构造函数据啊
    怎么要弄返回值?
      

  13.   

    写一个类如FormBase,在这个类中实现这种效果,需要这种效果的窗口,从这个FormBase继承就可以了,不知这种方案对LZ是否可行
      

  14.   

    写成一个基类,要用到这中效果,就由这个类继承就可以了,基类代码如下:
    using System;
    using System.Windows.Forms;namespace WindowsApplication
    {
    /// <summary>
    /// BaseForm 的摘要说明。
    /// </summary>
    public class BaseForm:System.Windows.Forms.Form
    {
    private System.Windows.Forms.Timer timer1 = new Timer(); public BaseForm()
    {
    this.Opacity = 0.01;
    timer1.Enabled = true;
    timer1.Tick += new System.EventHandler(timer1_Tick);
    } private void timer1_Tick(object sender, System.EventArgs e)
    {
    double d = 0.04;
    if (Opacity + d >= 1.0)
    {
    Opacity = 1.0;
    timer1.Stop();
    }
    else
    {
    Opacity += d;
    }
    }
    }
    }
    ——————————
    子类代码如下:
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;namespace WindowsApplication
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : BaseForm
    {
    private System.ComponentModel.IContainer components; public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void Form1_Load(object sender, System.EventArgs e)
    {

    }
    }
    }
    -------
    注意:如果要让窗体显示这种效果的话,子类窗体的Opacity值要么不指定,就是把那行代码删掉,要么指定为0.01。
      

  15.   

    写一个类
    添加一个静态方法
    public class Class1
    {
    public Class1()
    {
    } public static bool change(Form fm,double step)
    {
    fm.Opacity += step;
    return (fm.Opacity < 1);
    }
    }在窗口加一个timer private void timer1_Tick(object sender, System.EventArgs e)
    {
    if(!Class1.change(this,0.1)) timer1.Stop();
    }你可以对这个类进行以下改进
    将timer在类里面动态添加到参数窗口,然后直接调用一个方法启动该timer,
    给你提供一个思路,改进自己写一下