c# - 启动画面在关闭前向上移动

标签 c# winforms splash-screen

在 C# 中,我遇到了闪屏问题。当关闭时主 Form1 出现时,它移到 Form1 的右上角。然后它消失了。我以前从未发生过这种情况,并且几乎没有解决它的想法。我希望启动画面在中央屏幕中消失,而不是移到打开的 Form1 的上角。这是代码:

public Form1() {
    Splash mySplash = new Splash();
    mySplash.TotalValue = 7; //or however many steps you must complete
    mySplash.Show();
    mySplash.Update();
    InitializeComponent();

    //--<begin>--------------
    this.Hide();
    this.WindowState = FormWindowState.Normal;
    mySplash.Progress++;
    printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage);
    printBOM.PrintPage += new PrintPageEventHandler(printBOM_PrintPage);
    printList.PrintPage += new PrintPageEventHandler(printList_PrintPage);
    mySplash.Progress++;

    // using old Kodak Imaging OCX !
    axImgEdit1.Image = "\\\\Netstore\\eng_share\\EView\\BOB-eView9.tif";
    axImgEdit1.DisplayScaleAlgorithm = 
        ImgeditLibCtl.DisplayScaleConstants.wiScaleOptimize;
    axImgEdit1.FitTo(0);
    axImgEdit1.Display();
    mySplash.Progress++;

    //~~~~~~~~~~~~~~~~~~~~Getting printer info~~~~~~~~~~~~~~~~~~~~~~~~~

    List<Win32_Printer> printerList = Win32_Printer.GetList();
    int i = 0;
    foreach (Win32_Printer printer in printerList) {
        prnName = printer.Name;
        prnPort = printer.PortName;
        prnDriver = printer.DriverName;

        if (i == 0) {
            prnNameString = prnName;
            prnDriverString = prnDriver;
            prnPortString = prnPort;
        }

        else {
            prnNameString += "," + prnName;
            prnDriverString += "," + prnDriver;
            prnPortString += "," + prnPort;
        }

        i++;
    }

    mySplash.Progress++;
    EViewMethods.defaultPrn[0] = Settings.Default.DefaultPrinter; //defaultPrn[] is 
    //string array holding the default printer name, driver and port

    EViewMethods.defaultPrn[1] = Settings.Default.DefaultPrinterDriver;
    EViewMethods.defaultPrn[2] = Settings.Default.DefaultPrinterPort;

    //making this printer the system default printer
    object printerName = Settings.Default.DefaultPrinter;
    ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM 
        Win32_Printer");
    ManagementObjectCollection collection = searcher.Get();

    foreach (ManagementObject currentObject in collection) {
        if (currentObject["name"].ToString() == printerName.ToString()) {
            currentObject.InvokeMethod("SetDefaultPrinter", new object[] { printerName 
        });
    }
}

mySplash.Progress++;
EViewMethods.reCenterEVafterDwgClose = Settings.Default.ReCenterEVafterDwgClose;
if (Settings.Default.ReCenterEVafterDwgClose == true)
    recenterEViewAfterDrawingViewerClosesToolStripMenuItem.Checked = true;

