PdnBaseForm.cs 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  1. using PaintDotNet.Base.CommTool;
  2. using PaintDotNet.Base.Functionodel;
  3. using PaintDotNet.SystemLayer;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Drawing;
  8. using System.IO;
  9. using System.Resources;
  10. using System.Threading;
  11. using System.Windows.Forms;
  12. namespace PaintDotNet
  13. {
  14. /// <summary>
  15. /// This Form class is used to fix a few bugs in Windows Forms, and to add a few performance
  16. /// enhancements, such as disabling opacity != 1.0 when running in a remote TS/RD session.
  17. /// We derive from this class instead of Windows.Forms.Form directly.
  18. /// </summary>
  19. public class PdnBaseForm : Form, ISnapManagerHost
  20. {
  21. public static string xmlPath = "";
  22. static PdnBaseForm()
  23. {
  24. Application.EnterThreadModal += new EventHandler(Application_EnterThreadModal);
  25. Application.LeaveThreadModal += new EventHandler(Application_LeaveThreadModal);
  26. Application_EnterThreadModal(null, EventArgs.Empty);
  27. }
  28. // This set keeps track of forms which cannot be the current modal form.
  29. private static Stack<List<Form>> parentModalForms = new Stack<List<Form>>();
  30. private static bool IsInParentModalForms(Form form)
  31. {
  32. foreach (List<Form> formList in parentModalForms)
  33. {
  34. foreach (Form parentModalForm in formList)
  35. {
  36. if (parentModalForm == form)
  37. {
  38. return true;
  39. }
  40. }
  41. }
  42. return false;
  43. }
  44. private static List<Form> GetAllPeerForms(Form form)
  45. {
  46. if (form == null)
  47. {
  48. return new List<Form>();
  49. }
  50. if (form.Owner != null)
  51. {
  52. return GetAllPeerForms(form.Owner);
  53. }
  54. List<Form> forms = new List<Form>();
  55. forms.Add(form);
  56. forms.AddRange(form.OwnedForms);
  57. return forms;
  58. }
  59. private static void Application_EnterThreadModal(object sender, EventArgs e)
  60. {
  61. Form activeForm = Form.ActiveForm;
  62. List<Form> allPeerForms = GetAllPeerForms(activeForm);
  63. parentModalForms.Push(allPeerForms);
  64. }
  65. private static void Application_LeaveThreadModal(object sender, EventArgs e)
  66. {
  67. parentModalForms.Pop();
  68. }
  69. protected override void OnShown(EventArgs e)
  70. {
  71. isShown = true;
  72. Tracing.LogFeature("ShowDialog(" + GetType().FullName + ")");
  73. base.OnShown(e);
  74. }
  75. public bool IsShown
  76. {
  77. get
  78. {
  79. return this.isShown;
  80. }
  81. }
  82. private bool isShown = false;
  83. private bool enableOpacity = true;
  84. private double ourOpacity = 1.0; // store opacity setting so that when we go from disabled->enabled opacity we can set the correct value
  85. private SnapManager snapManager = null;
  86. private System.ComponentModel.IContainer components;
  87. private bool instanceEnableOpacity = true;
  88. private static bool globalEnableOpacity = true;
  89. private FormEx formEx;
  90. private bool processFormHotKeyMutex = false; // if we're already processing a form hotkey, don't let other hotkeys execute.
  91. private static Dictionary<Keys, Function<bool, Keys>> hotkeyRegistrar = null;
  92. /// <summary>
  93. /// 分析设置存储信息
  94. /// </summary>
  95. protected AnalyzeSettingModel analyzeSettingModel;
  96. public AnalyzeSettingModel AnalyzeSettingModel
  97. {
  98. get
  99. {
  100. return this.analyzeSettingModel;
  101. }
  102. set
  103. {
  104. this.analyzeSettingModel = value;
  105. }
  106. }
  107. /// <summary>
  108. /// Registers a form-wide hot key, and a callback for when the key is pressed.
  109. /// The callback must be an instance method on a Control. Whatever Form the Control
  110. /// is on will process the hotkey, as long as the Form is derived from PdnBaseForm.
  111. /// </summary>
  112. public static void RegisterFormHotKey(Keys keys, Function<bool, Keys> callback)
  113. {
  114. IComponent targetAsComponent = callback.Target as IComponent;
  115. IHotKeyTarget targetAsHotKeyTarget = callback.Target as IHotKeyTarget;
  116. if (targetAsComponent == null && targetAsHotKeyTarget == null)
  117. {
  118. throw new ArgumentException("target instance must implement IComponent or IHotKeyTarget", "callback");
  119. }
  120. if (hotkeyRegistrar == null)
  121. {
  122. hotkeyRegistrar = new Dictionary<Keys, Function<bool, Keys>>();
  123. }
  124. Function<bool, Keys> theDelegate = null;
  125. if (hotkeyRegistrar.ContainsKey(keys))
  126. {
  127. theDelegate = hotkeyRegistrar[keys];
  128. theDelegate += callback;
  129. hotkeyRegistrar[keys] = theDelegate;
  130. }
  131. else
  132. {
  133. theDelegate = new Function<bool, Keys>(callback);
  134. hotkeyRegistrar.Add(keys, theDelegate);
  135. }
  136. if (targetAsComponent != null)
  137. {
  138. targetAsComponent.Disposed += TargetAsComponent_Disposed;
  139. }
  140. else
  141. {
  142. targetAsHotKeyTarget.Disposed += TargetAsHotKeyTarget_Disposed;
  143. }
  144. }
  145. private bool ShouldProcessHotKey(Keys keys)
  146. {
  147. Keys keyOnly = keys & ~Keys.Modifiers;
  148. if (keyOnly == Keys.Back ||
  149. keyOnly == Keys.Delete ||
  150. keyOnly == Keys.Left ||
  151. keyOnly == Keys.Right ||
  152. keyOnly == Keys.Up ||
  153. keyOnly == Keys.Down ||
  154. keys == (Keys.Control | Keys.A) || // select all
  155. keys == (Keys.Control | Keys.Z) || // undo
  156. keys == (Keys.Control | Keys.Y) || // redo
  157. keys == (Keys.Control | Keys.X) || // cut
  158. keys == (Keys.Control | Keys.C) || // copy
  159. keys == (Keys.Control | Keys.V) || // paste
  160. keys == (Keys.Shift | Keys.Delete) || // cut (left-handed)
  161. keys == (Keys.Control | Keys.Insert) || // copy (left-handed)
  162. keys == (Keys.Shift | Keys.Insert) // paste (left-handed)
  163. )
  164. {
  165. Control focused = Utility.FindFocus();
  166. if (focused is TextBox || focused is ComboBox || focused is UpDownBase)
  167. {
  168. return false;
  169. }
  170. }
  171. return true;
  172. }
  173. private static void TargetAsComponent_Disposed(object sender, EventArgs e)
  174. {
  175. ((IComponent)sender).Disposed -= TargetAsComponent_Disposed;
  176. RemoveDisposedTarget(sender);
  177. }
  178. private static void TargetAsHotKeyTarget_Disposed(object sender, EventArgs e)
  179. {
  180. ((IHotKeyTarget)sender).Disposed -= TargetAsHotKeyTarget_Disposed;
  181. RemoveDisposedTarget(sender);
  182. }
  183. static void RemoveDisposedTarget(object sender)
  184. {
  185. // Control was disposed, but it never unregistered for its hotkeys!
  186. List<Keys> keysList = new List<Keys>(hotkeyRegistrar.Keys);
  187. foreach (Keys keys in keysList)
  188. {
  189. Function<bool, Keys> theMultiDelegate = hotkeyRegistrar[keys];
  190. foreach (Delegate theDelegate in theMultiDelegate.GetInvocationList())
  191. {
  192. if (object.ReferenceEquals(theDelegate.Target, sender))
  193. {
  194. UnregisterFormHotKey(keys, (Function<bool, Keys>)theDelegate);
  195. }
  196. }
  197. }
  198. }
  199. public static void UnregisterFormHotKey(Keys keys, Function<bool, Keys> callback)
  200. {
  201. if (hotkeyRegistrar != null)
  202. {
  203. //Function<bool, Keys> theDelegate = hotkeyRegistrar[keys];
  204. //theDelegate -= callback;
  205. //hotkeyRegistrar[keys] = theDelegate;
  206. IComponent targetAsComponent = callback.Target as IComponent;
  207. if (targetAsComponent != null)
  208. {
  209. targetAsComponent.Disposed -= TargetAsComponent_Disposed;
  210. }
  211. IHotKeyTarget targetAsHotKeyTarget = callback.Target as IHotKeyTarget;
  212. if (targetAsHotKeyTarget != null)
  213. {
  214. targetAsHotKeyTarget.Disposed -= TargetAsHotKeyTarget_Disposed;
  215. }
  216. if (hotkeyRegistrar.ContainsKey(keys)/*theDelegate.GetInvocationList().Length == 0*/)
  217. {
  218. Function<bool, Keys> theDelegate = hotkeyRegistrar[keys];
  219. theDelegate -= callback;
  220. hotkeyRegistrar.Remove(keys);
  221. }
  222. if (hotkeyRegistrar.Count == 0)
  223. {
  224. hotkeyRegistrar = null;
  225. }
  226. }
  227. }
  228. public void Flash()
  229. {
  230. UI.FlashForm(this);
  231. }
  232. public void RestoreWindow()
  233. {
  234. if (WindowState == FormWindowState.Minimized)
  235. {
  236. UI.RestoreWindow(this);
  237. }
  238. }
  239. /// <summary>
  240. /// Returns the currently active modal form if the process is in the foreground and is active.
  241. /// </summary>
  242. /// <remarks>
  243. /// If Form.ActiveForm is modeless, we search up the chain of owner forms
  244. /// to find its modeless owner form.
  245. /// </remarks>
  246. public static Form CurrentModalForm
  247. {
  248. get
  249. {
  250. Form theForm = Form.ActiveForm;
  251. while (theForm != null && !theForm.Modal && theForm.Owner != null)
  252. {
  253. theForm = theForm.Owner;
  254. }
  255. return theForm;
  256. }
  257. }
  258. /// <summary>
  259. /// Gets whether the current form is the processes' top level modal form.
  260. /// </summary>
  261. public bool IsCurrentModalForm
  262. {
  263. get
  264. {
  265. if (IsInParentModalForms(this))
  266. {
  267. return false;
  268. }
  269. if (this.ContainsFocus)
  270. {
  271. return true;
  272. }
  273. foreach (Form ownedForm in this.OwnedForms)
  274. {
  275. if (ownedForm.ContainsFocus)
  276. {
  277. return true;
  278. }
  279. }
  280. return (this == CurrentModalForm);
  281. }
  282. }
  283. private bool IsTargetFormActive(object target)
  284. {
  285. Control targetControl = null;
  286. if (targetControl == null)
  287. {
  288. Control asControl = target as Control;
  289. if (asControl != null)
  290. {
  291. targetControl = asControl;
  292. }
  293. }
  294. if (targetControl == null)
  295. {
  296. IFormAssociate asIFormAssociate = target as IFormAssociate;
  297. if (asIFormAssociate != null)
  298. {
  299. targetControl = asIFormAssociate.AssociatedForm;
  300. }
  301. }
  302. // target is not a control, or a type of non-control that we recognize as hosted by a control
  303. if (targetControl == null)
  304. {
  305. return false;
  306. }
  307. Form targetForm = targetControl.FindForm();
  308. // target is not on a form
  309. if (targetForm == null)
  310. {
  311. return false;
  312. }
  313. // is the target on the currently active form?
  314. Form activeModalForm = CurrentModalForm;
  315. if (targetForm == activeModalForm)
  316. {
  317. return true;
  318. }
  319. // Nope.
  320. return false;
  321. }
  322. private static object GetConcreteTarget(object target)
  323. {
  324. Delegate asDelegate = target as Delegate;
  325. if (asDelegate == null)
  326. {
  327. return target;
  328. }
  329. else
  330. {
  331. return GetConcreteTarget(asDelegate.Target);
  332. }
  333. }
  334. private bool ProcessFormHotKey(Keys keyData)
  335. {
  336. bool processed = false;
  337. if (this.processFormHotKeyMutex)
  338. {
  339. processed = true;
  340. }
  341. else
  342. {
  343. this.processFormHotKeyMutex = true;
  344. try
  345. {
  346. if (hotkeyRegistrar != null && hotkeyRegistrar.ContainsKey(keyData))
  347. {
  348. Function<bool, Keys> theDelegate = hotkeyRegistrar[keyData];
  349. Delegate[] invokeList = theDelegate.GetInvocationList();
  350. for (int i = invokeList.Length - 1; i >= 0; --i)
  351. {
  352. Function<bool, Keys> invokeMe = (Function<bool, Keys>)invokeList[i];
  353. object concreteTarget = GetConcreteTarget(invokeMe.Target);
  354. if (IsTargetFormActive(concreteTarget))
  355. {
  356. bool result = invokeMe(keyData);
  357. if (result)
  358. {
  359. // The callback handled the key.
  360. processed = true;
  361. break;
  362. }
  363. }
  364. }
  365. }
  366. }
  367. finally
  368. {
  369. this.processFormHotKeyMutex = false;
  370. }
  371. }
  372. return processed;
  373. }
  374. private void OnProcessCmdKeyRelay(object sender, FormEx.ProcessCmdKeyEventArgs e)
  375. {
  376. bool handled = e.Handled;
  377. if (!handled)
  378. {
  379. handled = ProcessCmdKeyData(e.KeyData);
  380. e.Handled = handled;
  381. }
  382. }
  383. public bool RelayProcessCmdKey(ref Message msg, Keys keyData)
  384. {
  385. return ProcessCmdKeyData(keyData);
  386. }
  387. protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
  388. {
  389. bool processed = ProcessCmdKeyData(keyData);
  390. if (!processed)
  391. {
  392. processed = base.ProcessCmdKey(ref msg, keyData);
  393. }
  394. return processed;
  395. }
  396. private bool ProcessCmdKeyData(Keys keyData)
  397. {
  398. bool shouldHandle = ShouldProcessHotKey(keyData);
  399. if (shouldHandle)
  400. {
  401. bool processed = ProcessFormHotKey(keyData);
  402. return processed;
  403. }
  404. else
  405. {
  406. return false;
  407. }
  408. }
  409. public static void UpdateAllForms()
  410. {
  411. try
  412. {
  413. foreach (Form form in Application.OpenForms)
  414. {
  415. try
  416. {
  417. form.Update();
  418. }
  419. catch (Exception)
  420. {
  421. }
  422. }
  423. }
  424. catch (InvalidOperationException)
  425. {
  426. }
  427. }
  428. /*protected override void OnHelpRequested(HelpEventArgs hevent)
  429. {
  430. if (!hevent.Handled)
  431. {
  432. Utility.ShowHelp(this);
  433. hevent.Handled = true;
  434. }
  435. base.OnHelpRequested(hevent);
  436. }*/
  437. public static EventHandler EnableOpacityChanged;
  438. private static void OnEnableOpacityChanged()
  439. {
  440. if (EnableOpacityChanged != null)
  441. {
  442. EnableOpacityChanged(null, EventArgs.Empty);
  443. }
  444. }
  445. public bool EnableInstanceOpacity
  446. {
  447. get
  448. {
  449. return instanceEnableOpacity;
  450. }
  451. set
  452. {
  453. instanceEnableOpacity = value;
  454. this.DecideOpacitySetting();
  455. }
  456. }
  457. /// <summary>
  458. /// Gets or sets a flag that enables or disables opacity for all PdnBaseForm instances.
  459. /// If a particular form's EnableInstanceOpacity property is false, that will override
  460. /// this property being 'true'.
  461. /// </summary>
  462. public static bool EnableOpacity
  463. {
  464. get
  465. {
  466. return globalEnableOpacity;
  467. }
  468. set
  469. {
  470. globalEnableOpacity = value;
  471. OnEnableOpacityChanged();
  472. }
  473. }
  474. /// <summary>
  475. /// Gets or sets the titlebar rendering behavior for when the form is deactivated.
  476. /// </summary>
  477. /// <remarks>
  478. /// If this property is false, the titlebar will be rendered in a different color when the form
  479. /// is inactive as opposed to active. If this property is true, it will always render with the
  480. /// active style. If the whole application is deactivated, the title bar will still be drawn in
  481. /// an inactive state.
  482. /// </remarks>
  483. public bool ForceActiveTitleBar
  484. {
  485. get
  486. {
  487. return this.formEx.ForceActiveTitleBar;
  488. }
  489. set
  490. {
  491. this.formEx.ForceActiveTitleBar = value;
  492. }
  493. }
  494. private ThreadPriority originalPriority;
  495. protected override void OnScroll(ScrollEventArgs se)
  496. {
  497. Thread.CurrentThread.Priority = this.originalPriority;
  498. base.OnScroll(se);
  499. }
  500. public PdnBaseForm()
  501. {
  502. this.originalPriority = Thread.CurrentThread.Priority;
  503. Thread.CurrentThread.Priority = ThreadPriority.AboveNormal;
  504. UI.InitScaling(this);
  505. this.SuspendLayout();
  506. InitializeComponent();
  507. this.formEx = new PaintDotNet.SystemLayer.FormEx(this, new RealParentWndProcDelegate(this.RealWndProc));
  508. this.Controls.Add(this.formEx);
  509. this.formEx.Visible = false;
  510. DecideOpacitySetting();
  511. this.ResumeLayout(false);
  512. this.formEx.ProcessCmdKeyRelay += OnProcessCmdKeyRelay;
  513. }
  514. protected override void OnLoad(EventArgs e)
  515. {
  516. if (!this.DesignMode)
  517. {
  518. LoadResources();
  519. }
  520. base.OnLoad(e);
  521. }
  522. public virtual void LoadResources()
  523. {
  524. if (!this.DesignMode)
  525. {
  526. string stringName = this.Name + ".Localized";
  527. string stringValue = StringsResourceManager.GetString(stringName);
  528. if (stringValue != null)
  529. {
  530. try
  531. {
  532. bool boolValue = bool.Parse(stringValue);
  533. if (boolValue)
  534. {
  535. LoadLocalizedResources();
  536. }
  537. }
  538. catch (Exception)
  539. {
  540. }
  541. }
  542. }
  543. }
  544. protected virtual ResourceManager StringsResourceManager
  545. {
  546. get
  547. {
  548. return PdnResources.Strings;
  549. }
  550. }
  551. private void LoadLocalizedResources()
  552. {
  553. LoadLocalizedResources(this.Name, this);
  554. }
  555. private void ParsePair(string theString, out int x, out int y)
  556. {
  557. string[] split = theString.Split(',');
  558. x = int.Parse(split[0]);
  559. y = int.Parse(split[1]);
  560. }
  561. private void LoadLocalizedResources(string baseName, Control control)
  562. {
  563. // Text
  564. string textStringName = baseName + ".Text";
  565. string textString = this.StringsResourceManager.GetString(textStringName);
  566. if (textString != null)
  567. {
  568. control.Text = textString;
  569. }
  570. // Location
  571. string locationStringName = baseName + ".Location";
  572. string locationString = this.StringsResourceManager.GetString(locationStringName);
  573. if (locationString != null)
  574. {
  575. try
  576. {
  577. int x;
  578. int y;
  579. ParsePair(locationString, out x, out y);
  580. control.Location = new Point(x, y);
  581. }
  582. catch (Exception ex)
  583. {
  584. Tracing.Ping(locationStringName + " is invalid: " + locationString + ", exception: " + ex.ToString());
  585. }
  586. }
  587. // Size
  588. string sizeStringName = baseName + ".Size";
  589. string sizeString = this.StringsResourceManager.GetString(sizeStringName);
  590. if (sizeString != null)
  591. {
  592. try
  593. {
  594. int width;
  595. int height;
  596. ParsePair(sizeString, out width, out height);
  597. control.Size = new Size(width, height);
  598. }
  599. catch (Exception ex)
  600. {
  601. Tracing.Ping(sizeStringName + " is invalid: " + sizeString + ", exception: " + ex.ToString());
  602. }
  603. }
  604. // Recurse
  605. foreach (Control child in control.Controls)
  606. {
  607. if (child.Name == null || child.Name.Length > 0)
  608. {
  609. string newBaseName = baseName + "." + child.Name;
  610. LoadLocalizedResources(newBaseName, child);
  611. }
  612. else
  613. {
  614. Tracing.Ping("Name property not set for an instance of " + child.GetType().Name + " within " + baseName);
  615. }
  616. }
  617. }
  618. protected override void OnClosing(CancelEventArgs e)
  619. {
  620. base.OnClosing(e);
  621. if (!e.Cancel)
  622. {
  623. this.ForceActiveTitleBar = false;
  624. }
  625. }
  626. private void EnableOpacityChangedHandler(object sender, EventArgs e)
  627. {
  628. DecideOpacitySetting();
  629. }
  630. protected override void OnHandleCreated(EventArgs e)
  631. {
  632. base.OnHandleCreated(e);
  633. PdnBaseForm.EnableOpacityChanged += new EventHandler(EnableOpacityChangedHandler);
  634. UserSessions.SessionChanged += new EventHandler(UserSessions_SessionChanged);
  635. DecideOpacitySetting();
  636. }
  637. protected override void OnHandleDestroyed(EventArgs e)
  638. {
  639. base.OnHandleDestroyed(e);
  640. PdnBaseForm.EnableOpacityChanged -= new EventHandler(EnableOpacityChangedHandler);
  641. UserSessions.SessionChanged -= new EventHandler(UserSessions_SessionChanged);
  642. }
  643. /// <summary>
  644. /// Clean up any resources being used.
  645. /// </summary>
  646. protected override void Dispose(bool disposing)
  647. {
  648. if (disposing)
  649. {
  650. if (components != null)
  651. {
  652. components.Dispose();
  653. components = null;
  654. }
  655. }
  656. base.Dispose(disposing);
  657. }
  658. /// <summary>
  659. /// Sets the opacity of the form.
  660. /// </summary>
  661. /// <param name="newOpacity">The new opacity value.</param>
  662. /// <remarks>
  663. /// Depending on the system configuration, this request may be ignored. For example,
  664. /// when running within a Terminal Service (or Remote Desktop) session, opacity will
  665. /// always be set to 1.0 for performance reasons.
  666. /// </remarks>
  667. public new double Opacity
  668. {
  669. get
  670. {
  671. return this.ourOpacity;
  672. }
  673. set
  674. {
  675. if (enableOpacity)
  676. {
  677. // Bypassing Form.Opacity eliminates a "black flickering" that occurs when
  678. // the form transitions from Opacity=1.0 to Opacity != 1.0, or vice versa.
  679. // It appears to be a result of toggling the WS_EX_LAYERED style, or the
  680. // fact that Form.Opacity re-applies visual styles when this value transition
  681. // takes place.
  682. SystemLayer.UI.SetFormOpacity(this, value);
  683. }
  684. this.ourOpacity = value;
  685. }
  686. }
  687. /// <summary>
  688. /// Decides whether or not to have opacity be enabled.
  689. /// </summary>
  690. private void DecideOpacitySetting()
  691. {
  692. if (UserSessions.IsRemote || !PdnBaseForm.globalEnableOpacity || !this.EnableInstanceOpacity)
  693. {
  694. if (this.enableOpacity)
  695. {
  696. try
  697. {
  698. UI.SetFormOpacity(this, 1.0);
  699. }
  700. // This fails in certain odd situations (bug #746), so we just eat the exception.
  701. catch (System.ComponentModel.Win32Exception)
  702. {
  703. }
  704. }
  705. this.enableOpacity = false;
  706. }
  707. else
  708. {
  709. if (!this.enableOpacity)
  710. {
  711. // This fails in certain odd situations (bug #746), so we just eat the exception.
  712. try
  713. {
  714. UI.SetFormOpacity(this, this.ourOpacity);
  715. }
  716. catch (System.ComponentModel.Win32Exception)
  717. {
  718. }
  719. }
  720. this.enableOpacity = true;
  721. }
  722. }
  723. public double ScreenAspect
  724. {
  725. get
  726. {
  727. Rectangle bounds = System.Windows.Forms.Screen.FromControl(this).Bounds;
  728. double aspect = (double)bounds.Width / (double)bounds.Height;
  729. return aspect;
  730. }
  731. }
  732. #region Windows Form Designer generated code
  733. /// <summary>
  734. /// Required method for Designer support - do not modify
  735. /// the contents of this method with the code editor.
  736. /// </summary>
  737. private void InitializeComponent()
  738. {
  739. this.components = new System.ComponentModel.Container();
  740. this.SuspendLayout();
  741. //
  742. // PdnBaseForm
  743. //
  744. this.AutoScaleDimensions = new SizeF(96F, 96F);
  745. this.AutoScaleMode = AutoScaleMode.Dpi;
  746. this.ClientSize = new System.Drawing.Size(291, 270);
  747. this.Name = "PdnBaseForm";
  748. this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
  749. this.Text = "PdnBaseForm";
  750. this.ResumeLayout(false);
  751. }
  752. #endregion
  753. public event MovingEventHandler Moving;
  754. protected virtual void OnMoving(MovingEventArgs mea)
  755. {
  756. if (Moving != null)
  757. {
  758. Moving(this, mea);
  759. }
  760. }
  761. public event CancelEventHandler QueryEndSession;
  762. protected virtual void OnQueryEndSession(CancelEventArgs e)
  763. {
  764. if (QueryEndSession != null)
  765. {
  766. QueryEndSession(this, e);
  767. }
  768. }
  769. private void UserSessions_SessionChanged(object sender, EventArgs e)
  770. {
  771. this.DecideOpacitySetting();
  772. }
  773. void RealWndProc(ref Message m)
  774. {
  775. OurWndProc(ref m);
  776. }
  777. protected override void WndProc(ref Message m)
  778. {
  779. if (this.formEx == null)
  780. {
  781. base.WndProc(ref m);
  782. }
  783. else if (!this.formEx.HandleParentWndProc(ref m))
  784. {
  785. OurWndProc(ref m);
  786. }
  787. }
  788. private void OurWndProc(ref Message m)
  789. {
  790. switch (m.Msg)
  791. {
  792. case 0x0216: // WM_MOVING
  793. unsafe
  794. {
  795. int* p = (int*)m.LParam;
  796. Rectangle rect = Rectangle.FromLTRB(p[0], p[1], p[2], p[3]);
  797. MovingEventArgs mea = new MovingEventArgs(rect);
  798. OnMoving(mea);
  799. p[0] = mea.Rectangle.Left;
  800. p[1] = mea.Rectangle.Top;
  801. p[2] = mea.Rectangle.Right;
  802. p[3] = mea.Rectangle.Bottom;
  803. m.Result = new IntPtr(1);
  804. }
  805. break;
  806. // WinForms doesn't handle this message correctly and wrongly returns 0 instead of 1.
  807. case 0x0011: // WM_QUERYENDSESSION
  808. CancelEventArgs e = new CancelEventArgs();
  809. OnQueryEndSession(e);
  810. m.Result = e.Cancel ? IntPtr.Zero : new IntPtr(1);
  811. break;
  812. default:
  813. base.WndProc(ref m);
  814. break;
  815. }
  816. }
  817. public SnapManager SnapManager
  818. {
  819. get
  820. {
  821. if (this.snapManager == null)
  822. {
  823. this.snapManager = new SnapManager();
  824. }
  825. return snapManager;
  826. }
  827. }
  828. public Size ClientSizeToWindowSize(Size clientSize)
  829. {
  830. Size baseClientSize = ClientSize;
  831. Size baseWindowSize = Size;
  832. int extraWidth = baseWindowSize.Width - baseClientSize.Width;
  833. int extraHeight = baseWindowSize.Height - baseClientSize.Height;
  834. Size windowSize = new Size(clientSize.Width + extraWidth, clientSize.Height + extraHeight);
  835. return windowSize;
  836. }
  837. public Size WindowSizeToClientSize(Size windowSize)
  838. {
  839. Size baseClientSize = ClientSize;
  840. Size baseWindowSize = Size;
  841. int extraWidth = baseWindowSize.Width - baseClientSize.Width;
  842. int extraHeight = baseWindowSize.Height - baseClientSize.Height;
  843. Size clientSize = new Size(windowSize.Width - extraWidth, windowSize.Height - extraHeight);
  844. return clientSize;
  845. }
  846. public Rectangle ClientBoundsToWindowBounds(Rectangle clientBounds)
  847. {
  848. Rectangle currentBounds = this.Bounds;
  849. Rectangle currentClientBounds = this.RectangleToScreen(ClientRectangle);
  850. Rectangle newWindowBounds = new Rectangle(
  851. clientBounds.Left - (currentClientBounds.Left - currentBounds.Left),
  852. clientBounds.Top - (currentClientBounds.Top - currentBounds.Top),
  853. clientBounds.Width + (currentBounds.Width - currentClientBounds.Width),
  854. clientBounds.Height + (currentBounds.Height - currentClientBounds.Height));
  855. return newWindowBounds;
  856. }
  857. public Rectangle WindowBoundsToClientBounds(Rectangle windowBounds)
  858. {
  859. Rectangle currentBounds = this.Bounds;
  860. Rectangle currentClientBounds = this.RectangleToScreen(ClientRectangle);
  861. Rectangle newClientBounds = new Rectangle(
  862. windowBounds.Left + (currentClientBounds.Left - currentBounds.Left),
  863. windowBounds.Top + (currentClientBounds.Top - currentBounds.Top),
  864. windowBounds.Width - (currentBounds.Width - currentClientBounds.Width),
  865. windowBounds.Height - (currentBounds.Height - currentClientBounds.Height));
  866. return newClientBounds;
  867. }
  868. public void EnsureFormIsOnScreen()
  869. {
  870. if (this.WindowState == FormWindowState.Maximized)
  871. {
  872. return;
  873. }
  874. if (this.WindowState == FormWindowState.Minimized)
  875. {
  876. return;
  877. }
  878. Screen ourScreen;
  879. try
  880. {
  881. ourScreen = Screen.FromControl(this);
  882. }
  883. catch (Exception)
  884. {
  885. ourScreen = null;
  886. }
  887. if (ourScreen == null)
  888. {
  889. ourScreen = Screen.PrimaryScreen;
  890. }
  891. Rectangle currentBounds = Bounds;
  892. Rectangle newBounds = EnsureRectIsOnScreen(ourScreen, currentBounds);
  893. Bounds = newBounds;
  894. }
  895. public static Rectangle EnsureRectIsOnScreen(Screen screen, Rectangle bounds)
  896. {
  897. Rectangle newBounds = bounds;
  898. Rectangle screenBounds = screen.WorkingArea;
  899. // Make sure the bottom and right do not fall off the edge, by moving the bounds
  900. if (newBounds.Right > screenBounds.Right)
  901. {
  902. newBounds.X -= (newBounds.Right - screenBounds.Right);
  903. }
  904. if (newBounds.Bottom > screenBounds.Bottom)
  905. {
  906. newBounds.Y -= (newBounds.Bottom - screenBounds.Bottom);
  907. }
  908. // Make sure the top and left haven't fallen off, by moving
  909. if (newBounds.Left < screenBounds.Left)
  910. {
  911. newBounds.X = screenBounds.Left;
  912. }
  913. if (newBounds.Top < screenBounds.Top)
  914. {
  915. newBounds.Y = screenBounds.Top;
  916. }
  917. // Make sure that we are not too wide / tall, by resizing
  918. if (newBounds.Right > screenBounds.Right)
  919. {
  920. newBounds.Width -= (newBounds.Right - screenBounds.Right);
  921. }
  922. if (newBounds.Bottom > screenBounds.Bottom)
  923. {
  924. newBounds.Height -= (newBounds.Bottom - screenBounds.Bottom);
  925. }
  926. // All done.
  927. return newBounds;
  928. }
  929. public virtual void CreateProjectName(AnalyzeSettingModel model)
  930. {
  931. }
  932. public virtual void CreateProjectName(AnalyzeSettingModel model, int id)
  933. {
  934. }
  935. public virtual void GetCreateName(string name)
  936. {
  937. }
  938. public virtual void GetCreateName(string name, string desc)
  939. {
  940. }
  941. /// <summary>
  942. /// 从xml读取对应分析报告的设置项
  943. /// </summary>
  944. public void SetAnalyzeModelFromXml(string languageName)
  945. {
  946. string filePath = Application.StartupPath + "\\Config\\" + xmlPath + "\\" + "AnalyzeSavedModel.xml";
  947. if (System.IO.File.Exists(filePath))
  948. {
  949. AnalyzeSavedModel analyzeSavedModel = XmlSerializeHelper.DESerializer<AnalyzeSavedModel>(FileOperationHelper.ReadStringFromFile(filePath, FileMode.Open));
  950. if (analyzeSavedModel.modelItems != null && analyzeSavedModel.modelItems.Count > 0)
  951. {
  952. AnalyzeSavedModel.ModelItem modelItem = analyzeSavedModel.modelItems.Find(a => a.languageName == languageName);
  953. if(modelItem != null && modelItem.analyzeSettingModel != null)
  954. {
  955. this.analyzeSettingModel = modelItem.analyzeSettingModel;
  956. }
  957. }
  958. }
  959. }
  960. /// <summary>
  961. /// 计算并修改DataGridView的列宽度
  962. /// 使其看起来舒服一点
  963. /// </summary>
  964. /// <param name="dgViewFiles"></param>
  965. /// <param name="offset"></param>
  966. public void AutoSizeColumn(DataGridView dgViewFiles, int offset)
  967. {
  968. int width = 0;
  969. //使列自使用宽度
  970. //对于DataGridView的每一个列都调整
  971. for (int i = 0; i < dgViewFiles.Columns.Count; i++)
  972. {
  973. //将每一列都调整为自动适应模式
  974. dgViewFiles.AutoResizeColumn(i, DataGridViewAutoSizeColumnMode.AllCells);
  975. //记录整个DataGridView的宽度
  976. width += dgViewFiles.Columns[i].Width + offset;
  977. }
  978. //判断调整后的宽度与原来设定的宽度的关系,如果是调整后的宽度大于原来设定的宽度,
  979. //则将DataGridView的列自动调整模式设置为显示的列即可,
  980. //如果是小于原来设定的宽度,将模式改为填充。
  981. if (width > dgViewFiles.Size.Width)
  982. {
  983. dgViewFiles.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
  984. for (int i = 0; i < dgViewFiles.Columns.Count; i++)
  985. {
  986. dgViewFiles.Columns[i].Width += offset;
  987. }
  988. }
  989. else
  990. {
  991. for (int i = 0; i < dgViewFiles.Columns.Count; i++)
  992. {
  993. dgViewFiles.Columns[i].Width = (int)(dgViewFiles.Size.Width / dgViewFiles.Columns.Count);
  994. }
  995. //dgViewFiles.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
  996. }
  997. //冻结某列 从左开始 0,1,2
  998. //dgViewFiles.Columns[1].Frozen = true;
  999. }
  1000. }
  1001. }