我正在使用Visual Studio 2013创建Visual C#Windows窗体应用程序,而我没有使用Designer来设置窗体。
我认为这是一个简单的计时器,应暂停我的应用程序几秒钟,以便在用户可以看到和访问菜单之前显示启动画面。但是当我运行调试时,应用程序不会出现在屏幕上,直到计时器完成,然后它跳过启动屏幕。我已经看了不同的方法来做计时器,并试图寻找这方面的帮助几个小时,但找不到让它工作的方法。我猜我错过了一些非常明显的东西,但作为一个新手,我无法发现它。
非常感谢任何帮助。
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Timers;
namespace SimpleForm
{
public class TheForm : Form
{
private MenuStrip menuStrip;
private MainMenu menuMain;
private MenuItem menuBlockFile;
private MenuItem menuBlockOthers;
private MenuItem menuItemExit;
private MenuItem menuItemHints;
static Bitmap imgIntroBg = null;
static Bitmap imgMenuBg = null;
private Panel pnlIntro;
private Panel pnlMenu;
static int counter = 1;
static System.Timers.Timer timer;
public TheForm()
{
FormInitialize();
MenuInitialize();
introDisplay();
MenuDisplay(true);
}
private void FormInitialize()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Game));
this.SuspendLayout();
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(714, 462);
this.FormBorderStyle = FormBorderStyle.FixedDialog;
this.Name = "SimpleForm";
this.Text = "Simple Form";
ControlBox = false;
BackColor = System.Drawing.Color.Black;
StartPosition = FormStartPosition.CenterScreen;
this.ResumeLayout(false);
}
private void MenuInitialize()
{
this.SuspendLayout();
this.menuStrip = new MenuStrip();
this.menuMain = new MainMenu();
this.menuBlockFile = new MenuItem();
this.menuBlockOthers = new MenuItem();
this.menuItemExit = new MenuItem();
this.menuItemHints = new MenuItem();
this.menuMain.MenuItems.AddRange(new MenuItem[] {
this.menuBlockFile,
this.menuBlockOthers});
this.menuBlockFile.Index = 0;
this.menuBlockFile.MenuItems.AddRange(new MenuItem[] {
this.menuItemExit
} );
this.menuBlockFile.Text = "File";
this.menuBlockOthers.Index = 1;
this.menuBlockOthers.MenuItems.AddRange(new MenuItem[] {
this.menuItemHints
});
this.menuBlockOthers.Text = "Others";
this.menuItemExit.Index = 0;
this.menuItemExit.Text = "Exit";
this.menuItemExit.Click += new System.EventHandler(this.menuItemExit_Click);
this.menuItemHints.Checked = true;
this.menuItemHints.Index = 0;
this.menuItemHints.Text = "Temp";
this.menuItemHints.Click += new System.EventHandler(this.menuItemExit_Click);
this.Menu = this.menuMain;
imgMenuBg = new Bitmap("graphics/layout/menubg.png");
pnlMenu = new Panel();
pnlMenu.Name = "pnlMenu";
pnlMenu.Location = new Point(0, 0);
pnlMenu.Width = 714;
pnlMenu.Height = 462;
pnlMenu.BackgroundImage = imgMenuBg;
MenuDisplay(false);
this.ResumeLayout(false);
}
private void MenuDisplay(bool display)
{
this.menuBlockFile.Visible = display;
this.menuBlockOthers.Visible = display;
if (display == true) {
Controls.Add(pnlMenu);
} else {
Controls.Remove(pnlMenu);
}
}
private void introDisplay()
{
timer = new System.Timers.Timer();
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
timer.Interval = 5*1000;
timer.Enabled = true;
imgIntroBg = new Bitmap("graphics/layout/Introbg.png");
pnlIntro = new Panel();
pnlIntro.Name = "pnlIntro";
pnlIntro.Location = new Point(0, 0);
pnlIntro.Width = 714;
pnlIntro.Height = 482;
pnlIntro.BackgroundImage = imgIntroBg;
timer.Start();
Controls.Add(pnlIntro);
while (counter != 0)
{
}
Controls.Remove(pnlIntro);
}
private static void timer_Elapsed(object source, ElapsedEventArgs e)
{
counter = 0;
timer.Stop();
}
private void menuItemExit_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
}
}
答案 0 :(得分:4)
while(counter != 0)
部分不是正确的事情。计时器完成后将触发timer_Elapsed
回调,因此您添加图像(Controls.Add(plnIntro);
),然后等待计时器的回调触发。那是它完成的时候。此时,只需删除plnIntro
即可按预期执行。
您不必使用while
循环锁定该线程,以使其正常工作。
尝试删除:
while (counter != 0)
{
}
Controls.Remove(pnlIntro);
并将Controls.Remove(pnlIntro);
放入计时器回调:
private void timer_Elapsed(object source, ElapsedEventArgs e)
{
Controls.Remove(pnlIntro);
timer.Stop();
}