else
    recenterEViewAfterDrawingViewerClosesToolStripMenuItem.Checked = false;

    //-------------------------------------------------------
    EViewMethods.screenBehavior = Settings.Default.ViewStyle;
    normalToolStripMenuItem.Checked = false;
    clearViewToolStripMenuItem.Checked = false;
    clearviewDULevLRToolStripMenuItem.Checked = false;
    clearviewdULevLLToolStripMenuItem.Checked = false;
    clearviewdURevULToolStripMenuItem.Checked = false;
    clearviewdURevLLToolStripMenuItem.Checked = false;
    clearviewdURevLRToolStripMenuItem.Checked = false;
    smallScreenToolStripMenuItem.Checked = false;

    switch (EViewMethods.screenBehavior) {
        case "Normal":
            normalToolStripMenuItem.Checked = true;
            Settings.Default.FormViewDwgLocation = new Point(0, 0);
            //Settings.Default.FormEviewLocation = new 
            //Point(EViewMethods.form1CenterLeft, EViewMethods.form1CenterTop);
            Settings.Default.FormEviewLocation = new Point(588, 312);
            Settings.Default.FormECNLocation = new Point(EViewMethods.formECNLeft, 
                EViewMethods.formECNTop);
            //EViewMethods.reCenterEVafterDwgClose = false;
            Form1.ActiveForm.SetDesktopLocation(588, 312);

        break;

        case "Clearview-dULevUR":
            clearViewToolStripMenuItem.Checked = true;
            Settings.Default.FormViewDwgLocation = new Point(0, 0);
            Settings.Default.FormEviewLocation = new Point(1180, 0);
            Settings.Default.FormECNLocation = new Point(EViewMethods.formECNLeft, 
                EViewMethods.formECNTop);
            //EViewMethods.reCenterEVafterDwgClose = true;
            Form1.ActiveForm.SetDesktopLocation(588, 312);
        break;

        case "Clearview-dULevLR":
            clearviewDULevLRToolStripMenuItem.Checked = true;
            Settings.Default.FormViewDwgLocation = new Point(0, 0);
            Settings.Default.FormEviewLocation = new Point(1180, 622);
            Settings.Default.FormECNLocation = new Point(EViewMethods.formECNLeft, 
                EViewMethods.formECNTop);
            //EViewMethods.reCenterEVafterDwgClose = true;
            Form1.ActiveForm.SetDesktopLocation(588, 312);
        break;

        case "Clearview-dULevLL":
            clearviewdULevLLToolStripMenuItem.Checked = true;
            Settings.Default.FormViewDwgLocation = new Point(0, 0);
            Settings.Default.FormEviewLocation = new Point(0, 622);
            Settings.Default.FormECNLocation = new Point(EViewMethods.formECNLeft, 
                EViewMethods.formECNTop);
            //EViewMethods.reCenterEVafterDwgClose = true;
            Form1.ActiveForm.SetDesktopLocation(588, 312);
        break;

        case "Clearview-dURevUL":
            clearviewdURevULToolStripMenuItem.Checked = true;
            Settings.Default.FormViewDwgLocation = new Point(825, 0);
            Settings.Default.FormEviewLocation = new Point(0, 0);
            Settings.Default.FormECNLocation = new Point(EViewMethods.formECNLeft, 
                EViewMethods.formECNTop);
            //EViewMethods.reCenterEVafterDwgClose = true;
            Form1.ActiveForm.SetDesktopLocation(588, 312);
        break;

        case "Clearview-dURevLL":
            clearviewdURevLLToolStripMenuItem.Checked = true;
            Settings.Default.FormViewDwgLocation = new Point(825, 0);
            Settings.Default.FormEviewLocation = new Point(0, 622);
            Settings.Default.FormECNLocation = new Point(EViewMethods.formECNLeft, 
                EViewMethods.formECNTop);
            //EViewMethods.reCenterEVafterDwgClose = true;
                Form1.ActiveForm.SetDesktopLocation(588, 312);
        break;

        case "Clearview-dURevLR":
            clearviewdURevLRToolStripMenuItem.Checked = true;
            Settings.Default.FormViewDwgLocation = new Point(825, 0);
            Settings.Default.FormEviewLocation = new Point(1180, 622);
            Settings.Default.FormECNLocation = new Point(EViewMethods.formECNLeft, 
                EViewMethods.formECNTop);
            //EViewMethods.reCenterEVafterDwgClose = true;
            Form1.ActiveForm.SetDesktopLocation(588, 312);
        break;

        case "Small-screen":
            //clearviewdURevLRToolStripMenuItem.Checked = true;
            smallScreenToolStripMenuItem.Checked = true;
            Settings.Default.FormViewDwgLocation = new Point(0, 0);
            Settings.Default.FormEviewLocation = new Point(0, 0);
            Settings.Default.FormECNLocation = new Point(0, 0);
            //EViewMethods.reCenterEVafterDwgClose = true;
            Form1.ActiveForm.SetDesktopLocation(588, 312);
        break;
    }

    //Form1.ActiveForm.SetDesktopLocation(588, 312); //all screen behavior mode will 
    //begin centered on the screen
    EViewMethods.eviewUserPrefLocation = Settings.Default.FormEviewLocation;
    //-------------------------------------------------------

    EViewMethods.syncListToDwgNum = Settings.Default.SyncListDwgNum;

    if (EViewMethods.syncListToDwgNum == true)
        synchronizeListToActiveDwgToolStripMenuItem.Checked = true;
    else
        synchronizeListToActiveDwgToolStripMenuItem.Checked = false;
        toolStripStatusLabel1.Text = "";
        toolStripStatusLabel2.Text = Settings.Default.ViewStyle;
        toolStripStatusLabel3.Text = Settings.Default.DefaultPrinter;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~

    Assembly asm = Assembly.GetExecutingAssembly();
    AssemblyName asmName = asm.GetName();
    EViewMethods.eviewVersion = asmName.Version.ToString();
    radioPartInfo.Checked = true;
    disableAllSearch();
    EViewMethods.userName = Environment.UserName;
    EViewMethods.openConnection();
    mySplash.Progress++;
    EViewMethods.loadFavorites(listFavorites);
    mySplash.Close();
    mySplash.Dispose();
    this.Show();
    this.ActiveControl = comboEntry;

}

更简洁的列表只显示更相关的代码:

