CNAS取数仪器端升级
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FormSizeHelper.cs 7.9KB

4 달 전
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows.Forms;
  7. using System.Configuration;
  8. using System.Collections;
  9. using System.Drawing;
  10. using System.IO;
  11. using CnasSynchronousCommon;
  12. using System.Xml;
  13. namespace CNAS_DBSync
  14. {
  15. struct FieldLine
  16. {
  17. Label label;
  18. Control editor;
  19. Button button;
  20. int size;
  21. public FieldLine(Label label, Control editor, Button button = null)
  22. {
  23. this.label = label;
  24. this.editor = editor;
  25. this.button = button;
  26. int count = 0;
  27. if (label != null) count++;
  28. if (editor != null) count++;
  29. if (button != null) count++;
  30. size = count;
  31. }
  32. public Label Label
  33. {
  34. get
  35. {
  36. return label;
  37. }
  38. }
  39. public Control Editor
  40. {
  41. get
  42. {
  43. return editor;
  44. }
  45. }
  46. public Button Button
  47. {
  48. get
  49. {
  50. return button;
  51. }
  52. }
  53. }
  54. /// <summary>
  55. /// 设置容器内编辑字段位置的公共算法
  56. /// </summary>
  57. class FormSizeHelper
  58. {
  59. private static readonly string strLocalConfigPath = FileHelper.getBasePath() + @"\Data\";
  60. private static readonly string FormSizeXml = "FormSize.xml";
  61. private static readonly FormSizeHelper instance;
  62. private Hashtable formSizes;
  63. // 静态构造函数确保线程安全且延迟加载。
  64. static FormSizeHelper() {
  65. instance = new FormSizeHelper();
  66. }
  67. // 防止外部实例化。
  68. private FormSizeHelper() {
  69. Init();
  70. }
  71. public static FormSizeHelper Instance => instance; // 提供全局访问点。
  72. public static void Arrange(Panel container, FieldLine[] fields, int span1 = 1, int span2 = 1)
  73. {
  74. int colLabel = 0;
  75. int colEditor = 0;
  76. int colButton = 0;
  77. int lbLeft = -1;
  78. for (int i = 0; i < fields.Length; i++)
  79. {
  80. if (lbLeft == -1)
  81. {
  82. if (fields[i].Label != null)
  83. lbLeft = fields[i].Label.Left;
  84. }
  85. else if (fields[i].Label != null && fields[i].Label.Left >= 0 && fields[i].Label.Left < lbLeft)
  86. {
  87. lbLeft = fields[i].Label.Left;
  88. }
  89. }
  90. int width = lbLeft < 0 ? (container.Width * 80 / 100) : (container.Width-lbLeft*2);
  91. int marginLeft = (container.Width - width) / 2;
  92. for (int i=0;i< fields.Length; i++)
  93. {
  94. if(fields[i].Label != null && fields[i].Label.Width > colLabel)
  95. {
  96. colLabel = fields[i].Label.Width;
  97. }
  98. if (fields[i].Button != null && fields[i].Button.Width > colButton)
  99. {
  100. colButton = fields[i].Button.Width;
  101. }
  102. }
  103. colEditor = width - (colLabel + span1 + span2 + colButton);
  104. for (int i = 0; i < fields.Length; i++)
  105. {
  106. //Label不动
  107. //改变Editor
  108. if (fields[i].Editor != null)
  109. {
  110. if(fields[i].Editor is TextBox)
  111. {
  112. TextBox tb = (TextBox)fields[i].Editor;
  113. //tb.Left = marginLeft + colLabel + span1;
  114. tb.Width = colEditor;
  115. if(fields[i].Button != null)
  116. {
  117. fields[i].Button.Left = tb.Left + colEditor + span2;
  118. }
  119. }
  120. if(fields[i].Editor is CheckBox)
  121. {
  122. //不动
  123. }
  124. if(fields[i].Editor is ComboBox)
  125. {
  126. ComboBox tb = (ComboBox)fields[i].Editor;
  127. //tb.Left = marginLeft + colLabel + span1;
  128. tb.Width = colEditor;
  129. if (fields[i].Button != null)
  130. {
  131. fields[i].Button.Left = tb.Left + colEditor + span2;
  132. }
  133. }
  134. }
  135. }
  136. }
  137. private static bool ValidBound(Form form, int x, int y, int w, int h)
  138. {
  139. if (x>=0 && y >= 0 && w>0 && h > 0)
  140. {
  141. Screen screen = Screen.FromControl(form);
  142. Rectangle r = screen.Bounds;
  143. return (x + w < r.Width && y + h < r.Height);
  144. }
  145. return false;
  146. }
  147. private void Init()
  148. {
  149. formSizes = new Hashtable(10);
  150. string strUrl = strLocalConfigPath + FormSizeXml;
  151. try
  152. {
  153. if (!File.Exists(strUrl)) return;
  154. XmlDocument doc = new XmlDocument();
  155. doc.Load(strUrl);
  156. XmlNode root = doc.DocumentElement;
  157. foreach (XmlNode node in root.ChildNodes)
  158. {
  159. string formname = node.Name;
  160. string value = node.InnerText;
  161. formSizes.Add(formname, value);
  162. }
  163. }
  164. catch (Exception ex)
  165. {
  166. AppLog.Error(ex.Message);
  167. }
  168. }
  169. public List<string> LoadDefaultSize(Form form, string key, out bool changed)
  170. {
  171. changed = false;
  172. if (formSizes.ContainsKey(key))
  173. {
  174. string value = formSizes[key] as string;
  175. string[] values = value.Split(',');
  176. if (values.Length >= 4)
  177. {
  178. int x, y, w, h;
  179. if (int.TryParse(values[0], out x) && int.TryParse(values[1], out y) &&
  180. int.TryParse(values[2], out w) && int.TryParse(values[3], out h))
  181. {
  182. if (ValidBound(form, x, y, w, h))
  183. {
  184. form.Left = x;
  185. form.Top = y;
  186. form.Width = w;
  187. form.Height = h;
  188. changed = true;
  189. }
  190. }
  191. if (changed && values.Length > 4)
  192. {
  193. List<string> list = new List<string>(values.Length - 4);
  194. for (int i = 4; i < values.Length; i++)
  195. {
  196. list.Add(values[i]);
  197. }
  198. return list;
  199. }
  200. }
  201. }
  202. return null;
  203. }
  204. public bool SaveDefaultSize(string key, string value)
  205. {
  206. if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value))
  207. {
  208. formSizes[key] = value;
  209. StringBuilder sb = new StringBuilder();
  210. sb.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<Forms>\n");
  211. foreach(object k in formSizes.Keys)
  212. {
  213. string line = string.Format("<{0}>{1}</{0}>", k, formSizes[k]);
  214. sb.Append(line).Append("\n");
  215. }
  216. sb.Append("</Forms>");
  217. try
  218. {
  219. bool bIfSuccess = FileHelper.SaveLocalFile(strLocalConfigPath, FormSizeXml, sb.ToString());
  220. return bIfSuccess;
  221. }
  222. catch (Exception ex)
  223. {
  224. AppLog.Error(ex.Message);
  225. }
  226. }
  227. return true;
  228. }
  229. }
  230. }