MainForm.cs 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302
  1. using PaintDotNet.Actions;
  2. using PaintDotNet.Base.CommTool;
  3. using PaintDotNet.File.UnsavedProcessing;
  4. using PaintDotNet.Camera;
  5. using PaintDotNet.SystemLayer;
  6. using StageController;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.ComponentModel;
  10. using System.Drawing;
  11. using System.IO;
  12. using System.Threading;
  13. using System.Windows.Forms;
  14. namespace PaintDotNet
  15. {
  16. internal sealed class MainForm : PdnBaseForm
  17. {
  18. private AppWorkspace appWorkspace;
  19. private FloatingToolForm[] floaters;
  20. private System.Windows.Forms.Timer floaterOpacityTimer;
  21. private System.Windows.Forms.Timer deferredInitializationTimer;
  22. private IContainer components;
  23. private bool killAfterInit = false;
  24. private SplashForm splashForm = null;
  25. private SingleInstanceManager singleInstanceManager = null;
  26. private List<string> queuedInstanceMessages = new List<string>();
  27. public SingleInstanceManager SingleInstanceManager
  28. {
  29. get
  30. {
  31. return this.singleInstanceManager;
  32. }
  33. set
  34. {
  35. if (this.singleInstanceManager != null)
  36. {
  37. this.singleInstanceManager.InstanceMessageReceived -= new EventHandler(SingleInstanceManager_InstanceMessageReceived);
  38. this.singleInstanceManager.SetWindow(null);
  39. }
  40. this.singleInstanceManager = value;
  41. if (this.singleInstanceManager != null)
  42. {
  43. this.singleInstanceManager.SetWindow(this);
  44. this.singleInstanceManager.InstanceMessageReceived += new EventHandler(SingleInstanceManager_InstanceMessageReceived);
  45. }
  46. }
  47. }
  48. private void SingleInstanceManager_InstanceMessageReceived(object sender, EventArgs e)
  49. {
  50. BeginInvoke(new Procedure(ProcessQueuedInstanceMessages), null);
  51. }
  52. public MainForm() : this(new string[0])
  53. {
  54. }
  55. public const int WM_DEVICECHANGE = 0x219;
  56. public const int DBT_DEVNODES_CHANGED = 0x0007;
  57. protected override void WndProc(ref Message m)
  58. {
  59. if (this.singleInstanceManager != null)
  60. {
  61. this.singleInstanceManager.FilterMessage(ref m);
  62. }
  63. if (m.Msg == WM_DEVICECHANGE) //设备改变消息
  64. {
  65. switch (m.WParam.ToInt32())
  66. {
  67. case DBT_DEVNODES_CHANGED:
  68. var cmgr = CameraManager.GetInstance();
  69. if (cmgr.CameraCount > 0)
  70. {
  71. int count = cmgr.CameraCount;
  72. var cnlist = cmgr.GetCameraNameList();
  73. cmgr.CheckDevices();
  74. if (count > cmgr.CameraCount)
  75. {
  76. foreach (var name in cnlist)
  77. {
  78. if (!cmgr.GetCameraNameList().Contains(name))
  79. MessageBox.Show("相机" + name + "已经断开");
  80. }
  81. }
  82. if (cmgr.CameraCount == 0)
  83. {
  84. cmgr.UnInitManager();
  85. }
  86. }
  87. else
  88. {
  89. cmgr.UnInitManager();
  90. cmgr.InitManager();
  91. if (CameraManager.IsLive)
  92. MessageBox.Show("相机已经重连");
  93. }
  94. if (!AxisController.GetInstance().IsOpen)
  95. AxisController.GetInstance().Open();
  96. break;
  97. }
  98. }
  99. base.WndProc(ref m);
  100. }
  101. private enum ArgumentAction
  102. {
  103. Open,
  104. OpenUntitled,
  105. NoOp
  106. }
  107. private bool SplitMessage(string message, out ArgumentAction action, out string actionParm)
  108. {
  109. if (message.Length == 0)
  110. {
  111. action = ArgumentAction.NoOp;
  112. actionParm = null;
  113. return false;
  114. }
  115. const string untitledPrefix = "untitled:";
  116. if (message.IndexOf(untitledPrefix) == 0)
  117. {
  118. action = ArgumentAction.OpenUntitled;
  119. actionParm = message.Substring(untitledPrefix.Length);
  120. return true;
  121. }
  122. action = ArgumentAction.Open;
  123. actionParm = message;
  124. return true;
  125. }
  126. private bool ProcessMessage(string message)
  127. {
  128. if (IsDisposed)
  129. {
  130. return false;
  131. }
  132. ArgumentAction action;
  133. string actionParm;
  134. bool result;
  135. result = SplitMessage(message, out action, out actionParm);
  136. if (!result)
  137. {
  138. return true;
  139. }
  140. switch (action)
  141. {
  142. case ArgumentAction.NoOp:
  143. result = true;
  144. break;
  145. case ArgumentAction.Open:
  146. this.Invoke(new Action(() =>
  147. {
  148. Activate();
  149. }));
  150. if (/*IsCurrentModalForm && */Enabled)
  151. {
  152. result = this.appWorkspace.OpenFileInNewWorkspace(actionParm);
  153. }
  154. break;
  155. case ArgumentAction.OpenUntitled:
  156. this.Invoke(new Action(() =>
  157. {
  158. Activate();
  159. }));
  160. if (!string.IsNullOrEmpty(actionParm) && IsCurrentModalForm && Enabled)
  161. {
  162. result = this.appWorkspace.OpenFileInNewWorkspace(actionParm, false);
  163. if (result)
  164. {
  165. this.appWorkspace.ActiveDocumentWorkspace.SetDocumentSaveOptions(null/*, null, null*/);
  166. this.appWorkspace.ActiveDocumentWorkspace.Document.Dirty = true;
  167. }
  168. }
  169. break;
  170. default:
  171. throw new InvalidEnumArgumentException();
  172. }
  173. return result;
  174. }
  175. private void ProcessQueuedInstanceMessages()
  176. {
  177. if (IsDisposed)
  178. {
  179. return;
  180. }
  181. if (this.splashForm != null)
  182. {
  183. this.splashForm.Close();
  184. this.splashForm.Dispose();
  185. this.splashForm = null;
  186. }
  187. if (IsHandleCreated &&
  188. //!PdnInfo.IsExpired &&
  189. this.singleInstanceManager != null)
  190. {
  191. string[] messages1 = this.singleInstanceManager.GetPendingInstanceMessages();
  192. string[] messages2 = this.queuedInstanceMessages.ToArray();
  193. this.queuedInstanceMessages.Clear();
  194. string[] messages = new string[messages1.Length + messages2.Length];
  195. for (int i = 0; i < messages1.Length; ++i)
  196. {
  197. messages[i] = messages1[i];
  198. }
  199. for (int i = 0; i < messages2.Length; ++i)
  200. {
  201. messages[i + messages1.Length] = messages2[i];
  202. }
  203. foreach (string message in messages)
  204. {
  205. bool result = ProcessMessage(message);
  206. if (!result)
  207. {
  208. break;
  209. }
  210. }
  211. }
  212. }
  213. private void Application_Idle(object sender, EventArgs e)
  214. {
  215. if (!this.IsDisposed &&
  216. (this.queuedInstanceMessages.Count > 0 || (this.singleInstanceManager != null && this.singleInstanceManager.AreMessagesPending)))
  217. {
  218. ProgressThreadProcClass procClass = new ProgressThreadProcClass();
  219. ProgressThreadProcClass.IFileTransferProgressEvents progressEvents = new ProgressThreadProcClass.IFileTransferProgressEvents();
  220. System.Threading.ThreadStart copyThreadProc =
  221. delegate ()
  222. {
  223. try
  224. {
  225. if (IsDisposed)
  226. {
  227. return;
  228. }
  229. this.Invoke(new Action(() =>
  230. {
  231. if (this.splashForm != null)
  232. {
  233. this.splashForm.Close();
  234. this.splashForm.Dispose();
  235. this.splashForm = null;
  236. }
  237. }));
  238. if (IsHandleCreated && this.singleInstanceManager != null)
  239. {
  240. string[] messages1 = this.singleInstanceManager.GetPendingInstanceMessages();
  241. string[] messages2 = this.queuedInstanceMessages.ToArray();
  242. this.queuedInstanceMessages.Clear();
  243. string[] messages = new string[messages1.Length + messages2.Length];
  244. for (int i = 0; i < messages1.Length; ++i)
  245. {
  246. messages[i] = messages1[i];
  247. }
  248. for (int i = 0; i < messages2.Length; ++i)
  249. {
  250. messages[i + messages1.Length] = messages2[i];
  251. }
  252. for (int i = 0; i < messages.Length; i++)
  253. {
  254. if (!ProcessMessage(messages[i])) continue;
  255. System.Threading.Thread.Sleep(100);
  256. this.Invoke(new Action(() =>
  257. {
  258. progressEvents.SetStringProgress((i + 1) + "/" + messages.Length);
  259. }));
  260. }
  261. }
  262. }
  263. catch (Exception)
  264. {
  265. }
  266. finally
  267. {
  268. progressEvents.EndOperation(OperationResult.Finished);
  269. }
  270. };
  271. procClass.StartProgressAction(this, 100, copyThreadProc, progressEvents, "打开图片");
  272. }
  273. }
  274. public MainForm(string[] args)
  275. {
  276. PdnBaseForm.xmlPath = Startup.instance.SettingPrefix;
  277. bool canSetCurrentDir = true;
  278. this.StartPosition = FormStartPosition.WindowsDefaultLocation;
  279. //bool splash = false;
  280. List<string> fileNames = new List<string>();
  281. // Parse command line arguments
  282. foreach (string argument in args)
  283. {
  284. if (0 == string.Compare(argument, "/dontForceGC"))
  285. {
  286. Utility.AllowGCFullCollect = false;
  287. }
  288. else if (0 == string.Compare(argument, "/splash", true))
  289. {
  290. //splash = true;
  291. }
  292. else if (0 == string.Compare(argument, "/test", true))
  293. {
  294. // This lets us use an alternate update manifest on the web server so that
  295. // we can test manifests on a small scale before "deploying" them to everybody
  296. PdnInfo.IsTestMode = true;
  297. }
  298. else if (0 == string.Compare(argument, "/profileStartupTimed", true))
  299. {
  300. // profileStartupTimed and profileStartupWorkingSet compete, which
  301. // ever is last in the args list wins.
  302. PdnInfo.StartupTest = StartupTestType.Timed;
  303. }
  304. else if (0 == string.Compare(argument, "/profileStartupWorkingSet", true))
  305. {
  306. // profileStartupTimed and profileStartupWorkingSet compete, which
  307. // ever is last in the args list wins.
  308. PdnInfo.StartupTest = StartupTestType.WorkingSet;
  309. }
  310. else if (argument.Length > 0 && argument[0] != '/')
  311. {
  312. //try
  313. //{
  314. // string fullPath = Path.GetFullPath(argument);
  315. // fileNames.Add(fullPath);
  316. //}
  317. //catch (Exception)
  318. //{
  319. // fileNames.Add(argument);
  320. // canSetCurrentDir = false;
  321. //}
  322. //splash = true;
  323. }
  324. }
  325. if (canSetCurrentDir)
  326. {
  327. try
  328. {
  329. Environment.CurrentDirectory = PdnInfo.GetApplicationDir();
  330. }
  331. catch (Exception ex)
  332. {
  333. Tracing.Ping("Exception while trying to set Environment.CurrentDirectory: " + ex.ToString());
  334. }
  335. }
  336. // make splash, if warranted
  337. //if (splash)
  338. //if (Startup.instance.configModel.StartImgStatus == 2)
  339. //{
  340. // //this.splashForm = new SplashForm();
  341. // this.splashForm = new SplashForm(Startup.instance.configModel.StartImg);
  342. // this.splashForm.TopMost = true;
  343. // this.splashForm.Show();
  344. // this.splashForm.Update();
  345. //}
  346. InitializeComponent();
  347. this.Icon = PdnInfo.AppIcon;
  348. // Does not load window location/state
  349. LoadSettings();
  350. //if (Startup.instance.splashForm != null && !Startup.instance.splashForm.IsDisposed)
  351. // Startup.instance.splashForm.Hide();
  352. if (Startup.instance.splashForm != null && !Startup.instance.splashForm.IsDisposed)
  353. Startup.instance.splashForm.Hide();
  354. //恢复图片
  355. List<string> imageList = FileOperationHelper.RestoreImages();
  356. if (imageList != null && imageList.Count > 0)
  357. {
  358. if (MessageBox.Show(PdnResources.GetString("Menu.RetrievableimagdetectedReopeit.Text") + "?", PdnResources.GetString("Menu.Edit.Redo.Text"), MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
  359. {
  360. List<string> newImg = FileOperationHelper.MoveRestoreImages(imageList);
  361. foreach (string n in newImg)
  362. {
  363. fileNames.Add(n);
  364. }
  365. }
  366. }
  367. foreach (string fileName in fileNames)
  368. {
  369. this.queuedInstanceMessages.Add(fileName);
  370. }
  371. // 原来是创建一个空白的文件,现在不要了
  372. /*if (fileNames.Count == 0)
  373. {
  374. MeasurementUnit units = Document.DefaultDpuUnit;
  375. double dpu = Document.GetDefaultDpu(units);
  376. Size newSize = this.appWorkspace.GetNewDocumentSize();
  377. this.appWorkspace.CreateBlankDocumentInNewWorkspace(newSize, units, dpu, true);
  378. this.appWorkspace.ActiveDocumentWorkspace.Document.Dirty = false;
  379. }*/
  380. LoadWindowState();
  381. deferredInitializationTimer.Enabled = true;
  382. Application.Idle += new EventHandler(Application_Idle);
  383. this.Shown += new EventHandler(this.MainForm_Shown);
  384. }
  385. private void MainForm_Shown(object sender, EventArgs e)
  386. {
  387. this.appWorkspace.ToolBar.test1(false);
  388. this.appWorkspace.SetTopLeftCheckState();
  389. }
  390. protected override void OnShown(EventArgs e)
  391. {
  392. base.OnShown(e);
  393. /*//判断是否过期的处理
  394. if (PdnInfo.IsExpired)
  395. {
  396. foreach (Form form in Application.OpenForms)
  397. {
  398. form.Enabled = false;
  399. }
  400. TaskButton checkForUpdatesTB = new TaskButton(
  401. PdnResources.GetImageResource("Icons.MenuHelpCheckForUpdatesIcon.png").Reference,
  402. PdnResources.GetString("ExpiredTaskDialog.CheckForUpdatesTB.ActionText"),
  403. PdnResources.GetString("ExpiredTaskDialog.CheckForUpdatesTB.ExplanationText"));
  404. TaskButton goToWebSiteTB = new TaskButton(
  405. PdnResources.GetImageResource("Icons.MenuHelpPdnWebsiteIcon.png").Reference,
  406. PdnResources.GetString("ExpiredTaskDialog.GoToWebSiteTB.ActionText"),
  407. PdnResources.GetString("ExpiredTaskDialog.GoToWebSiteTB.ExplanationText"));
  408. TaskButton doNotCheckForUpdatesTB = new TaskButton(
  409. PdnResources.GetImageResource("Icons.CancelIcon.png").Reference,
  410. PdnResources.GetString("ExpiredTaskDialog.DoNotCheckForUpdatesTB.ActionText"),
  411. PdnResources.GetString("ExpiredTaskDialog.DoNotCheckForUpdatesTB.ExplanationText"));
  412. TaskButton[] taskButtons =
  413. new TaskButton[]
  414. {
  415. checkForUpdatesTB,
  416. goToWebSiteTB,
  417. doNotCheckForUpdatesTB
  418. };
  419. TaskButton clickedTB = TaskDialog.Show(
  420. this,
  421. Icon,
  422. PdnInfo.GetFullAppName(),
  423. PdnResources.GetImageResource("Icons.WarningIcon.png").Reference,
  424. true,
  425. PdnResources.GetString("ExpiredTaskDialog.InfoText"),
  426. taskButtons,
  427. checkForUpdatesTB,
  428. doNotCheckForUpdatesTB,
  429. 450,
  430. false,
  431. 0,
  432. out bool unuse);
  433. if (clickedTB == goToWebSiteTB)
  434. {
  435. PdnInfo.LaunchWebSite(this, "http://www.baidu.com");//InvariantStrings.ExpiredPage
  436. }
  437. Close();
  438. }*/
  439. }
  440. private void LoadWindowState()
  441. {
  442. try
  443. {
  444. FormWindowState fws = (FormWindowState)Enum.Parse(typeof(FormWindowState),
  445. Settings.CurrentUser.GetString(SettingNames.WindowState, WindowState.ToString()), true);
  446. // if the state was saved as 'minimized' then just ignore whatever was saved
  447. if (fws != FormWindowState.Minimized)
  448. {
  449. if (fws != FormWindowState.Maximized)
  450. {
  451. Rectangle newBounds = Rectangle.Empty;
  452. // Load the registry values into a rectangle so that we
  453. // can update the settings all at once, instead of one
  454. // at a time. This will make loading the size an all or
  455. // none operation, with no rollback necessary
  456. newBounds.Width = Settings.CurrentUser.GetInt32(SettingNames.Width, this.Width);
  457. newBounds.Height = Settings.CurrentUser.GetInt32(SettingNames.Height, this.Height);
  458. int left = Settings.CurrentUser.GetInt32(SettingNames.Left, this.Left);
  459. int top = Settings.CurrentUser.GetInt32(SettingNames.Top, this.Top);
  460. newBounds.Location = new Point(left, top);
  461. this.Bounds = newBounds;
  462. }
  463. this.WindowState = fws;
  464. }
  465. }
  466. catch
  467. {
  468. try
  469. {
  470. Settings.CurrentUser.Delete(
  471. new string[]
  472. {
  473. SettingNames.Width,
  474. SettingNames.Height,
  475. SettingNames.WindowState,
  476. SettingNames.Top,
  477. SettingNames.Left
  478. });
  479. }
  480. catch
  481. {
  482. // ignore errors
  483. }
  484. }
  485. }
  486. private void LoadSettings()
  487. {
  488. try
  489. {
  490. PdnBaseForm.EnableOpacity = Settings.CurrentUser.GetBoolean(SettingNames.TranslucentWindows, true);
  491. }
  492. catch (Exception ex)
  493. {
  494. Tracing.Ping("Exception in MainForm.LoadSettings:" + ex.ToString());
  495. try
  496. {
  497. Settings.CurrentUser.Delete(
  498. new string[]
  499. {
  500. SettingNames.TranslucentWindows
  501. });
  502. }
  503. catch
  504. {
  505. }
  506. }
  507. }
  508. private void SaveSettings()
  509. {
  510. Settings.CurrentUser.SetInt32(SettingNames.Width, this.Width);
  511. Settings.CurrentUser.SetInt32(SettingNames.Height, this.Height);
  512. Settings.CurrentUser.SetInt32(SettingNames.Top, this.Top);
  513. Settings.CurrentUser.SetInt32(SettingNames.Left, this.Left);
  514. Settings.CurrentUser.SetString(SettingNames.WindowState, this.WindowState.ToString());
  515. Settings.CurrentUser.SetBoolean(SettingNames.TranslucentWindows, PdnBaseForm.EnableOpacity);
  516. if (this.WindowState != FormWindowState.Minimized)
  517. {
  518. Settings.CurrentUser.SetBoolean(SettingNames.RuleListFormVisible, this.appWorkspace.Widgets.RuleListForm.Visible);
  519. Settings.CurrentUser.SetBoolean(SettingNames.PixelTrackingDialogVisible, this.appWorkspace.Widgets.PixelTrackingDialog.Visible);
  520. Settings.CurrentUser.SetBoolean(SettingNames.ImageIndexDialogVisible, this.appWorkspace.Widgets.ImageIndexDialog.Visible);
  521. Settings.CurrentUser.SetBoolean(SettingNames.GeometryMeasureDialogVisible, this.appWorkspace.Widgets.GeometryMeasureDialog.Visible);
  522. Settings.CurrentUser.SetBoolean(SettingNames.HistogramDialogVisible, this.appWorkspace.Widgets.HistogramDialog.Visible);
  523. //Settings.CurrentUser.SetBoolean(SettingNames.HistogramDialogVisible, this.appWorkspace.Widgets.HistogramDialog.Visible);
  524. //Settings.CurrentUser.SetBoolean(SettingNames.HistogramDialogVisible, this.appWorkspace.Widgets.HistogramDialog.Visible);
  525. Settings.CurrentUser.SetBoolean(SettingNames.WorkFlowDialogVisible, this.appWorkspace.Widgets.WorkFlowDialog.Visible);
  526. Settings.CurrentUser.SetBoolean(SettingNames.MeasureListDialogVisible, this.appWorkspace.Widgets.MeasureListDialog.Visible);
  527. Settings.CurrentUser.SetBoolean(SettingNames.LabelListDialogVisible, this.appWorkspace.Widgets.LabelListDialog.Visible);
  528. Settings.CurrentUser.SetBoolean(SettingNames.OpticalDensityDialogVisible, this.appWorkspace.Widgets.OpticalDensityDialog.Visible);
  529. Settings.CurrentUser.SetBoolean(SettingNames.ProjectEngineeringVisible, this.appWorkspace.Widgets.ProjectEngineering.Visible);
  530. }
  531. SnapManager.Save(Settings.CurrentUser);
  532. this.appWorkspace.SaveSettings();
  533. }
  534. protected override void OnQueryEndSession(CancelEventArgs e)
  535. {
  536. if (IsCurrentModalForm)
  537. {
  538. OnClosing(e);
  539. }
  540. else
  541. {
  542. foreach (Form form in Application.OpenForms)
  543. {
  544. PdnBaseForm asPDF = form as PdnBaseForm;
  545. if (asPDF != null)
  546. {
  547. asPDF.Flash();
  548. }
  549. }
  550. e.Cancel = true;
  551. }
  552. base.OnQueryEndSession(e);
  553. }
  554. protected override void OnClosing(CancelEventArgs e)
  555. {
  556. UnsavedProcessingDialog unsavedProcessingDialog;
  557. if (!e.Cancel)
  558. {
  559. if (this.appWorkspace != null)
  560. {
  561. if (this.appWorkspace.DocumentWorkspaces != null && this.appWorkspace.DocumentWorkspaces.Length > 0)
  562. {
  563. foreach (DocumentWorkspace document in this.appWorkspace.DocumentWorkspaces)
  564. {
  565. if (document.Document.Dirty)
  566. {
  567. unsavedProcessingDialog = new UnsavedProcessingDialog(this.appWorkspace);
  568. if (unsavedProcessingDialog.ShowDialog() == DialogResult.OK)
  569. {
  570. e.Cancel = unsavedProcessingDialog.Cancelled;
  571. }
  572. else
  573. {
  574. e.Cancel = true;
  575. }
  576. break;
  577. }
  578. //CloseWorkspaceAction action = new CloseWorkspaceAction();
  579. //this.appWorkspace.PerformAction(action);
  580. //e.Cancel = action.Cancelled;
  581. //if (e.Cancel) break;
  582. }
  583. }
  584. /**
  585. CloseAllWorkspacesAction cawa = new CloseAllWorkspacesAction();
  586. this.appWorkspace.PerformAction(cawa);
  587. e.Cancel = cawa.Cancelled;
  588. **/
  589. }
  590. }
  591. //if (unsavedProcessingDialog.ShowDialog() == DialogResult.OK)
  592. //{
  593. // return;
  594. //}
  595. if (!e.Cancel)
  596. {
  597. SaveSettings();
  598. if (this.floaters != null)
  599. {
  600. foreach (Form hideMe in this.floaters)
  601. {
  602. hideMe.Hide();
  603. }
  604. }
  605. this.Hide();
  606. if (this.queuedInstanceMessages != null)
  607. {
  608. this.queuedInstanceMessages.Clear();
  609. }
  610. SingleInstanceManager sim2 = this.singleInstanceManager;
  611. SingleInstanceManager = null;
  612. if (sim2 != null)
  613. {
  614. sim2.Dispose();
  615. sim2 = null;
  616. }
  617. }
  618. FileOperationHelper.DeleteFolder(Application.StartupPath + "\\BackupImages");//删除备份文件
  619. FileOperationHelper.DeleteFolder(Application.StartupPath + "\\BackupImagesTemp");//删除备份文件
  620. base.OnClosing(e);
  621. }
  622. protected override void OnClosed(EventArgs e)
  623. {
  624. base.OnClosed(e);
  625. }
  626. /// <summary>
  627. /// Clean up any resources being used.
  628. /// </summary>
  629. protected override void Dispose(bool disposing)
  630. {
  631. if (disposing)
  632. {
  633. if (this.singleInstanceManager != null)
  634. {
  635. SingleInstanceManager sim2 = this.singleInstanceManager;
  636. SingleInstanceManager = null;
  637. sim2.Dispose();
  638. sim2 = null;
  639. }
  640. if (this.floaterOpacityTimer != null)
  641. {
  642. this.floaterOpacityTimer.Tick -= new System.EventHandler(this.FloaterOpacityTimer_Tick);
  643. this.floaterOpacityTimer.Dispose();
  644. this.floaterOpacityTimer = null;
  645. }
  646. if (this.components != null)
  647. {
  648. this.components.Dispose();
  649. this.components = null;
  650. }
  651. }
  652. try
  653. {
  654. base.Dispose(disposing);
  655. }
  656. catch (RankException)
  657. {
  658. // System.Windows.Forms.PropertyStore
  659. // Discard error - bug #2746
  660. }
  661. }
  662. /// <summary>
  663. /// Required method for Designer support - do not modify
  664. /// the contents of this method with the code editor.
  665. /// </summary>
  666. private void InitializeComponent()
  667. {
  668. this.components = new Container();
  669. this.appWorkspace = new AppWorkspace(this);
  670. this.floaterOpacityTimer = new System.Windows.Forms.Timer(this.components);
  671. this.deferredInitializationTimer = new System.Windows.Forms.Timer(this.components);
  672. this.SuspendLayout();
  673. //
  674. // appWorkspace
  675. //
  676. this.appWorkspace.Dock = System.Windows.Forms.DockStyle.Fill;
  677. this.appWorkspace.Location = new System.Drawing.Point(0, 0);
  678. this.appWorkspace.Name = "appWorkspace";
  679. this.appWorkspace.Size = new System.Drawing.Size(752, 648);
  680. this.appWorkspace.TabIndex = 2;
  681. this.appWorkspace.ActiveDocumentWorkspaceChanging += new EventHandler(AppWorkspace_ActiveDocumentWorkspaceChanging);
  682. this.appWorkspace.ActiveDocumentWorkspaceChanged += new EventHandler(AppWorkspace_ActiveDocumentWorkspaceChanged);
  683. //
  684. // floaterOpacityTimer
  685. //
  686. this.floaterOpacityTimer.Enabled = false;
  687. this.floaterOpacityTimer.Interval = 25;
  688. this.floaterOpacityTimer.Tick += new System.EventHandler(this.FloaterOpacityTimer_Tick);
  689. //
  690. // deferredInitializationTimer
  691. //
  692. this.deferredInitializationTimer.Interval = 250;
  693. this.deferredInitializationTimer.Tick += new EventHandler(DeferredInitialization);
  694. //
  695. // MainForm
  696. //
  697. try
  698. {
  699. this.AllowDrop = true;
  700. }
  701. catch (InvalidOperationException)
  702. {
  703. // Discard error. See bug #2605.
  704. }
  705. this.AutoScaleDimensions = new SizeF(96F, 96F);
  706. this.AutoScaleMode = AutoScaleMode.Dpi;
  707. this.ClientSize = new Size(950, 738);
  708. this.Controls.Add(this.appWorkspace);
  709. this.Name = "MainForm";
  710. this.StartPosition = FormStartPosition.WindowsDefaultLocation;
  711. this.ForceActiveTitleBar = true;
  712. this.KeyPreview = true;
  713. this.Controls.SetChildIndex(this.appWorkspace, 0);
  714. this.ResumeLayout(false);
  715. this.PerformLayout();
  716. }
  717. private void AppWorkspace_ActiveDocumentWorkspaceChanging(object sender, EventArgs e)
  718. {
  719. if (this.appWorkspace.ActiveDocumentWorkspace != null)
  720. {
  721. this.appWorkspace.ActiveDocumentWorkspace.ScaleFactorChanged -= DocumentWorkspace_ScaleFactorChanged;
  722. this.appWorkspace.ActiveDocumentWorkspace.DocumentChanged -= DocumentWorkspace_DocumentChanged;
  723. this.appWorkspace.ActiveDocumentWorkspace.SaveOptionsChanged -= DocumentWorkspace_SaveOptionsChanged;
  724. }
  725. }
  726. private void AppWorkspace_ActiveDocumentWorkspaceChanged(object sender, EventArgs e)
  727. {
  728. if (this.appWorkspace.ActiveDocumentWorkspace != null)
  729. {
  730. this.appWorkspace.ActiveDocumentWorkspace.ScaleFactorChanged += DocumentWorkspace_ScaleFactorChanged;
  731. this.appWorkspace.ActiveDocumentWorkspace.DocumentChanged += DocumentWorkspace_DocumentChanged;
  732. this.appWorkspace.ActiveDocumentWorkspace.SaveOptionsChanged += DocumentWorkspace_SaveOptionsChanged;
  733. }
  734. SetTitleText();
  735. }
  736. private void DocumentWorkspace_SaveOptionsChanged(object sender, EventArgs e)
  737. {
  738. SetTitleText();
  739. }
  740. protected override void OnLoad(EventArgs e)
  741. {
  742. EnsureFormIsOnScreen();
  743. if (killAfterInit)
  744. {
  745. Application.Exit();
  746. }
  747. this.floaters = new FloatingToolForm[] {
  748. appWorkspace.Widgets.RuleListForm,
  749. appWorkspace.Widgets.PixelTrackingDialog,
  750. appWorkspace.Widgets.ImageIndexDialog,
  751. appWorkspace.Widgets.GeometryMeasureDialog,
  752. appWorkspace.Widgets.RunningDialog,
  753. appWorkspace.Widgets.HistogramDialog,
  754. appWorkspace.Widgets.ScratchTreatmentDialog,
  755. appWorkspace.Widgets.SmudgeTreatmentDialog,
  756. appWorkspace.Widgets.LabelListDialog,
  757. appWorkspace.Widgets.WorkFlowDialog,
  758. appWorkspace.Widgets.MeasureListDialog,
  759. appWorkspace.Widgets.OpticalDensityDialog,
  760. appWorkspace.Widgets.ProjectEngineering
  761. };
  762. foreach (FloatingToolForm ftf in floaters)
  763. {
  764. ftf.Closing += this.HideInsteadOfCloseHandler;
  765. }
  766. PositionFloatingForms();
  767. base.OnLoad(e);
  768. switch (PdnInfo.StartupTest)
  769. {
  770. case StartupTestType.Timed:
  771. Application.DoEvents();
  772. Application.Exit();
  773. break;
  774. case StartupTestType.WorkingSet:
  775. const int waitPeriodForVadumpSnapshot = 20000;
  776. Application.DoEvents();
  777. Thread.Sleep(waitPeriodForVadumpSnapshot);
  778. Application.Exit();
  779. break;
  780. }
  781. }
  782. private void PositionFloatingForms()
  783. {
  784. this.appWorkspace.ResetFloatingForms();
  785. try
  786. {
  787. SnapManager.Load(Settings.CurrentUser);
  788. }
  789. catch
  790. {
  791. this.appWorkspace.ResetFloatingForms();
  792. }
  793. foreach (FloatingToolForm ftf in floaters)
  794. {
  795. this.AddOwnedForm(ftf);
  796. }
  797. if (Settings.CurrentUser.GetBoolean(SettingNames.RuleListFormVisible, false))
  798. {
  799. this.appWorkspace.Widgets.RuleListForm.Show();
  800. }
  801. if (Settings.CurrentUser.GetBoolean(SettingNames.PixelTrackingDialogVisible, false))
  802. {
  803. this.appWorkspace.Widgets.PixelTrackingDialog.Show();
  804. }
  805. if (Settings.CurrentUser.GetBoolean(SettingNames.ImageIndexDialogVisible, false))
  806. {
  807. this.appWorkspace.Widgets.ImageIndexDialog.Show();
  808. }
  809. if (Settings.CurrentUser.GetBoolean(SettingNames.GeometryMeasureDialogVisible, false))
  810. {
  811. this.appWorkspace.Widgets.GeometryMeasureDialog.Show();
  812. }
  813. if (Settings.CurrentUser.GetBoolean(SettingNames.HistogramDialogVisible, false))
  814. {
  815. this.appWorkspace.Widgets.HistogramDialog.Show();
  816. }
  817. //if (Settings.CurrentUser.GetBoolean(SettingNames.HistogramDialogVisible, false))
  818. //{
  819. // this.appWorkspace.Widgets.HistogramDialog.Show();
  820. //}
  821. //if (Settings.CurrentUser.GetBoolean(SettingNames.HistogramDialogVisible, false))
  822. //{
  823. // this.appWorkspace.Widgets.HistogramDialog.Show();
  824. //}
  825. if (Settings.CurrentUser.GetBoolean(SettingNames.WorkFlowDialogVisible, false))
  826. {
  827. this.appWorkspace.Widgets.WorkFlowDialog.Show();
  828. }
  829. if (Settings.CurrentUser.GetBoolean(SettingNames.LabelListDialogVisible, false))
  830. {
  831. this.appWorkspace.Widgets.LabelListDialog.Show();
  832. }
  833. if (Settings.CurrentUser.GetBoolean(SettingNames.MeasureListDialogVisible, false))
  834. {
  835. this.appWorkspace.Widgets.MeasureListDialog.Show();
  836. }
  837. if (Settings.CurrentUser.GetBoolean(SettingNames.OpticalDensityDialogVisible, false))
  838. {
  839. this.appWorkspace.Widgets.OpticalDensityDialog.Show();
  840. }
  841. if (Settings.CurrentUser.GetBoolean(SettingNames.ProjectEngineeringVisible, false))
  842. {
  843. this.appWorkspace.Widgets.ProjectEngineering.Show();
  844. }
  845. // If the floating form is off screen somehow, reset it
  846. // We've been getting a lot of reports where people say their Colors window has disappeared
  847. Screen[] allScreens = Screen.AllScreens;
  848. foreach (FloatingToolForm ftf in this.floaters)
  849. {
  850. if (!ftf.Visible)
  851. {
  852. continue;
  853. }
  854. bool reset = false;
  855. try
  856. {
  857. bool foundAScreen = false;
  858. foreach (Screen screen in allScreens)
  859. {
  860. Rectangle intersect = Rectangle.Intersect(screen.Bounds, ftf.Bounds);
  861. if (intersect.Width > 0 && intersect.Height > 0)
  862. {
  863. foundAScreen = true;
  864. break;
  865. }
  866. }
  867. if (!foundAScreen)
  868. {
  869. reset = true;
  870. }
  871. }
  872. catch (Exception)
  873. {
  874. reset = true;
  875. }
  876. if (reset)
  877. {
  878. this.appWorkspace.ResetFloatingForm(ftf);
  879. }
  880. }
  881. this.floaterOpacityTimer.Enabled = true;
  882. }
  883. protected override void OnResize(EventArgs e)
  884. {
  885. if (floaterOpacityTimer != null)
  886. {
  887. if (WindowState == FormWindowState.Minimized)
  888. {
  889. if (this.floaterOpacityTimer.Enabled)
  890. {
  891. this.floaterOpacityTimer.Enabled = false;
  892. }
  893. }
  894. else
  895. {
  896. if (!this.floaterOpacityTimer.Enabled)
  897. {
  898. this.floaterOpacityTimer.Enabled = true;
  899. }
  900. this.FloaterOpacityTimer_Tick(this, EventArgs.Empty);
  901. }
  902. }
  903. base.OnResize(e);
  904. }
  905. private void DocumentWorkspace_DocumentChanged(object sender, System.EventArgs e)
  906. {
  907. SetTitleText();
  908. OnResize(EventArgs.Empty);
  909. }
  910. private void SetTitleText()
  911. {
  912. if (this.appWorkspace == null)
  913. {
  914. return;
  915. }
  916. this.Text = PdnInfo.GetAppName();
  917. /*if (this.appWorkspace.ActiveDocumentWorkspace == null)
  918. {
  919. this.Text = PdnInfo.GetAppName();
  920. }
  921. else
  922. {
  923. string appTitle = PdnInfo.GetAppName();
  924. string ratio = string.Empty;
  925. string title = string.Empty;
  926. string friendlyName = this.appWorkspace.ActiveDocumentWorkspace.GetFriendlyName();
  927. string text;
  928. if (this.WindowState != FormWindowState.Minimized)
  929. {
  930. string format = PdnResources.GetString("MainForm.Title.Format.Normal");
  931. text = string.Format(format, friendlyName, appWorkspace.ActiveDocumentWorkspace.ScaleFactor, appTitle);
  932. }
  933. else
  934. {
  935. string format = PdnResources.GetString("MainForm.Title.Format.Minimized");
  936. text = string.Format(format, friendlyName, appTitle);
  937. }
  938. if (appWorkspace.ActiveDocumentWorkspace.Document != null)
  939. {
  940. title = text;
  941. }
  942. this.Text = title;
  943. }*/
  944. }
  945. private void OnMenuDropDownClosed(object sender, System.EventArgs e)
  946. {
  947. ToolStripMenuItem menu = (ToolStripMenuItem)sender;
  948. foreach (ToolStripItem tsi in menu.DropDownItems)
  949. {
  950. tsi.Enabled = true;
  951. }
  952. }
  953. private void HideInsteadOfCloseHandler(object sender, CancelEventArgs e)
  954. {
  955. e.Cancel = true;
  956. ((Form)sender).Hide();
  957. }
  958. private void FloaterOpacityTimer_Tick(object sender, System.EventArgs e)
  959. {
  960. if (this.WindowState == FormWindowState.Minimized ||
  961. this.floaters == null ||
  962. !PdnBaseForm.EnableOpacity ||
  963. this.appWorkspace.ActiveDocumentWorkspace == null)
  964. {
  965. return;
  966. }
  967. // Here's the behavior we want for our floaters:
  968. // 1. If the mouse is within a floaters rectangle, it should transition to fully opaque
  969. // 2. If the mouse is outside the floater's rectangle, it should transition to partially
  970. // opaque
  971. // 3. However, if the floater is outside where the document is visible on screen, it
  972. // should always be fully opaque.
  973. Rectangle screenDocRect;
  974. try
  975. {
  976. screenDocRect = this.appWorkspace.ActiveDocumentWorkspace.VisibleDocumentBounds;
  977. }
  978. catch (ObjectDisposedException)
  979. {
  980. return; // do nothing, we are probably in the process of shutting down the app
  981. }
  982. for (int i = 0; i < floaters.Length; ++i)
  983. {
  984. FloatingToolForm ftf = floaters[i];
  985. Rectangle intersect = Rectangle.Intersect(screenDocRect, ftf.Bounds);
  986. double opacity = -1.0;
  987. try
  988. {
  989. if (intersect.Width == 0 ||
  990. intersect.Height == 0 ||
  991. (ftf.Bounds.Contains(Control.MousePosition) &&
  992. !appWorkspace.ActiveDocumentWorkspace.IsMouseCaptured()) ||
  993. Utility.DoesControlHaveMouseCaptured(ftf))
  994. {
  995. opacity = Math.Min(1.0, ftf.Opacity + 0.125);
  996. }
  997. else
  998. {
  999. opacity = Math.Max(0.75, ftf.Opacity - 0.0625);
  1000. }
  1001. if (opacity != ftf.Opacity)
  1002. {
  1003. ftf.Opacity = opacity;
  1004. }
  1005. }
  1006. catch (System.ComponentModel.Win32Exception)
  1007. {
  1008. // We just eat the exception. Chris Strahl was having some problem where opacity was 0.7
  1009. // and we were trying to set it to 0.7 and it said "the parameter is incorrect"
  1010. // ... which is stupid. Bad NVIDIA drivers for his GeForce Go?
  1011. }
  1012. }
  1013. }
  1014. protected override void OnDragEnter(DragEventArgs drgevent)
  1015. {
  1016. if (Enabled && drgevent.Data.GetDataPresent(DataFormats.FileDrop))
  1017. {
  1018. string[] files = (string[])drgevent.Data.GetData(DataFormats.FileDrop);
  1019. foreach (string file in files)
  1020. {
  1021. try
  1022. {
  1023. FileAttributes fa = System.IO.File.GetAttributes(file);
  1024. if ((fa & FileAttributes.Directory) == 0)
  1025. {
  1026. drgevent.Effect = DragDropEffects.Copy;
  1027. }
  1028. }
  1029. catch
  1030. {
  1031. }
  1032. }
  1033. }
  1034. base.OnDragEnter(drgevent);
  1035. }
  1036. private string[] PruneDirectories(string[] fileNames)
  1037. {
  1038. List<string> result = new List<string>();
  1039. foreach (string fileName in fileNames)
  1040. {
  1041. try
  1042. {
  1043. FileAttributes fa = System.IO.File.GetAttributes(fileName);
  1044. if ((fa & FileAttributes.Directory) == 0)
  1045. {
  1046. result.Add(fileName);
  1047. }
  1048. }
  1049. catch
  1050. {
  1051. }
  1052. }
  1053. return result.ToArray();
  1054. }
  1055. protected override void OnDragDrop(DragEventArgs drgevent)
  1056. {
  1057. Activate();
  1058. if (!IsCurrentModalForm || !Enabled)
  1059. {
  1060. // do nothing
  1061. }
  1062. else if (drgevent.Data.GetDataPresent(DataFormats.FileDrop))
  1063. {
  1064. string[] allFiles = (string[])drgevent.Data.GetData(DataFormats.FileDrop);
  1065. if (allFiles == null)
  1066. {
  1067. return;
  1068. }
  1069. string[] files = PruneDirectories(allFiles);
  1070. if (files.Length == 0)
  1071. {
  1072. return;
  1073. }
  1074. this.appWorkspace.OpenFilesInNewWorkspace(files);
  1075. }
  1076. base.OnDragDrop(drgevent);
  1077. }
  1078. private void DocumentWorkspace_ScaleFactorChanged(object sender, EventArgs e)
  1079. {
  1080. SetTitleText();
  1081. }
  1082. protected override void OnSizeChanged(EventArgs e)
  1083. {
  1084. base.OnSizeChanged(e);
  1085. SetTitleText();
  1086. }
  1087. public void DeferredInitialization(object sender, EventArgs e)
  1088. {
  1089. this.deferredInitializationTimer.Enabled = false;
  1090. this.deferredInitializationTimer.Tick -= new EventHandler(DeferredInitialization);
  1091. this.deferredInitializationTimer.Dispose();
  1092. this.deferredInitializationTimer = null;
  1093. }
  1094. /*protected override void OnHelpRequested(HelpEventArgs hevent)
  1095. {
  1096. // F1 is already handled by the Menu->Help menu item. No need to process it twice.
  1097. hevent.Handled = true;
  1098. base.OnHelpRequested(hevent);
  1099. }*/
  1100. }
  1101. }