2010-06-16 56 views
2

在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); 
     } 
+2

Splash中的代碼是做什麼的?你在玩尺寸還是位置? – 2010-06-16 13:53:12

+0

你的MainForm的代碼太多了(你認爲什麼是相關的?)並且沒有來自SplashScreen的代碼。而這個問題可能會隱藏在.Designer.cs中的一個意外屬性設置中。 – 2010-06-16 14:14:17

+0

它在一個單獨的線程上運行並填充一個進度表。但沒有尺寸變化或位置發生。我可以顯示代碼,如果你想看到它。 – 2010-06-16 14:21:44

回答

1

我從飛濺的多線程內容中看到,它看起來像您試圖讓啓動畫面更流暢地運行,並可能遇到有關從不同於創建它的線程更新表單屬性的錯誤。
方式圍繞:
- 調用(貌似複雜的代碼,但真的不是那麼糟糕)
- 在初始線程從實例變量/屬性(hackish的)

需要命令250ms的定時器,它必須是進度條無論如何設法更新自己。如果你遇到麻煩,你可以在進度制定者中加入一個新的。或更新pb後的事件,只有當有任何可見的表單沒有可引發事件(例如,點擊)的控件時纔會使用,因爲這會導致重入。

但這不是你問的。抱歉。爲了解決這個問題W/O重新書寫,在收盤前添加以下代碼/處置飛濺

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

原諒我,如果我不能測試來證明這一點的,我缺乏一個最簡單的 - reduced-這種情況表明加載和測試的問題(提示 - 發佈代碼時總是這樣做,50%的時候你發現錯誤在做,另外50%你會讓人們給你答案,不會閱讀過去的兩個代碼屏幕)。我也不會承諾,不會導致出現在屏幕上另一個窗口下的應用程序出現問題,但你也沒有問過這個問題;-)

+0

我很高興看到您的回覆,並且非常享受您的討論。我希望你的建議能夠解決這個問題,但事實並非如此。在它關閉並且Form1出現之前,飛濺無情地移動到Form1的右上角。我只是放棄了它,用戶可能會或可能不會抱怨。但我急切希望最終解決它。任何其他想法將不勝感激。 – 2010-06-17 20:11:57

+0

其他幾個黑客可以嘗試: - 移動隱藏進度條步驟或2 - 取代隱藏,將寬度/高度設置爲1 -set將windowstate屬性最小化 - 要弄清楚導致移動的原因: 將事件處理程序添加到窗體的OnMove事件。添加關鍵字「停止」。這就像代碼中的斷點。當它被擊中時,看看頂部/左邊是不同的。查看調用堆棧(調試/窗口)。你可以看到調用你的函數,並雙擊它們去看看發生了什麼。這項技術很多時候都挽救了我的屁股! – FastAl 2010-06-18 14:27:16

+0

感謝您的建議。我認爲OnMove事件可能是一個很好的檢查。現在不能嘗試,但午餐後會跳上去。現在卡住了另一個問題。 – 2010-06-18 14:54:54