12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208 |
- using PaintDotNet.Actions;
- using PaintDotNet.Base.CommTool;
- using PaintDotNet.SystemLayer;
- using StageController;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Drawing;
- using System.IO;
- using System.Threading;
- using System.Windows.Forms;
- using TUCamera;
- namespace PaintDotNet
- {
- internal sealed class MainForm : PdnBaseForm
- {
- private AppWorkspace appWorkspace;
- private FloatingToolForm[] floaters;
- private System.Windows.Forms.Timer floaterOpacityTimer;
- private System.Windows.Forms.Timer deferredInitializationTimer;
- private IContainer components;
- private bool killAfterInit = false;
- private SplashForm splashForm = null;
- private SingleInstanceManager singleInstanceManager = null;
- private List<string> queuedInstanceMessages = new List<string>();
- public SingleInstanceManager SingleInstanceManager
- {
- get
- {
- return this.singleInstanceManager;
- }
- set
- {
- if (this.singleInstanceManager != null)
- {
- this.singleInstanceManager.InstanceMessageReceived -= new EventHandler(SingleInstanceManager_InstanceMessageReceived);
- this.singleInstanceManager.SetWindow(null);
- }
- this.singleInstanceManager = value;
- if (this.singleInstanceManager != null)
- {
- this.singleInstanceManager.SetWindow(this);
- this.singleInstanceManager.InstanceMessageReceived += new EventHandler(SingleInstanceManager_InstanceMessageReceived);
- }
- }
- }
- private void SingleInstanceManager_InstanceMessageReceived(object sender, EventArgs e)
- {
- BeginInvoke(new Procedure(ProcessQueuedInstanceMessages), null);
- }
- public MainForm() : this(new string[0])
- {
- }
- public const int WM_DEVICECHANGE = 0x219;
- public const int DBT_DEVNODES_CHANGED = 0x0007;
- protected override void WndProc(ref Message m)
- {
- if (this.singleInstanceManager != null)
- {
- this.singleInstanceManager.FilterMessage(ref m);
- }
- if (m.Msg == WM_DEVICECHANGE) //设备改变消息
- {
- switch (m.WParam.ToInt32())
- {
- case DBT_DEVNODES_CHANGED:
- if (TUCameraManager.GetInstance().CameraCount > 0)
- {
- Startup.instance.CheckCameraSN();
- if (!Startup.cameraSNLegal)
- {
- TUCameraManager.GetInstance().UnInitManager();
- MessageBox.Show("相机已经断开");
- }
- }
- else
- {
- TUCameraManager.GetInstance().UnInitManager();
- TUCameraManager.GetInstance().InitManager();
- Startup.instance.CheckCameraSN();
- if (Startup.cameraSNLegal)
- MessageBox.Show("相机已经重连");
- }
- if (!AxisController.GetInstance().IsOpen)
- AxisController.GetInstance().Open();
- break;
- }
- }
- base.WndProc(ref m);
- }
- private enum ArgumentAction
- {
- Open,
- OpenUntitled,
- NoOp
- }
- private bool SplitMessage(string message, out ArgumentAction action, out string actionParm)
- {
- if (message.Length == 0)
- {
- action = ArgumentAction.NoOp;
- actionParm = null;
- return false;
- }
- const string untitledPrefix = "untitled:";
- if (message.IndexOf(untitledPrefix) == 0)
- {
- action = ArgumentAction.OpenUntitled;
- actionParm = message.Substring(untitledPrefix.Length);
- return true;
- }
- action = ArgumentAction.Open;
- actionParm = message;
- return true;
- }
- private bool ProcessMessage(string message)
- {
- if (IsDisposed)
- {
- return false;
- }
- ArgumentAction action;
- string actionParm;
- bool result;
- result = SplitMessage(message, out action, out actionParm);
- if (!result)
- {
- return true;
- }
- switch (action)
- {
- case ArgumentAction.NoOp:
- result = true;
- break;
- case ArgumentAction.Open:
- Activate();
- if (IsCurrentModalForm && Enabled)
- {
- result = this.appWorkspace.OpenFileInNewWorkspace(actionParm);
- }
- break;
- case ArgumentAction.OpenUntitled:
- Activate();
- if (!string.IsNullOrEmpty(actionParm) && IsCurrentModalForm && Enabled)
- {
- result = this.appWorkspace.OpenFileInNewWorkspace(actionParm, false);
- if (result)
- {
- this.appWorkspace.ActiveDocumentWorkspace.SetDocumentSaveOptions(null, null, null);
- this.appWorkspace.ActiveDocumentWorkspace.Document.Dirty = true;
- }
- }
- break;
- default:
- throw new InvalidEnumArgumentException();
- }
- return result;
- }
- private void ProcessQueuedInstanceMessages()
- {
- if (IsDisposed)
- {
- return;
- }
- if (this.splashForm != null)
- {
- this.splashForm.Close();
- this.splashForm.Dispose();
- this.splashForm = null;
- }
- if (IsHandleCreated &&
- //!PdnInfo.IsExpired &&
- this.singleInstanceManager != null)
- {
- string[] messages1 = this.singleInstanceManager.GetPendingInstanceMessages();
- string[] messages2 = this.queuedInstanceMessages.ToArray();
- this.queuedInstanceMessages.Clear();
- string[] messages = new string[messages1.Length + messages2.Length];
- for (int i = 0; i < messages1.Length; ++i)
- {
- messages[i] = messages1[i];
- }
- for (int i = 0; i < messages2.Length; ++i)
- {
- messages[i + messages1.Length] = messages2[i];
- }
- foreach (string message in messages)
- {
- bool result = ProcessMessage(message);
- if (!result)
- {
- break;
- }
- }
- }
- }
- private void Application_Idle(object sender, EventArgs e)
- {
- if (!this.IsDisposed &&
- (this.queuedInstanceMessages.Count > 0 || (this.singleInstanceManager != null && this.singleInstanceManager.AreMessagesPending)))
- {
- ProcessQueuedInstanceMessages();
- }
- }
- public MainForm(string[] args)
- {
- PdnBaseForm.xmlPath = Startup.instance.SettingPrefix;
- bool canSetCurrentDir = true;
- this.StartPosition = FormStartPosition.WindowsDefaultLocation;
- //bool splash = false;
- List<string> fileNames = new List<string>();
- // Parse command line arguments
- foreach (string argument in args)
- {
- if (0 == string.Compare(argument, "/dontForceGC"))
- {
- Utility.AllowGCFullCollect = false;
- }
- else if (0 == string.Compare(argument, "/splash", true))
- {
- //splash = true;
- }
- else if (0 == string.Compare(argument, "/test", true))
- {
- // This lets us use an alternate update manifest on the web server so that
- // we can test manifests on a small scale before "deploying" them to everybody
- PdnInfo.IsTestMode = true;
- }
- else if (0 == string.Compare(argument, "/profileStartupTimed", true))
- {
- // profileStartupTimed and profileStartupWorkingSet compete, which
- // ever is last in the args list wins.
- PdnInfo.StartupTest = StartupTestType.Timed;
- }
- else if (0 == string.Compare(argument, "/profileStartupWorkingSet", true))
- {
- // profileStartupTimed and profileStartupWorkingSet compete, which
- // ever is last in the args list wins.
- PdnInfo.StartupTest = StartupTestType.WorkingSet;
- }
- else if (argument.Length > 0 && argument[0] != '/')
- {
- //try
- //{
- // string fullPath = Path.GetFullPath(argument);
- // fileNames.Add(fullPath);
- //}
- //catch (Exception)
- //{
- // fileNames.Add(argument);
- // canSetCurrentDir = false;
- //}
- //splash = true;
- }
- }
- if (canSetCurrentDir)
- {
- try
- {
- Environment.CurrentDirectory = PdnInfo.GetApplicationDir();
- }
- catch (Exception ex)
- {
- Tracing.Ping("Exception while trying to set Environment.CurrentDirectory: " + ex.ToString());
- }
- }
- // make splash, if warranted
- //if (splash)
- if (Startup.instance.configModel.StartImgStatus == 2)
- {
- //this.splashForm = new SplashForm();
- string pt = System.Environment.CurrentDirectory + "\\Banner.jpg";
- this.splashForm = new SplashForm(pt);
- // this.splashForm = new SplashForm(Startup.instance.configModel.StartImg);
- this.splashForm.TopMost = true;
- this.splashForm.Show();
- this.splashForm.Update();
- }
- InitializeComponent();
- this.Icon = PdnInfo.AppIcon;
- // Does not load window location/state
- LoadSettings();
- //if (Startup.instance.splashForm != null && !Startup.instance.splashForm.IsDisposed)
- // Startup.instance.splashForm.Hide();
- if (this.splashForm != null && !this.splashForm.IsDisposed)
- this.splashForm.Hide();
- //恢复图片
- List<string> imageList = FileOperationHelper.RestoreImages();
- if (imageList != null && imageList.Count > 0)
- {
- if (MessageBox.Show(PdnResources.GetString("Menu.RetrievableimagdetectedReopeit.Text") + "?", PdnResources.GetString("Menu.Edit.Redo.Text"), MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
- {
- List<string> newImg = FileOperationHelper.MoveRestoreImages(imageList);
- foreach (string n in newImg)
- {
- fileNames.Add(n);
- }
- }
- }
- foreach (string fileName in fileNames)
- {
- this.queuedInstanceMessages.Add(fileName);
- }
- // 原来是创建一个空白的文件,现在不要了
- /*if (fileNames.Count == 0)
- {
- MeasurementUnit units = Document.DefaultDpuUnit;
- double dpu = Document.GetDefaultDpu(units);
- Size newSize = this.appWorkspace.GetNewDocumentSize();
- this.appWorkspace.CreateBlankDocumentInNewWorkspace(newSize, units, dpu, true);
- this.appWorkspace.ActiveDocumentWorkspace.Document.Dirty = false;
- }*/
- LoadWindowState();
- deferredInitializationTimer.Enabled = true;
- Application.Idle += new EventHandler(Application_Idle);
- this.Shown += new EventHandler(this.MainForm_Shown);
- }
- private void MainForm_Shown(object sender, EventArgs e)
- {
- this.appWorkspace.ToolBar.test1(false);
- this.appWorkspace.SetTopLeftCheckState();
- }
- protected override void OnShown(EventArgs e)
- {
- base.OnShown(e);
- /*//判断是否过期的处理
- if (PdnInfo.IsExpired)
- {
- foreach (Form form in Application.OpenForms)
- {
- form.Enabled = false;
- }
- TaskButton checkForUpdatesTB = new TaskButton(
- PdnResources.GetImageResource("Icons.MenuHelpCheckForUpdatesIcon.png").Reference,
- PdnResources.GetString("ExpiredTaskDialog.CheckForUpdatesTB.ActionText"),
- PdnResources.GetString("ExpiredTaskDialog.CheckForUpdatesTB.ExplanationText"));
- TaskButton goToWebSiteTB = new TaskButton(
- PdnResources.GetImageResource("Icons.MenuHelpPdnWebsiteIcon.png").Reference,
- PdnResources.GetString("ExpiredTaskDialog.GoToWebSiteTB.ActionText"),
- PdnResources.GetString("ExpiredTaskDialog.GoToWebSiteTB.ExplanationText"));
- TaskButton doNotCheckForUpdatesTB = new TaskButton(
- PdnResources.GetImageResource("Icons.CancelIcon.png").Reference,
- PdnResources.GetString("ExpiredTaskDialog.DoNotCheckForUpdatesTB.ActionText"),
- PdnResources.GetString("ExpiredTaskDialog.DoNotCheckForUpdatesTB.ExplanationText"));
- TaskButton[] taskButtons =
- new TaskButton[]
- {
- checkForUpdatesTB,
- goToWebSiteTB,
- doNotCheckForUpdatesTB
- };
- TaskButton clickedTB = TaskDialog.Show(
- this,
- Icon,
- PdnInfo.GetFullAppName(),
- PdnResources.GetImageResource("Icons.WarningIcon.png").Reference,
- true,
- PdnResources.GetString("ExpiredTaskDialog.InfoText"),
- taskButtons,
- checkForUpdatesTB,
- doNotCheckForUpdatesTB,
- 450,
- false,
- 0,
- out bool unuse);
- if (clickedTB == goToWebSiteTB)
- {
- PdnInfo.LaunchWebSite(this, "http://www.baidu.com");//InvariantStrings.ExpiredPage
- }
- Close();
- }*/
- }
- private void LoadWindowState()
- {
- try
- {
- FormWindowState fws = (FormWindowState)Enum.Parse(typeof(FormWindowState),
- Settings.CurrentUser.GetString(SettingNames.WindowState, WindowState.ToString()), true);
- // if the state was saved as 'minimized' then just ignore whatever was saved
- if (fws != FormWindowState.Minimized)
- {
- if (fws != FormWindowState.Maximized)
- {
- Rectangle newBounds = Rectangle.Empty;
- // Load the registry values into a rectangle so that we
- // can update the settings all at once, instead of one
- // at a time. This will make loading the size an all or
- // none operation, with no rollback necessary
- newBounds.Width = Settings.CurrentUser.GetInt32(SettingNames.Width, this.Width);
- newBounds.Height = Settings.CurrentUser.GetInt32(SettingNames.Height, this.Height);
- int left = Settings.CurrentUser.GetInt32(SettingNames.Left, this.Left);
- int top = Settings.CurrentUser.GetInt32(SettingNames.Top, this.Top);
- newBounds.Location = new Point(left, top);
- this.Bounds = newBounds;
- }
- this.WindowState = fws;
- }
- }
- catch
- {
- try
- {
- Settings.CurrentUser.Delete(
- new string[]
- {
- SettingNames.Width,
- SettingNames.Height,
- SettingNames.WindowState,
- SettingNames.Top,
- SettingNames.Left
- });
- }
- catch
- {
- // ignore errors
- }
- }
- }
- private void LoadSettings()
- {
- try
- {
- PdnBaseForm.EnableOpacity = Settings.CurrentUser.GetBoolean(SettingNames.TranslucentWindows, true);
- }
- catch (Exception ex)
- {
- Tracing.Ping("Exception in MainForm.LoadSettings:" + ex.ToString());
- try
- {
- Settings.CurrentUser.Delete(
- new string[]
- {
- SettingNames.TranslucentWindows
- });
- }
- catch
- {
- }
- }
- }
- private void SaveSettings()
- {
- Settings.CurrentUser.SetInt32(SettingNames.Width, this.Width);
- Settings.CurrentUser.SetInt32(SettingNames.Height, this.Height);
- Settings.CurrentUser.SetInt32(SettingNames.Top, this.Top);
- Settings.CurrentUser.SetInt32(SettingNames.Left, this.Left);
- Settings.CurrentUser.SetString(SettingNames.WindowState, this.WindowState.ToString());
- Settings.CurrentUser.SetBoolean(SettingNames.TranslucentWindows, PdnBaseForm.EnableOpacity);
- if (this.WindowState != FormWindowState.Minimized)
- {
- Settings.CurrentUser.SetBoolean(SettingNames.RuleListFormVisible, this.appWorkspace.Widgets.RuleListForm.Visible);
- Settings.CurrentUser.SetBoolean(SettingNames.PixelTrackingDialogVisible, this.appWorkspace.Widgets.PixelTrackingDialog.Visible);
- Settings.CurrentUser.SetBoolean(SettingNames.ImageIndexDialogVisible, this.appWorkspace.Widgets.ImageIndexDialog.Visible);
- Settings.CurrentUser.SetBoolean(SettingNames.GeometryMeasureDialogVisible, this.appWorkspace.Widgets.GeometryMeasureDialog.Visible);
- Settings.CurrentUser.SetBoolean(SettingNames.HistogramDialogVisible, this.appWorkspace.Widgets.HistogramDialog.Visible);
- //Settings.CurrentUser.SetBoolean(SettingNames.HistogramDialogVisible, this.appWorkspace.Widgets.HistogramDialog.Visible);
- //Settings.CurrentUser.SetBoolean(SettingNames.HistogramDialogVisible, this.appWorkspace.Widgets.HistogramDialog.Visible);
- Settings.CurrentUser.SetBoolean(SettingNames.WorkFlowDialogVisible, this.appWorkspace.Widgets.WorkFlowDialog.Visible);
- Settings.CurrentUser.SetBoolean(SettingNames.MeasureListDialogVisible, this.appWorkspace.Widgets.MeasureListDialog.Visible);
- Settings.CurrentUser.SetBoolean(SettingNames.LabelListDialogVisible, this.appWorkspace.Widgets.LabelListDialog.Visible);
- Settings.CurrentUser.SetBoolean(SettingNames.OpticalDensityDialogVisible, this.appWorkspace.Widgets.OpticalDensityDialog.Visible);
- Settings.CurrentUser.SetBoolean(SettingNames.ProjectEngineeringVisible, this.appWorkspace.Widgets.ProjectEngineering.Visible);
- }
- SnapManager.Save(Settings.CurrentUser);
- this.appWorkspace.SaveSettings();
- }
- protected override void OnQueryEndSession(CancelEventArgs e)
- {
- if (IsCurrentModalForm)
- {
- OnClosing(e);
- }
- else
- {
- foreach (Form form in Application.OpenForms)
- {
- PdnBaseForm asPDF = form as PdnBaseForm;
- if (asPDF != null)
- {
- asPDF.Flash();
- }
- }
- e.Cancel = true;
- }
- base.OnQueryEndSession(e);
- }
- protected override void OnClosing(CancelEventArgs e)
- {
- if (!e.Cancel)
- {
- if (this.appWorkspace != null)
- {
- if (this.appWorkspace.DocumentWorkspaces != null && this.appWorkspace.DocumentWorkspaces.Length > 0)
- {
- foreach (DocumentWorkspace document in this.appWorkspace.DocumentWorkspaces)
- {
- CloseWorkspaceAction action = new CloseWorkspaceAction();
- this.appWorkspace.PerformAction(action);
- e.Cancel = action.Cancelled;
- if (e.Cancel) break;
- }
- }
- /**
- CloseAllWorkspacesAction cawa = new CloseAllWorkspacesAction();
- this.appWorkspace.PerformAction(cawa);
- e.Cancel = cawa.Cancelled;
- **/
- }
- }
- if (!e.Cancel)
- {
- SaveSettings();
- if (this.floaters != null)
- {
- foreach (Form hideMe in this.floaters)
- {
- hideMe.Hide();
- }
- }
- this.Hide();
- if (this.queuedInstanceMessages != null)
- {
- this.queuedInstanceMessages.Clear();
- }
- SingleInstanceManager sim2 = this.singleInstanceManager;
- SingleInstanceManager = null;
- if (sim2 != null)
- {
- sim2.Dispose();
- sim2 = null;
- }
- }
- FileOperationHelper.DeleteFolder(Application.StartupPath + "\\BackupImages");//删除备份文件
- FileOperationHelper.DeleteFolder(Application.StartupPath + "\\BackupImagesTemp");//删除备份文件
- base.OnClosing(e);
- }
- protected override void OnClosed(EventArgs e)
- {
- base.OnClosed(e);
- }
- /// <summary>
- /// Clean up any resources being used.
- /// </summary>
- protected override void Dispose(bool disposing)
- {
- if (disposing)
- {
- if (this.singleInstanceManager != null)
- {
- SingleInstanceManager sim2 = this.singleInstanceManager;
- SingleInstanceManager = null;
- sim2.Dispose();
- sim2 = null;
- }
- if (this.floaterOpacityTimer != null)
- {
- this.floaterOpacityTimer.Tick -= new System.EventHandler(this.FloaterOpacityTimer_Tick);
- this.floaterOpacityTimer.Dispose();
- this.floaterOpacityTimer = null;
- }
- if (this.components != null)
- {
- this.components.Dispose();
- this.components = null;
- }
- }
- try
- {
- base.Dispose(disposing);
- }
- catch (RankException)
- {
- // System.Windows.Forms.PropertyStore
- // Discard error - bug #2746
- }
- }
- /// <summary>
- /// Required method for Designer support - do not modify
- /// the contents of this method with the code editor.
- /// </summary>
- private void InitializeComponent()
- {
- this.components = new Container();
- this.appWorkspace = new AppWorkspace(this);
- this.floaterOpacityTimer = new System.Windows.Forms.Timer(this.components);
- this.deferredInitializationTimer = new System.Windows.Forms.Timer(this.components);
- this.SuspendLayout();
- //
- // appWorkspace
- //
- this.appWorkspace.Dock = System.Windows.Forms.DockStyle.Fill;
- this.appWorkspace.Location = new System.Drawing.Point(0, 0);
- this.appWorkspace.Name = "appWorkspace";
- this.appWorkspace.Size = new System.Drawing.Size(752, 648);
- this.appWorkspace.TabIndex = 2;
- this.appWorkspace.ActiveDocumentWorkspaceChanging += new EventHandler(AppWorkspace_ActiveDocumentWorkspaceChanging);
- this.appWorkspace.ActiveDocumentWorkspaceChanged += new EventHandler(AppWorkspace_ActiveDocumentWorkspaceChanged);
- //
- // floaterOpacityTimer
- //
- this.floaterOpacityTimer.Enabled = false;
- this.floaterOpacityTimer.Interval = 25;
- this.floaterOpacityTimer.Tick += new System.EventHandler(this.FloaterOpacityTimer_Tick);
- //
- // deferredInitializationTimer
- //
- this.deferredInitializationTimer.Interval = 250;
- this.deferredInitializationTimer.Tick += new EventHandler(DeferredInitialization);
- //
- // MainForm
- //
- try
- {
- this.AllowDrop = true;
- }
- catch (InvalidOperationException)
- {
- // Discard error. See bug #2605.
- }
- this.AutoScaleDimensions = new SizeF(96F, 96F);
- this.AutoScaleMode = AutoScaleMode.Dpi;
- this.ClientSize = new Size(950, 738);
- this.Controls.Add(this.appWorkspace);
- this.Name = "MainForm";
- this.StartPosition = FormStartPosition.WindowsDefaultLocation;
- this.ForceActiveTitleBar = true;
- this.KeyPreview = true;
- this.Controls.SetChildIndex(this.appWorkspace, 0);
- this.ResumeLayout(false);
- this.PerformLayout();
- }
- private void AppWorkspace_ActiveDocumentWorkspaceChanging(object sender, EventArgs e)
- {
- if (this.appWorkspace.ActiveDocumentWorkspace != null)
- {
- this.appWorkspace.ActiveDocumentWorkspace.ScaleFactorChanged -= DocumentWorkspace_ScaleFactorChanged;
- this.appWorkspace.ActiveDocumentWorkspace.DocumentChanged -= DocumentWorkspace_DocumentChanged;
- this.appWorkspace.ActiveDocumentWorkspace.SaveOptionsChanged -= DocumentWorkspace_SaveOptionsChanged;
- }
- }
- private void AppWorkspace_ActiveDocumentWorkspaceChanged(object sender, EventArgs e)
- {
- if (this.appWorkspace.ActiveDocumentWorkspace != null)
- {
- this.appWorkspace.ActiveDocumentWorkspace.ScaleFactorChanged += DocumentWorkspace_ScaleFactorChanged;
- this.appWorkspace.ActiveDocumentWorkspace.DocumentChanged += DocumentWorkspace_DocumentChanged;
- this.appWorkspace.ActiveDocumentWorkspace.SaveOptionsChanged += DocumentWorkspace_SaveOptionsChanged;
- }
- SetTitleText();
- }
- private void DocumentWorkspace_SaveOptionsChanged(object sender, EventArgs e)
- {
- SetTitleText();
- }
- protected override void OnLoad(EventArgs e)
- {
- EnsureFormIsOnScreen();
- if (killAfterInit)
- {
- Application.Exit();
- }
- this.floaters = new FloatingToolForm[] {
- appWorkspace.Widgets.RuleListForm,
- appWorkspace.Widgets.PixelTrackingDialog,
- appWorkspace.Widgets.ImageIndexDialog,
- appWorkspace.Widgets.GeometryMeasureDialog,
- appWorkspace.Widgets.RunningDialog,
- appWorkspace.Widgets.HistogramDialog,
- appWorkspace.Widgets.ScratchTreatmentDialog,
- appWorkspace.Widgets.SmudgeTreatmentDialog,
- appWorkspace.Widgets.LabelListDialog,
- appWorkspace.Widgets.WorkFlowDialog,
- appWorkspace.Widgets.MeasureListDialog,
- appWorkspace.Widgets.OpticalDensityDialog,
- appWorkspace.Widgets.ProjectEngineering
- };
- foreach (FloatingToolForm ftf in floaters)
- {
- ftf.Closing += this.HideInsteadOfCloseHandler;
- }
- PositionFloatingForms();
- base.OnLoad(e);
- switch (PdnInfo.StartupTest)
- {
- case StartupTestType.Timed:
- Application.DoEvents();
- Application.Exit();
- break;
- case StartupTestType.WorkingSet:
- const int waitPeriodForVadumpSnapshot = 20000;
- Application.DoEvents();
- Thread.Sleep(waitPeriodForVadumpSnapshot);
- Application.Exit();
- break;
- }
- }
- private void PositionFloatingForms()
- {
- this.appWorkspace.ResetFloatingForms();
- try
- {
- SnapManager.Load(Settings.CurrentUser);
- }
- catch
- {
- this.appWorkspace.ResetFloatingForms();
- }
- foreach (FloatingToolForm ftf in floaters)
- {
- this.AddOwnedForm(ftf);
- }
- if (Settings.CurrentUser.GetBoolean(SettingNames.RuleListFormVisible, false))
- {
- this.appWorkspace.Widgets.RuleListForm.Show();
- }
- if (Settings.CurrentUser.GetBoolean(SettingNames.PixelTrackingDialogVisible, false))
- {
- this.appWorkspace.Widgets.PixelTrackingDialog.Show();
- }
- if (Settings.CurrentUser.GetBoolean(SettingNames.ImageIndexDialogVisible, false))
- {
- this.appWorkspace.Widgets.ImageIndexDialog.Show();
- }
- if (Settings.CurrentUser.GetBoolean(SettingNames.GeometryMeasureDialogVisible, false))
- {
- this.appWorkspace.Widgets.GeometryMeasureDialog.Show();
- }
- if (Settings.CurrentUser.GetBoolean(SettingNames.HistogramDialogVisible, false))
- {
- this.appWorkspace.Widgets.HistogramDialog.Show();
- }
- //if (Settings.CurrentUser.GetBoolean(SettingNames.HistogramDialogVisible, false))
- //{
- // this.appWorkspace.Widgets.HistogramDialog.Show();
- //}
- //if (Settings.CurrentUser.GetBoolean(SettingNames.HistogramDialogVisible, false))
- //{
- // this.appWorkspace.Widgets.HistogramDialog.Show();
- //}
- if (Settings.CurrentUser.GetBoolean(SettingNames.WorkFlowDialogVisible, false))
- {
- this.appWorkspace.Widgets.WorkFlowDialog.Show();
- }
- if (Settings.CurrentUser.GetBoolean(SettingNames.LabelListDialogVisible, false))
- {
- this.appWorkspace.Widgets.LabelListDialog.Show();
- }
- if (Settings.CurrentUser.GetBoolean(SettingNames.MeasureListDialogVisible, false))
- {
- this.appWorkspace.Widgets.MeasureListDialog.Show();
- }
- if (Settings.CurrentUser.GetBoolean(SettingNames.OpticalDensityDialogVisible, false))
- {
- this.appWorkspace.Widgets.OpticalDensityDialog.Show();
- }
- if (Settings.CurrentUser.GetBoolean(SettingNames.ProjectEngineeringVisible, false))
- {
- this.appWorkspace.Widgets.ProjectEngineering.Show();
- }
- // If the floating form is off screen somehow, reset it
- // We've been getting a lot of reports where people say their Colors window has disappeared
- Screen[] allScreens = Screen.AllScreens;
- foreach (FloatingToolForm ftf in this.floaters)
- {
- if (!ftf.Visible)
- {
- continue;
- }
- bool reset = false;
- try
- {
- bool foundAScreen = false;
- foreach (Screen screen in allScreens)
- {
- Rectangle intersect = Rectangle.Intersect(screen.Bounds, ftf.Bounds);
- if (intersect.Width > 0 && intersect.Height > 0)
- {
- foundAScreen = true;
- break;
- }
- }
- if (!foundAScreen)
- {
- reset = true;
- }
- }
- catch (Exception)
- {
- reset = true;
- }
- if (reset)
- {
- this.appWorkspace.ResetFloatingForm(ftf);
- }
- }
- this.floaterOpacityTimer.Enabled = true;
- }
- protected override void OnResize(EventArgs e)
- {
- if (floaterOpacityTimer != null)
- {
- if (WindowState == FormWindowState.Minimized)
- {
- if (this.floaterOpacityTimer.Enabled)
- {
- this.floaterOpacityTimer.Enabled = false;
- }
- }
- else
- {
- if (!this.floaterOpacityTimer.Enabled)
- {
- this.floaterOpacityTimer.Enabled = true;
- }
- this.FloaterOpacityTimer_Tick(this, EventArgs.Empty);
- }
- }
- base.OnResize(e);
- }
- private void DocumentWorkspace_DocumentChanged(object sender, System.EventArgs e)
- {
- SetTitleText();
- OnResize(EventArgs.Empty);
- }
- private void SetTitleText()
- {
- if (this.appWorkspace == null)
- {
- return;
- }
- // this.Text = PdnInfo.GetAppName();
- this.Text = "锂电材料智能图像分析系统";
- /*if (this.appWorkspace.ActiveDocumentWorkspace == null)
- {
- this.Text = PdnInfo.GetAppName();
- }
- else
- {
- string appTitle = PdnInfo.GetAppName();
- string ratio = string.Empty;
- string title = string.Empty;
- string friendlyName = this.appWorkspace.ActiveDocumentWorkspace.GetFriendlyName();
- string text;
- if (this.WindowState != FormWindowState.Minimized)
- {
- string format = PdnResources.GetString("MainForm.Title.Format.Normal");
- text = string.Format(format, friendlyName, appWorkspace.ActiveDocumentWorkspace.ScaleFactor, appTitle);
- }
- else
- {
- string format = PdnResources.GetString("MainForm.Title.Format.Minimized");
- text = string.Format(format, friendlyName, appTitle);
- }
- if (appWorkspace.ActiveDocumentWorkspace.Document != null)
- {
- title = text;
- }
- this.Text = title;
- }*/
- }
- private void OnMenuDropDownClosed(object sender, System.EventArgs e)
- {
- ToolStripMenuItem menu = (ToolStripMenuItem)sender;
- foreach (ToolStripItem tsi in menu.DropDownItems)
- {
- tsi.Enabled = true;
- }
- }
- private void HideInsteadOfCloseHandler(object sender, CancelEventArgs e)
- {
- e.Cancel = true;
- ((Form)sender).Hide();
- }
- private void FloaterOpacityTimer_Tick(object sender, System.EventArgs e)
- {
- if (this.WindowState == FormWindowState.Minimized ||
- this.floaters == null ||
- !PdnBaseForm.EnableOpacity ||
- this.appWorkspace.ActiveDocumentWorkspace == null)
- {
- return;
- }
- // Here's the behavior we want for our floaters:
- // 1. If the mouse is within a floaters rectangle, it should transition to fully opaque
- // 2. If the mouse is outside the floater's rectangle, it should transition to partially
- // opaque
- // 3. However, if the floater is outside where the document is visible on screen, it
- // should always be fully opaque.
- Rectangle screenDocRect;
- try
- {
- screenDocRect = this.appWorkspace.ActiveDocumentWorkspace.VisibleDocumentBounds;
- }
- catch (ObjectDisposedException)
- {
- return; // do nothing, we are probably in the process of shutting down the app
- }
- for (int i = 0; i < floaters.Length; ++i)
- {
- FloatingToolForm ftf = floaters[i];
- Rectangle intersect = Rectangle.Intersect(screenDocRect, ftf.Bounds);
- double opacity = -1.0;
- try
- {
- if (intersect.Width == 0 ||
- intersect.Height == 0 ||
- (ftf.Bounds.Contains(Control.MousePosition) &&
- !appWorkspace.ActiveDocumentWorkspace.IsMouseCaptured()) ||
- Utility.DoesControlHaveMouseCaptured(ftf))
- {
- opacity = Math.Min(1.0, ftf.Opacity + 0.125);
- }
- else
- {
- opacity = Math.Max(0.75, ftf.Opacity - 0.0625);
- }
- if (opacity != ftf.Opacity)
- {
- ftf.Opacity = opacity;
- }
- }
- catch (System.ComponentModel.Win32Exception)
- {
- // We just eat the exception. Chris Strahl was having some problem where opacity was 0.7
- // and we were trying to set it to 0.7 and it said "the parameter is incorrect"
- // ... which is stupid. Bad NVIDIA drivers for his GeForce Go?
- }
- }
- }
- protected override void OnDragEnter(DragEventArgs drgevent)
- {
- if (Enabled && drgevent.Data.GetDataPresent(DataFormats.FileDrop))
- {
- string[] files = (string[])drgevent.Data.GetData(DataFormats.FileDrop);
- foreach (string file in files)
- {
- try
- {
- FileAttributes fa = System.IO.File.GetAttributes(file);
- if ((fa & FileAttributes.Directory) == 0)
- {
- drgevent.Effect = DragDropEffects.Copy;
- }
- }
- catch
- {
- }
- }
- }
- base.OnDragEnter(drgevent);
- }
- private string[] PruneDirectories(string[] fileNames)
- {
- List<string> result = new List<string>();
- foreach (string fileName in fileNames)
- {
- try
- {
- FileAttributes fa = System.IO.File.GetAttributes(fileName);
- if ((fa & FileAttributes.Directory) == 0)
- {
- result.Add(fileName);
- }
- }
- catch
- {
- }
- }
- return result.ToArray();
- }
- protected override void OnDragDrop(DragEventArgs drgevent)
- {
- Activate();
- if (!IsCurrentModalForm || !Enabled)
- {
- // do nothing
- }
- else if (drgevent.Data.GetDataPresent(DataFormats.FileDrop))
- {
- string[] allFiles = (string[])drgevent.Data.GetData(DataFormats.FileDrop);
- if (allFiles == null)
- {
- return;
- }
- string[] files = PruneDirectories(allFiles);
- if (files.Length == 0)
- {
- return;
- }
- this.appWorkspace.OpenFilesInNewWorkspace(files);
- }
- base.OnDragDrop(drgevent);
- }
- private void DocumentWorkspace_ScaleFactorChanged(object sender, EventArgs e)
- {
- SetTitleText();
- }
- protected override void OnSizeChanged(EventArgs e)
- {
- base.OnSizeChanged(e);
- SetTitleText();
- }
- public void DeferredInitialization(object sender, EventArgs e)
- {
- this.deferredInitializationTimer.Enabled = false;
- this.deferredInitializationTimer.Tick -= new EventHandler(DeferredInitialization);
- this.deferredInitializationTimer.Dispose();
- this.deferredInitializationTimer = null;
- }
- /*protected override void OnHelpRequested(HelpEventArgs hevent)
- {
- // F1 is already handled by the Menu->Help menu item. No need to process it twice.
- hevent.Handled = true;
- base.OnHelpRequested(hevent);
- }*/
- }
- }
|