关闭前启动画面向上移动

发布于 2024-09-06 13:00:39 字数 12310 浏览 2 评论 0原文

在 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;
}

Splash 代码:

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

In C# I am having a problem with the splash screen. When it is time to close and the main Form1 appears, it moves over to the upper right corner of Form1. It then disappears. I have never had this occur before and have just about run out of ideas to fix it. I want the splash screen to disappear in the center screen and not move over to the upper corner of the opening Form1. Here is the code:

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;

}

More condensed listing showing only more pertinent code:

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

Code for Splash:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海螺姑娘 2024-09-13 13:00:39

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

所以进度条无论如何都必须设法自我更新。如果您遇到问题,可以随时在进度设置器中放入 me.refresh 。或者在更新 pb 后执行 doevents,这实际上应该只在存在任何可见表单且没有控件可以引发您可能正在处理的事件(例如,单击)时使用,因为这将导致重入。

但这不是你问的。对不起。要解决此问题,无需重写,请在关闭/处理启动画面之前添加此代码,

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

如果我无法测试它来证明这一点,请原谅我,我缺少一个最简单的简化案例场景,该场景将问题表现出来加载并测试(提示 - 在发布代码时始终这样做,50% 的时间你会发现 bug 这样做,另外 50% 你实际上会得到人们给你答案,因为很多人不会阅读过去的 2 个屏幕代码)。我也不保证这不会导致应用程序出现在屏幕上另一个窗口下的问题,但你也没有问过这个问题;-)

I see from the multithreading stuff in splash that it looks like you tried to make the splash screen operate more smoothly and probably ran into the error about updating form properties from a different thread than the one that created it.
Ways around that:
- Invoke (looks like complicated code but really not that bad)
- 250ms timer in the splash thread that takes commands from instance variables / properties (hackish)

So it must be that the progress bar manages to update itself anyway. You could alway put in a me.refresh in the progress setter if you're having trouble with that. Or doevents after updating the pb, which really should only be used when there is any visible form with no controls that can raise events (e.g., click) which you might be handling on it, as that will cause reentrancy.

But that's not what you asked. Sorry. To fix that w/o re-writing, add this code before the close/dispose of the splash

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

Forgive me if I can't test it to prove it, I'm lacking a simplest - reduced-case screnario which manifests the problem to load up and test with (tip - always do that when posting code, 50% of the time you find the bug doing it, the other 50% you'll actually get people giving you answers as many won't read past 2 screens of code). I am also not promising that won't cause problems with the app appearing under another window on the screen, but you didn't ask about that either ;-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文