我想实现在程序运行的时候显示启动画面,这个画面采用由完全不透明到完全透明的方式来显示,最后关闭,进而显示主窗体。代码如下:
program中的代码
namespace 渐变的窗体
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form2 form2 = new Form2();
form2.ShowDialog();
Application.Run(new Form1());
}
}
}form2中的代码
private void timer1_Tick(object sender, EventArgs e)
{
for (double opacityDouble = 1; opacityDouble > 0; opacityDouble -= 0.05)
{
this.Opacity = opacityDouble;
this.Refresh();
System.Threading.Thread.Sleep(200);
} this.Close();
} private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
//窗体关闭后发生 //Form1 form1 = new Form1(); //for (double opacityDouble = 0; opacityDouble < 1; opacityDouble += 0.05)
//{
// form1.Opacity = opacityDouble;
// form1.Refresh();
// System.Threading.Thread.Sleep(200);
//} } private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
//窗体即将关闭时发生 //Form1 form1 = new Form1(); //for (double opacityDouble = 0; opacityDouble < 1; opacityDouble += 0.05)
//{
// form1.Opacity = opacityDouble;
// form1.Refresh();
// System.Threading.Thread.Sleep(200);
//}
}其中想采用相反的方式显示主窗体form1,可是没有达到我想要的效果。
现象:
程序运行后,form2假面先会黑一下,虽然很短暂,但看着不舒服。
主界面form1不是逐渐显示的,而且显示后,有一段时间无法对齐进行操作。
program中的代码
namespace 渐变的窗体
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form2 form2 = new Form2();
form2.ShowDialog();
Application.Run(new Form1());
}
}
}form2中的代码
private void timer1_Tick(object sender, EventArgs e)
{
for (double opacityDouble = 1; opacityDouble > 0; opacityDouble -= 0.05)
{
this.Opacity = opacityDouble;
this.Refresh();
System.Threading.Thread.Sleep(200);
} this.Close();
} private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
//窗体关闭后发生 //Form1 form1 = new Form1(); //for (double opacityDouble = 0; opacityDouble < 1; opacityDouble += 0.05)
//{
// form1.Opacity = opacityDouble;
// form1.Refresh();
// System.Threading.Thread.Sleep(200);
//} } private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
//窗体即将关闭时发生 //Form1 form1 = new Form1(); //for (double opacityDouble = 0; opacityDouble < 1; opacityDouble += 0.05)
//{
// form1.Opacity = opacityDouble;
// form1.Refresh();
// System.Threading.Thread.Sleep(200);
//}
}其中想采用相反的方式显示主窗体form1,可是没有达到我想要的效果。
现象:
程序运行后,form2假面先会黑一下,虽然很短暂,但看着不舒服。
主界面form1不是逐渐显示的,而且显示后,有一段时间无法对齐进行操作。
private static extern bool AnimateWindow(int hwnd, int dwTime, int dwFlags); private const int AW_HOR_POSITIVE = 0x0001;//AW_HOR_POSITIVE : 自左向右显示窗口
private const int AW_HOR_NEGATIVE = 0x0002;//AW_HOR_NEGATIVE: 自右向左显示窗口
private const int AW_VER_POSITIVE = 0x0004;//AW_VER_POSITVE: 自顶向下显示窗口
private const int AW_VER_NEGATIVE = 0x0008;//AW_VER_NEGATIVE : 自下向上显示窗口
private const int AW_CENTER = 0x0010;//与 AW_HIDE 效果配合使用则效果为窗口几内重叠, 单独使用窗口向外扩展
private const int AW_HIDE = 0x10000;//隐藏窗口
private const int AW_ACTIVATE = 0x20000;//激活窗口, 在使用了 AW_HIDE 效果时不可使用此效果
private const int AW_SLIDE = 0x40000;//使用滑动类型, 默认为该类型. 当使用 AW_CENTER 效果时, 此效果被忽略
private const int AW_BLEND = 0x80000;//使用淡入效果
private int CloseOpen = 0x20000;// public Form1()
{
InitializeComponent();
} private void exitButton_Click(object sender, EventArgs e)
{
Application.Exit();
} private void Form1_Load(object sender, EventArgs e)
{
//窗体从无到有 AnimateWindow(this.Handle.ToInt32(), 5000, AW_BLEND | CloseOpen);
}
应该是form2的吧,我的启动界面是form2,主界面是form1.using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace 渐变的窗体
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
} private void timer1_Tick(object sender, EventArgs e)
{
for (double opacityDouble = 1; opacityDouble > 0; opacityDouble -= 0.05)
{
this.Opacity = opacityDouble;
this.Refresh();
System.Threading.Thread.Sleep(200);
} this.Close();
} private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
//窗体关闭后发生 //Form1 form1 = new Form1(); //for (double opacityDouble = 0; opacityDouble < 1; opacityDouble += 0.05)
//{
// form1.Opacity = opacityDouble;
// form1.Refresh();
// System.Threading.Thread.Sleep(200);
//}
} private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
//窗体即将关闭时发生 //Form1 form1 = new Form1(); //for (double opacityDouble = 0; opacityDouble < 1; opacityDouble += 0.05)
//{
// form1.Opacity = opacityDouble;
// form1.Refresh();
// System.Threading.Thread.Sleep(200);
//}
} private void Form2_Load(object sender, EventArgs e)
{
this.Opacity = 0;
}
}
}
还是会出现黑一下的现象。form1的我借用别人的API实现了一下,不过,不是很熟悉APIusing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;namespace 渐变的窗体
{
public partial class Form1 : Form
{
[DllImport("user32.dll")]
private static extern bool AnimateWindow(int hwnd, int dwTime, int dwFlags); private const int AW_HOR_POSITIVE = 0x0001;//AW_HOR_POSITIVE : 自左向右显示窗口
private const int AW_HOR_NEGATIVE = 0x0002;//AW_HOR_NEGATIVE: 自右向左显示窗口
private const int AW_VER_POSITIVE = 0x0004;//AW_VER_POSITVE: 自顶向下显示窗口
private const int AW_VER_NEGATIVE = 0x0008;//AW_VER_NEGATIVE : 自下向上显示窗口
private const int AW_CENTER = 0x0010;//与 AW_HIDE 效果配合使用则效果为窗口几内重叠, 单独使用窗口向外扩展
private const int AW_HIDE = 0x10000;//隐藏窗口
private const int AW_ACTIVATE = 0x20000;//激活窗口, 在使用了 AW_HIDE 效果时不可使用此效果
private const int AW_SLIDE = 0x40000;//使用滑动类型, 默认为该类型. 当使用 AW_CENTER 效果时, 此效果被忽略
private const int AW_BLEND = 0x80000;//使用淡入效果
private int CloseOpen = 0x20000; public Form1()
{
InitializeComponent();
} private void exitButton_Click(object sender, EventArgs e)
{
Application.Exit();
} private void Form1_Load(object sender, EventArgs e)
{
//窗体从无到有 AnimateWindow(this.Handle.ToInt32(), 5000, AW_BLEND | CloseOpen);
//函数原型:BOOL AnimateWindow(HWND hWnd,DWORD dwTime,DWORD dwFlags);
//bool Form1.AnimateWindow(int hwnd, int dwTime, int dwFags)
//hwnd:指定产生动画的窗口的句柄。
//dwTime:指明动画持续的时间(以微秒计),完成一个动画的标准时间为200微秒。
//dwFags:指定动画类型。这个参数可以是一个或多个下列标志的组合。标志描述 //窗体从左到右
//AnimateWindow(this.Handle.ToInt32(), 5000, AW_HOR_POSITIVE | CloseOpen);
}
}
}
一开始把form2的location设置到屏幕之外
渐变的时候再设置回来
楼主有没有在一开始把 Form.Opacity = 0?
其次,我没有看到楼主调用的具体代码,全是注释,Form1 的代码呢?
这样不对吧,应该不行的。首先,Form2是启动画面,运行时先出现的界面。Form2消失后Form1才出现。我在Form2的timer里写了如下的代码:private void timer1_Tick(object sender, EventArgs e)
{
//this.Opacity = 1; for (double opacityDouble = 1; opacityDouble > 0; opacityDouble -= 0.05)
{
this.Opacity = opacityDouble;
this.Refresh();
System.Threading.Thread.Sleep(200);
} this.Close();
}这可以实现窗体的渐渐消失。问题就是:在开始执行渐渐消失前的那一霎那,界面会闪一下啊。我不明白为什么会闪一下。你说先将Form2先移到屏幕之外在移回来,我试了一下。虽然没能把界面移到屏幕之外,但是闪烁的情况依然存在。而且眼睛可以分辨出界面是从一个地方迅速的移到另一个地方。
主要是Form2的代码,timer那一段。Form1我用的是API实现的。没有出现闪烁的现象。你可以看一下我在3楼贴的Form2 的代码。至于
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
及下面的代码成了注释,是因为我当时Form1 的也用的是属性里的opacity来实现的。还是由于闪烁,我改成了API函数。改了之后,那些代码就没有用了,我有没有删除,所以就改成了注释的形式。既然API可以实现窗体的渐变,而且Winform里的opacity属性也可以,那么就应该是一样的。但用opacity属性会闪烁,这个问题没有解决。
不是,开始的时候,窗体的opacity=1;Form2 是渐渐消失的,即:opacity由1逐渐变化到0,然后关闭。
from1 出现。opacity由0渐渐变化到1,最后保持opacity=1.
一下是我测试过的 Demo,没有出现闪一下的情况,你看看。private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
}private void timer1_Tick(object sender, EventArgs e)
{
if (Opacity == 1)
{
timer1.Stop();
}
else
{
Opacity += 0.05;
}
}private void button1_Click(object sender, EventArgs e)
{
using (Form1 f = new Form1())
{
f.ShowDialog();
}
}timer1 的时间间隔为 20
这网,哎,不说了。
liu-988是163的。看一下你的程序。谢谢。
不清楚你那里的情况,我这里是 VS2010、VS2008 都试过。可能和系统有关系。和软件版本无关吧。某个地方可能出问题了。我猜的