MainForm.cs 42 KB

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