PdnBaseForm.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  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. protected virtual 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 = false;
  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. }