BaseForm.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Drawing;
  5. using System.Windows.Forms;
  6. using SAGA.DotNetUtils.Configration;
  7. namespace SAGA.DotNetUtils.WinForms
  8. {
  9. public class BaseForm : FormDataProvide
  10. {
  11. protected bool IsNeedEscClose = true;
  12. protected bool IsSaveFormData = true;
  13. public BaseForm()
  14. {
  15. base.ShowInTaskbar = false;
  16. base.FormBorderStyle = FormBorderStyle.FixedSingle;
  17. base.Icon = AppBaseInfo.AppIcon;
  18. this.IsSaveFormData = true;
  19. }
  20. protected void AbandonExtendComboxDropDownWidth()
  21. {
  22. foreach (Control control in base.FormControls)
  23. {
  24. if (control is ComboBox)
  25. {
  26. ((ComboBox) control).DropDown -= new EventHandler(this.ComboBox_DropDown);
  27. }
  28. }
  29. }
  30. protected void AutoExtendComboxDropDownWidth()
  31. {
  32. foreach (Control control in base.FormControls)
  33. {
  34. if (control is ComboBox)
  35. {
  36. ((ComboBox) control).DropDown += new EventHandler(this.ComboBox_DropDown);
  37. }
  38. }
  39. }
  40. private void ComboBox_DropDown(object sender, EventArgs e)
  41. {
  42. ComboBox box = sender as ComboBox;
  43. if (box != null)
  44. {
  45. int dropDownWidth = box.DropDownWidth;
  46. using (Graphics graphics = box.CreateGraphics())
  47. {
  48. int num2 = (box.Items.Count > box.MaxDropDownItems) ? (SystemInformation.VerticalScrollBarWidth + 5) : 0;
  49. Font font = box.Font;
  50. foreach (object obj2 in box.Items)
  51. {
  52. string text = obj2.ToString();
  53. int num3 = (int) Math.Round((double) (graphics.MeasureString(text, font).Width + num2), 0);
  54. if (dropDownWidth < num3)
  55. {
  56. dropDownWidth = num3;
  57. }
  58. }
  59. if (box.DropDownWidth != dropDownWidth)
  60. {
  61. box.DropDownWidth = dropDownWidth;
  62. }
  63. }
  64. }
  65. }
  66. private void Control_KeyDown(object sender, KeyEventArgs e)
  67. {
  68. if (this.IsNeedEscClose && (e.KeyCode == Keys.Escape))
  69. {
  70. base.Close();
  71. }
  72. }
  73. private void DealKeyDown(Control ctrl)
  74. {
  75. if (ctrl.Controls.Count != 0)
  76. {
  77. foreach (Control control in ctrl.Controls)
  78. {
  79. this.DealKeyDown(control);
  80. }
  81. }
  82. ctrl.KeyDown += new KeyEventHandler(this.Control_KeyDown);
  83. }
  84. protected virtual void InitializeData()
  85. {
  86. }
  87. protected override void OnClosed(EventArgs e)
  88. {
  89. if (this.IsSaveFormData)
  90. {
  91. this.SaveFormData();
  92. }
  93. base.OnClosed(e);
  94. }
  95. protected override void OnLoad(EventArgs e)
  96. {
  97. base.OnLoad(e);
  98. this.InitializeData();
  99. this.AutoExtendComboxDropDownWidth();
  100. Dictionary<string, object> dictionary = this.LoadConfigFrom();
  101. if (dictionary != null)
  102. {
  103. if (dictionary.ContainsKey("FormOldLocation"))
  104. {
  105. int[] numArray = dictionary["FormOldLocation"] as int[];
  106. Point pt = new Point(numArray[0], numArray[1]);
  107. IntPtr mainWindowHandle = Process.GetCurrentProcess().MainWindowHandle;
  108. if ((mainWindowHandle != IntPtr.Zero) && Screen.FromHandle(mainWindowHandle).Bounds.Contains(pt))
  109. {
  110. base.Location = pt;
  111. }
  112. }
  113. if (dictionary.ContainsKey("FormSize") && (base.FormBorderStyle != FormBorderStyle.FixedSingle))
  114. {
  115. int[] numArray2 = dictionary["FormSize"] as int[];
  116. if (numArray2 != null)
  117. {
  118. base.Size = new Size(numArray2[0], numArray2[1]);
  119. }
  120. }
  121. }
  122. foreach (Control control in this.FormControls)
  123. {
  124. this.DealKeyDown(control);
  125. }
  126. base.KeyDown += new KeyEventHandler(this.Control_KeyDown);
  127. }
  128. }
  129. }