5个回答
2011-12-17
展开全部
FormMain.cs // FormMain 当前窗口,用 this 表示,其实现大致如下:
-----------------------------------------------------------
//省略其他命名空间引用
using System.Threading;
public class FormMain
{
FormOther frmOther;//在FormMain类中定义一个FormOther变量
//省略其他变量定义
//构造函数
public FormMain()
{
frmOther = new FormOther()
frmOther.Visible = false;
Thread thread = new Thread(new ParameterizedThreadStart(threadDo));
thread.Start(3);//等待特定时间
}
public void threadDo(object args)
{
int wait = Convert.ToInt32(args);
while(true)
{
Thread.Sleep(--wait);
if(wait <=0)
{
this.Hide();
frmOther.Show();
break;
}
}
}
}
-----------------------------------------------------------
//省略其他命名空间引用
using System.Threading;
public class FormMain
{
FormOther frmOther;//在FormMain类中定义一个FormOther变量
//省略其他变量定义
//构造函数
public FormMain()
{
frmOther = new FormOther()
frmOther.Visible = false;
Thread thread = new Thread(new ParameterizedThreadStart(threadDo));
thread.Start(3);//等待特定时间
}
public void threadDo(object args)
{
int wait = Convert.ToInt32(args);
while(true)
{
Thread.Sleep(--wait);
if(wait <=0)
{
this.Hide();
frmOther.Show();
break;
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
首先一个问题,你要关闭的窗体是不是主窗体,如果是主窗体要实现这样只能隐藏:this.Hide(); 然后显示另一个窗体,不能关闭,因为主窗体关闭程序就退出; 如果要你要关闭的窗体不是主窗体, 那么你只要在 弹出这个窗体的 窗体放一个定时器, 控制关闭和显示另一个窗体就可以了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
两个类成员变量f2 f3,f1的构造中定义了个Timer,5秒切换f2和f3的显示
Form2 f2;
Form3 f3;
public Form1()
{
InitializeComponent();
Timer tmr = new Timer();
tmr.Interval = 5000;
tmr.Tick += new EventHandler(tmr_Tick);
tmr.Start();
f2 = new Form2();
f2.Show();
}
private void tmr_Tick(object sender, EventArgs e)
{
if(f2 != null)
{
f2.Close();
f2 = null;
f3 = new Form3();
f3.Show();
}
else
{
f3.Close();
f3 = null;
f2 = new Form2();
f2.Show();
}
}
Form2 f2;
Form3 f3;
public Form1()
{
InitializeComponent();
Timer tmr = new Timer();
tmr.Interval = 5000;
tmr.Tick += new EventHandler(tmr_Tick);
tmr.Start();
f2 = new Form2();
f2.Show();
}
private void tmr_Tick(object sender, EventArgs e)
{
if(f2 != null)
{
f2.Close();
f2 = null;
f3 = new Form3();
f3.Show();
}
else
{
f3.Close();
f3 = null;
f2 = new Form2();
f2.Show();
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用窗口 没有做过,是不是可以用其它的方法代替
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
加入以下类:
public class SplashAppContext : ApplicationContext
{
Form mainForm = null;
Timer splashTimer = new Timer();
public SplashAppContext(Form mainForm, Form splashForm) : base(splashForm)
{
this.mainForm = mainForm;
splashTimer.Tick += new EventHandler(SplashTimeUp);
splashTimer.Interval = 8000;
splashTimer.Enabled = true;
}
/// <summary>
/// This is the timer event handler. We use the timer to tell the ApplicationContext
/// that the splash screen is ready to be closed.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SplashTimeUp(object sender, EventArgs e)
{
//This is called if the splash is ready to be closed. In order to do this
//we first dispose of the timer (we dont need any leaks do we?) and then
//call the base.MainForm.Close function which will triger the MainFormClosed event
//that we overrode so we can set the Application Context's main form as the
//main form the user passed into the constructor.
splashTimer.Enabled = false;
splashTimer.Dispose();
base.MainForm.Close();
}
/// <summary>
/// Normaly, if not overridden, this event will call ExitThreadCore function.
/// We have overridden it to catch when the splash form has closed. When the
/// spash form closes, we want to set the main form passed in the contructor to
/// the base.MainForm property. when the main form closes, this override will be
/// called again, and we just pass the call onto the base.OnMainFormClosed, with will
/// tells the Application object to terminate the UI thread.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected override void OnMainFormClosed(object sender, EventArgs e)
{
if (sender is Splash)
{
base.MainForm = this.mainForm;
base.MainForm.Show();
}
else if (sender is frmMain)
{
base.OnMainFormClosed(sender, e);
}
}
/// <summary>
/// This sets how long the spash form will show once it is at 100% opacity. Default is 2 seconds.
/// </summary>
public int SecondsSplashShown
{
set
{
splashTimer.Interval = value * 1000;
}
}
}
public class SplashFadeAppContext : ApplicationContext
{
/// <summary>
/// Internal flags to tell process what state the splash form is in
/// </summary>
private enum FormStatus
{
Open = 1,
Opening = 2,
Closing = 4,
Closed = 8
}
private FormStatus formStatus = FormStatus.Opening;
private Form flashForm = null;
private Form mainForm = null;
private Timer splashTimer = new Timer();
private int showSplashInterval = 8000;
private int fadeInterval = 50;
private bool doFadeClose = false;
public SplashFadeAppContext(Form mainForm, Form splashForm) : base(splashForm)
{
this.flashForm = splashForm;
this.mainForm = mainForm;
splashTimer.Tick += new EventHandler(SplashTimeUp);
splashTimer.Interval = fadeInterval;
splashTimer.Enabled = true;
}
/// <summary>
/// This is the timer event that controls what our spash screen does.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SplashTimeUp(object sender, EventArgs e)
{
if (formStatus == FormStatus.Opening)
{
//if the splash is opening, the opacity will increase in increments of 5 until it is fully
//shown. Once it is fully opacic, it sets the status flag to open.
if (base.MainForm.Opacity < 1)
base.MainForm.Opacity += .05;
else
formStatus = FormStatus.Open;
}
else if (formStatus == FormStatus.Closing)
{
//if the splash is closing, the opacity will decrease in increments of 5 until it is fully
//hidden. Once it is fully transparent, it sets the status flag to closed.
if (base.MainForm.Opacity > .10)
{
base.MainForm.Opacity -= .05;
splashTimer.Interval = fadeInterval;
}
else
{
formStatus = FormStatus.Closed;
}
}
else if (formStatus == FormStatus.Open)
{
//Once the splash is open and fully shown, the timer interval is set to the splash delay setting,
//which is defaulted to 2 seconds, and then sets the status flag depending on if the user
//wants to just close the splash or fade it out.
splashTimer.Interval = showSplashInterval;
if (doFadeClose)
formStatus = FormStatus.Closing;
else
formStatus = FormStatus.Closed;
}
else if(formStatus == FormStatus.Closed)
{
//This is called if the splash is ready to be closed. In order to do this
//we first dispose of the timer (we dont need any leaks do we?) and then
//call the base.MainForm.Close function which will triger the MainFormClosed event
//that we overrode so we can set the Application Context's main form as the
//main form the user passed into the constructor.
splashTimer.Enabled = false;
splashTimer.Dispose();
base.MainForm.Close();
}
}
/// <summary>
/// Normaly, if not overridden, this event will call ExitThreadCore function.
/// We have overridden it to catch when the splash form has closed. When the
/// spash form closes, we want to set the main form passed in the contructor to
/// the base.MainForm property. when the main form closes, this override will be
/// called again, and we just pass the call onto the base.OnMainFormClosed, with will
/// tells the Application object to terminate the UI thread.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected override void OnMainFormClosed(object sender, EventArgs e)
{
if (sender is Splash)
{
base.MainForm = this.mainForm;
base.MainForm.Show();
}
else if (sender is frmMain)
{
base.OnMainFormClosed(sender, e);
}
}
#region Public Properties
/// <summary>
/// Bool that determines if spash form will fade up from 0% opacity to 100% when
/// the spash form opens. Default is false.
/// </summary>
public bool DoFadeOpen
{
set
{
if (value == true)
{
base.MainForm.Opacity = 0;
formStatus = FormStatus.Opening;
}
}
}
/// <summary>
/// Bool that determines if spash form will fade down from 100% opacity to 0% when
/// the spash form closes. Default is false.
/// </summary>
public bool DoFadeClose
{
set
{
doFadeClose = value;
}
}
/// <summary>
/// This sets how long the spash form will show once it is at 100% opacity. Default is 2 seconds.
/// </summary>
public int SecondsSplashShown
{
set
{
showSplashInterval = value * 1000;
}
}
#endregion Public Properties
}
在主函数中依次调用两个窗体frmMain和Splash,
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Process instance = RunningInstance();
if (instance == null)
{
SplashFadeAppContext splashContext = new SplashFadeAppContext(new frmMain(), new Splash());
Application.Run(splashContext);
}
}
至于Splash只是先出现的窗体,里面的代码就可以自已写了
private void Splash_Load(object sender, EventArgs e)
{
timer1.Interval = 1000;
timer2.Interval = 80;
timer1.Enabled = true;
timer2.Enabled = true;
}
public class SplashAppContext : ApplicationContext
{
Form mainForm = null;
Timer splashTimer = new Timer();
public SplashAppContext(Form mainForm, Form splashForm) : base(splashForm)
{
this.mainForm = mainForm;
splashTimer.Tick += new EventHandler(SplashTimeUp);
splashTimer.Interval = 8000;
splashTimer.Enabled = true;
}
/// <summary>
/// This is the timer event handler. We use the timer to tell the ApplicationContext
/// that the splash screen is ready to be closed.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SplashTimeUp(object sender, EventArgs e)
{
//This is called if the splash is ready to be closed. In order to do this
//we first dispose of the timer (we dont need any leaks do we?) and then
//call the base.MainForm.Close function which will triger the MainFormClosed event
//that we overrode so we can set the Application Context's main form as the
//main form the user passed into the constructor.
splashTimer.Enabled = false;
splashTimer.Dispose();
base.MainForm.Close();
}
/// <summary>
/// Normaly, if not overridden, this event will call ExitThreadCore function.
/// We have overridden it to catch when the splash form has closed. When the
/// spash form closes, we want to set the main form passed in the contructor to
/// the base.MainForm property. when the main form closes, this override will be
/// called again, and we just pass the call onto the base.OnMainFormClosed, with will
/// tells the Application object to terminate the UI thread.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected override void OnMainFormClosed(object sender, EventArgs e)
{
if (sender is Splash)
{
base.MainForm = this.mainForm;
base.MainForm.Show();
}
else if (sender is frmMain)
{
base.OnMainFormClosed(sender, e);
}
}
/// <summary>
/// This sets how long the spash form will show once it is at 100% opacity. Default is 2 seconds.
/// </summary>
public int SecondsSplashShown
{
set
{
splashTimer.Interval = value * 1000;
}
}
}
public class SplashFadeAppContext : ApplicationContext
{
/// <summary>
/// Internal flags to tell process what state the splash form is in
/// </summary>
private enum FormStatus
{
Open = 1,
Opening = 2,
Closing = 4,
Closed = 8
}
private FormStatus formStatus = FormStatus.Opening;
private Form flashForm = null;
private Form mainForm = null;
private Timer splashTimer = new Timer();
private int showSplashInterval = 8000;
private int fadeInterval = 50;
private bool doFadeClose = false;
public SplashFadeAppContext(Form mainForm, Form splashForm) : base(splashForm)
{
this.flashForm = splashForm;
this.mainForm = mainForm;
splashTimer.Tick += new EventHandler(SplashTimeUp);
splashTimer.Interval = fadeInterval;
splashTimer.Enabled = true;
}
/// <summary>
/// This is the timer event that controls what our spash screen does.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SplashTimeUp(object sender, EventArgs e)
{
if (formStatus == FormStatus.Opening)
{
//if the splash is opening, the opacity will increase in increments of 5 until it is fully
//shown. Once it is fully opacic, it sets the status flag to open.
if (base.MainForm.Opacity < 1)
base.MainForm.Opacity += .05;
else
formStatus = FormStatus.Open;
}
else if (formStatus == FormStatus.Closing)
{
//if the splash is closing, the opacity will decrease in increments of 5 until it is fully
//hidden. Once it is fully transparent, it sets the status flag to closed.
if (base.MainForm.Opacity > .10)
{
base.MainForm.Opacity -= .05;
splashTimer.Interval = fadeInterval;
}
else
{
formStatus = FormStatus.Closed;
}
}
else if (formStatus == FormStatus.Open)
{
//Once the splash is open and fully shown, the timer interval is set to the splash delay setting,
//which is defaulted to 2 seconds, and then sets the status flag depending on if the user
//wants to just close the splash or fade it out.
splashTimer.Interval = showSplashInterval;
if (doFadeClose)
formStatus = FormStatus.Closing;
else
formStatus = FormStatus.Closed;
}
else if(formStatus == FormStatus.Closed)
{
//This is called if the splash is ready to be closed. In order to do this
//we first dispose of the timer (we dont need any leaks do we?) and then
//call the base.MainForm.Close function which will triger the MainFormClosed event
//that we overrode so we can set the Application Context's main form as the
//main form the user passed into the constructor.
splashTimer.Enabled = false;
splashTimer.Dispose();
base.MainForm.Close();
}
}
/// <summary>
/// Normaly, if not overridden, this event will call ExitThreadCore function.
/// We have overridden it to catch when the splash form has closed. When the
/// spash form closes, we want to set the main form passed in the contructor to
/// the base.MainForm property. when the main form closes, this override will be
/// called again, and we just pass the call onto the base.OnMainFormClosed, with will
/// tells the Application object to terminate the UI thread.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected override void OnMainFormClosed(object sender, EventArgs e)
{
if (sender is Splash)
{
base.MainForm = this.mainForm;
base.MainForm.Show();
}
else if (sender is frmMain)
{
base.OnMainFormClosed(sender, e);
}
}
#region Public Properties
/// <summary>
/// Bool that determines if spash form will fade up from 0% opacity to 100% when
/// the spash form opens. Default is false.
/// </summary>
public bool DoFadeOpen
{
set
{
if (value == true)
{
base.MainForm.Opacity = 0;
formStatus = FormStatus.Opening;
}
}
}
/// <summary>
/// Bool that determines if spash form will fade down from 100% opacity to 0% when
/// the spash form closes. Default is false.
/// </summary>
public bool DoFadeClose
{
set
{
doFadeClose = value;
}
}
/// <summary>
/// This sets how long the spash form will show once it is at 100% opacity. Default is 2 seconds.
/// </summary>
public int SecondsSplashShown
{
set
{
showSplashInterval = value * 1000;
}
}
#endregion Public Properties
}
在主函数中依次调用两个窗体frmMain和Splash,
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Process instance = RunningInstance();
if (instance == null)
{
SplashFadeAppContext splashContext = new SplashFadeAppContext(new frmMain(), new Splash());
Application.Run(splashContext);
}
}
至于Splash只是先出现的窗体,里面的代码就可以自已写了
private void Splash_Load(object sender, EventArgs e)
{
timer1.Interval = 1000;
timer2.Interval = 80;
timer1.Enabled = true;
timer2.Enabled = true;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |