0

我正在使用 Visual Studio 2013 创建 Visual C# Windows 窗体应用程序,但我没有使用设计器来设置表单。

我有一个我认为是一个简单的计时器,它应该暂停我的应用程序几秒钟,以允许在用户看到和访问菜单之前显示启动屏幕。但是当我运行调试时,应用程序不会出现在屏幕上,直到计时器完成,然后它才跳过启动屏幕。我已经研究了做计时器的不同方法,并试图在这方面寻求帮助几个小时,但找不到让它工作的方法。我猜我错过了一些非常明显的东西,但作为一个新手,我无法发现它。

任何帮助将不胜感激。

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();
        }

    }

}
4

1 回答 1

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();
}
于 2013-11-13T09:56:35.890 回答