public Form1() {
    Splash mySplash = new Splash();
    mySplash.TotalValue = 7; //or however many steps you must complete
    mySplash.Show();
    mySplash.Update();

    InitializeComponent();

    //--<begin>--------------
    this.Hide();
    this.WindowState = FormWindowState.Normal;
    mySplash.Progress++;

    ...

    mySplash.Progress++;

    ...

    mySplash.Progress++;

    //~~~~~~~~~~~~~~~~~~~~Getting printer info~~~~~~~~~~~~~~~~~~~~~~~~~
    List<Win32_Printer> printerList = Win32_Printer.GetList();

    ...

    mySplash.Progress++;

    ...

    mySplash.Progress++;

    EViewMethods.reCenterEVafterDwgClose = Settings.Default.ReCenterEVafterDwgClose;
    if (Settings.Default.ReCenterEVafterDwgClose == true)
        recenterEViewAfterDrawingViewerClosesToolStripMenuItem.Checked = true;
    else
        recenterEViewAfterDrawingViewerClosesToolStripMenuItem.Checked = false;

    //-------------------------------------------------------
    EViewMethods.screenBehavior = Settings.Default.ViewStyle;

    ...

    //Form1.ActiveForm.SetDesktopLocation(588, 312); //all screen behavior mode will 
    begin centered on the screen
    EViewMethods.eviewUserPrefLocation = Settings.Default.FormEviewLocation;

    //-------------------------------------------------------

    EViewMethods.openConnection();
    mySplash.Progress++;

    ...

    mySplash.Close();
    mySplash.Dispose();

    this.Show();
    this.ActiveControl = comboEntry;
}

启动代码:

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;
using System.Reflection;
using System.Threading;

namespace EView2 {
    public partial class Splash : Form {
        static int total = 100;
        static int currPos = 0;
        static string text;

        // Threading
        static Splash ms_frmSplash = null;
        static Thread ms_oThread = null;

        public Splash() {
            InitializeComponent();

            this.labelVersion.Text = 
                Assembly.GetExecutingAssembly().GetName().Version.ToString();
        }

        public int TotalValue {
            get {
                return total;
            }

            set {
                total = value;
                progressBar1.Maximum = total;
            }
        }

        public int Progress {
            get {
                return currPos;
            }

            set {
                currPos = value;

                try {
                    progressBar1.Value = currPos;
                }

                catch { 
                }
            }
        }

        static public void ShowSplashScreen() {
            // Make sure it's only launched once.
            if (ms_frmSplash != null)
                return;
                ms_oThread = new Thread(new ThreadStart(Splash.ShowForm));
                ms_oThread.IsBackground = true;
                ms_oThread.Start();
            }

            // A property returning the splash screen instance
            static public Splash SplashForm {
                get {
                    return ms_frmSplash;
                }
            }

            // A private entry point for the thread.
            static private void ShowForm() {
            ms_frmSplash = new Splash();
            Application.Run(ms_frmSplash);
        }

最佳答案

我从 splash 中的多线程内容中看到,您似乎试图让启动画面运行得更流畅,并且可能遇到了关于从与创建它的线程不同的线程更新表单属性的错误。
解决方法:
- 调用(看起来像复杂的代码但实际上还不错)
- 启动线程中的 250 毫秒计时器从实例变量/属性获取命令(hackish)

所以一定是进度条设法自行更新了。如果您遇到麻烦,您总是可以在进度 setter 中放入 me.refresh。或者在更新 pb 之后执行事件,这实际上应该只在存在任何可见表单且没有控件可以引发您可能正在处理的事件(例如,单击)时使用,因为这会导致重入。

但这不是你问的。对不起。要修复不重写的问题,请在关闭/处理飞溅之前添加此代码

MySplash.Visible = False
System.Windows.Forms.Application.DoEvents

如果我不能测试它来证明它,请原谅我,我缺少一个最简单的 - 减少案例的场景,它显示了加载和测试的问题(提示 - 在发布代码时总是这样做,50%当你发现错误时,另外 50% 的人实际上会给你答案,因为许多人不会阅读超过 2 屏的代码)。我也不保证这不会导致应用出现在屏幕上另一个窗口下的问题,但你也没有问过这个问题;-)

关于c# - 启动画面在关闭前向上移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3053799/

相关文章:

c# - 如何使用 ProgressBar、Progress 和 IProgress<int> 显示任务的进度

android - 启动画面出现在android之前的黑屏

javascript - api调用完成后 ionic 隐藏启动画面

android - PhoneGap 1.8.1 - 启动画面不起作用?

c# - 无法加载 WCF 服务中的相关实体

c# - 从存储过程返回多个表

c# - 在 C# 中解析和分析 C++ 代码

C# 子窗体在主窗体上具有灰色效果

c# - 引发异常或返回错误 - 更好的方法是什么?

c# - 如何将具有描述属性的枚举转换为字典?