C# 启动屏幕在关闭前向上移动
在C#中,我的启动屏幕有问题。当该关闭时,主Form1出现,它移到Form1的右上角。然后它就消失了。我以前从未遇到过这种情况,而且几乎没有办法解决它。我希望启动屏幕在中央屏幕中消失,而不是移到打开窗体1的上角。代码如下:C# 启动屏幕在关闭前向上移动,c#,winforms,splash-screen,C#,Winforms,Splash Screen,在C#中,我的启动屏幕有问题。当该关闭时,主Form1出现,它移到Form1的右上角。然后它就消失了。我以前从未遇到过这种情况,而且几乎没有办法解决它。我希望启动屏幕在中央屏幕中消失,而不是移到打开窗体1的上角。代码如下: public Form1() { Splash mySplash = new Splash(); mySplash.TotalValue = 7; //or however many steps you must complete mySplash.S
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;
更精简的列表仅显示更相关的代码:
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中的多线程处理内容中看到,您似乎试图使启动屏幕运行更平稳,并且可能在更新表单属性时遇到错误,而不是从创建它的线程更新表单属性。
解决方法:
-调用(看起来很复杂,但实际上没有那么糟糕)
-启动线程中的250ms计时器,从实例变量/属性中获取命令(hackish) 因此,无论如何,进度条都必须设法自我更新。如果您遇到问题,可以在进度设置程序中输入me.refresh。或更新pb后的doevents,只有当存在任何可见表单且没有控件可引发您可能正在其上处理的事件(例如,单击)时,才应使用它,因为这将导致重新进入 但你不是这么问的。很抱歉要修复此问题,请在关闭/处理飞溅之前添加此代码
MySplash.Visible = False
System.Windows.Forms.Application.DoEvents
如果我不能测试它来证明它,请原谅我,我缺少一个最简单的简化案例screnario,它显示了要加载和测试的问题(提示-在发布代码时一定要这样做,50%的时候你会发现错误在做,另外50%的时候你会让人们给你答案,因为很多人不会读过两屏代码)。我也不能保证应用程序不会出现在屏幕上另一个窗口下的问题,但你也没有问过;-) Splash中的代码是什么?你是在玩弄尺寸还是位置?你的主窗体的代码太多了(你认为哪些是相关的?),而你的SplashScreen上没有代码。问题可能隐藏在.Designer.cs中,不受意外属性设置的影响。它在单独的线程上运行,并填充进度表。但不会发生尺寸变化或位置变化。如果您想查看,我可以显示代码。启动窗体设置为的
StartPosition
属性是什么?StartPosition=CenterScreen我尝试手动设置它,但它总是在关闭前滑到窗体1的上角!我的其他启动屏幕都没有这样做(在其他程序上)。我很高兴看到你的回复,也很享受你的讨论。我希望你的建议能解决这个问题,但没有。飞溅物无情地移动到Form1的右上角,然后关闭并显示Form1。我已经放弃了,用户可能会抱怨,也可能不会抱怨。但我急切地希望最终能解决这个问题。任何其他想法都将不胜感激。您可以尝试其他一些技巧:-将隐藏向上移动一个进度条步骤或2-而不是隐藏,将宽度/高度设置为1-将WindowsState属性设置为最小化-以找出导致移动的原因:将事件处理程序添加到表单的OnMove事件。添加关键字“stop”。这就像代码中的断点。点击时,查看顶部/左侧是否不同。查看调用堆栈(调试/windows)。您可以看到调用您的函数,然后双击它们以转到那里并查看发生了什么。这个技术救了我很多次!谢谢你的建议。我认为OnMove事件可能是一个很好的检查。现在无法尝试,但午餐后会立即尝试。现在遇到了另一个问题。FastAI-谢谢你的建议!我将Splash设置为运行最小化,这导致在“Form1.ActiveForm.SetDesktopLocation(588,312);”处发生错误,该代码原本用于Form1,但Splash正在接收该代码,无法定位到Form1位置588312(不应该,但确实如此)。谢谢你的建议。问题解决了。
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);
}
MySplash.Visible = False
System.Windows.Forms.Application.DoEvents