ControlHelper.cs 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Diagnostics;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Runtime.InteropServices;
  10. using System.Text;
  11. using System.Text.RegularExpressions;
  12. using System.Threading;
  13. using System.Windows.Forms;
  14. namespace HOZProject
  15. {
  16. /// <summary>
  17. /// Class ControlHelper.
  18. /// </summary>
  19. public static class ControlHelper
  20. {
  21. #region 设置控件Enabled,切不改变控件颜色
  22. /// <summary>
  23. /// 功能描述:设置控件Enabled,切不改变控件颜色
  24. /// 作  者:HZH
  25. /// 创建日期:2019-03-04 13:43:32
  26. /// 任务编号:POS
  27. /// </summary>
  28. /// <param name="c">c</param>
  29. /// <param name="enabled">enabled</param>
  30. public static void SetControlEnabled(this Control c, bool enabled)
  31. {
  32. if (!c.IsDisposed)
  33. {
  34. if (enabled)
  35. {
  36. ControlHelper.SetWindowLong(c.Handle, -16, -134217729 & ControlHelper.GetWindowLong(c.Handle, -16));
  37. }
  38. else
  39. {
  40. ControlHelper.SetWindowLong(c.Handle, -16, 134217728 + ControlHelper.GetWindowLong(c.Handle, -16));
  41. }
  42. }
  43. }
  44. /// <summary>
  45. /// 功能描述:设置控件Enabled,切不改变控件颜色
  46. /// 作  者:HZH
  47. /// 创建日期:2019-03-04 13:43:32
  48. /// 任务编号:POS
  49. /// </summary>
  50. /// <param name="cs">cs</param>
  51. /// <param name="enabled">enabled</param>
  52. public static void SetControlEnableds(Control[] cs, bool enabled)
  53. {
  54. for (int i = 0; i < cs.Length; i++)
  55. {
  56. Control c = cs[i];
  57. SetControlEnabled(c, enabled);
  58. }
  59. }
  60. #endregion
  61. /// <summary>
  62. /// Sets the window long.
  63. /// </summary>
  64. /// <param name="hWnd">The h WND.</param>
  65. /// <param name="nIndex">Index of the n.</param>
  66. /// <param name="wndproc">The wndproc.</param>
  67. /// <returns>System.Int32.</returns>
  68. [DllImport("user32.dll ")]
  69. public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int wndproc);
  70. /// <summary>
  71. /// Gets the window long.
  72. /// </summary>
  73. /// <param name="hWnd">The h WND.</param>
  74. /// <param name="nIndex">Index of the n.</param>
  75. /// <returns>System.Int32.</returns>
  76. [DllImport("user32.dll ")]
  77. public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
  78. /// <summary>
  79. /// Gets the foreground window.
  80. /// </summary>
  81. /// <returns>IntPtr.</returns>
  82. [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
  83. public static extern IntPtr GetForegroundWindow();
  84. /// <summary>
  85. /// Threads the base call back.
  86. /// </summary>
  87. /// <param name="parent">The parent.</param>
  88. /// <param name="obj">The object.</param>
  89. private static void ThreadBaseCallBack(Control parent, object obj)
  90. {
  91. if (obj is Exception)
  92. {
  93. if (parent != null)
  94. {
  95. ThreadInvokerControl(parent, delegate
  96. {
  97. Exception ex = obj as Exception;
  98. });
  99. }
  100. }
  101. }
  102. /// <summary>
  103. /// 委托调用主线程控件
  104. /// </summary>
  105. /// <param name="parent">主线程控件</param>
  106. /// <param name="action">修改控件方法</param>
  107. public static void ThreadInvokerControl(Control parent, Action action)
  108. {
  109. if (parent != null)
  110. {
  111. if (parent.InvokeRequired)
  112. {
  113. parent.BeginInvoke(action);
  114. }
  115. else
  116. {
  117. action();
  118. SetForegroundWindow(parent.Handle);
  119. }
  120. }
  121. }
  122. /// <summary>
  123. /// Sets the foreground window.
  124. /// </summary>
  125. /// <param name="hWnd">The h WND.</param>
  126. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
  127. [DllImport("user32.dll")]
  128. public static extern bool SetForegroundWindow(IntPtr hWnd);
  129. /// <summary>
  130. /// Closes the process panel.
  131. /// </summary>
  132. /// <param name="parent">The parent.</param>
  133. public static void CloseProcessPanel(Control parent)
  134. {
  135. if (parent.InvokeRequired)
  136. {
  137. parent.BeginInvoke(new MethodInvoker(delegate
  138. {
  139. CloseProcessPanel(parent);
  140. }));
  141. }
  142. else if (parent != null)
  143. {
  144. Control control = HaveProcessPanelControl(parent);
  145. if (control != null)
  146. {
  147. Form frm = control.Tag as Form;
  148. if (frm != null && !frm.IsDisposed && frm.Visible)
  149. {
  150. if (frm.InvokeRequired)
  151. {
  152. frm.BeginInvoke(new MethodInvoker(delegate
  153. {
  154. frm.Hide();
  155. }));
  156. }
  157. else
  158. {
  159. frm.Hide();
  160. }
  161. }
  162. }
  163. }
  164. }
  165. /// <summary>
  166. /// Haves the process panel control.
  167. /// </summary>
  168. /// <param name="parent">The parent.</param>
  169. /// <returns>Control.</returns>
  170. public static Control HaveProcessPanelControl(Control parent)
  171. {
  172. Control[] array = parent.Controls.Find("myprogressPanelext", false);
  173. Control result;
  174. if (array.Length > 0)
  175. {
  176. result = array[0];
  177. }
  178. else
  179. {
  180. result = null;
  181. }
  182. return result;
  183. }
  184. /// <summary>
  185. /// Converts to array.
  186. /// </summary>
  187. /// <param name="controls">The controls.</param>
  188. /// <returns>Control[].</returns>
  189. public static Control[] ToArray(this System.Windows.Forms.Control.ControlCollection controls)
  190. {
  191. if (controls == null || controls.Count <= 0)
  192. return new Control[0];
  193. List<Control> lst = new List<Control>();
  194. foreach (Control item in controls)
  195. {
  196. lst.Add(item);
  197. }
  198. return lst.ToArray();
  199. }
  200. #region 根据控件宽度截取字符串
  201. /// <summary>
  202. /// 功能描述:根据控件宽度截取字符串
  203. /// 作  者:HZH
  204. /// 创建日期:2019-06-27 10:49:10
  205. /// 任务编号:POS
  206. /// </summary>
  207. /// <param name="strSource">字符串</param>
  208. /// <param name="fltControlWidth">控件宽度</param>
  209. /// <param name="g">Graphics</param>
  210. /// <param name="font">字体</param>
  211. /// <returns>截取后的字符串</returns>
  212. public static string GetSubString(
  213. string strSource,
  214. float fltControlWidth,
  215. System.Drawing.Graphics g,
  216. System.Drawing.Font font)
  217. {
  218. try
  219. {
  220. fltControlWidth = fltControlWidth - 20;
  221. strSource = strSource.Trim();
  222. while (true)
  223. {
  224. System.Drawing.SizeF sizeF = g.MeasureString(strSource.Replace(" ", "A"), font);
  225. if (sizeF.Width > fltControlWidth)
  226. {
  227. strSource = strSource.TrimEnd('…');
  228. if (strSource.Length <= 1)
  229. return "";
  230. strSource = strSource.Substring(0, strSource.Length - 1).Trim() + "…";
  231. }
  232. else
  233. {
  234. return strSource;
  235. }
  236. }
  237. }
  238. finally
  239. {
  240. g.Dispose();
  241. }
  242. }
  243. #endregion
  244. #region 获取字符串宽度
  245. /// <summary>
  246. /// 功能描述:获取字符串宽度
  247. /// 作  者:HZH
  248. /// 创建日期:2019-06-27 11:54:50
  249. /// 任务编号:POS
  250. /// </summary>
  251. /// <param name="strSource">strSource</param>
  252. /// <param name="g">g</param>
  253. /// <param name="font">font</param>
  254. /// <returns>返回值</returns>
  255. public static int GetStringWidth(
  256. string strSource,
  257. System.Drawing.Graphics g,
  258. System.Drawing.Font font)
  259. {
  260. string[] strs = strSource.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
  261. float fltWidth = 0;
  262. foreach (var item in strs)
  263. {
  264. System.Drawing.SizeF sizeF = g.MeasureString(strSource.Replace(" ", "A"), font);
  265. if (sizeF.Width > fltWidth)
  266. fltWidth = sizeF.Width;
  267. }
  268. return (int)fltWidth;
  269. }
  270. #endregion
  271. #region 动画特效
  272. /// <summary>
  273. /// Animates the window.
  274. /// </summary>
  275. /// <param name="whnd">The WHND.</param>
  276. /// <param name="dwtime">The dwtime.</param>
  277. /// <param name="dwflag">The dwflag.</param>
  278. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
  279. [DllImport("user32.dll")]
  280. public static extern bool AnimateWindow(IntPtr whnd, int dwtime, int dwflag);
  281. //dwflag的取值如下
  282. /// <summary>
  283. /// The aw hor positive
  284. /// </summary>
  285. public const Int32 AW_HOR_POSITIVE = 0x00000001;
  286. //从左到右显示
  287. /// <summary>
  288. /// The aw hor negative
  289. /// </summary>
  290. public const Int32 AW_HOR_NEGATIVE = 0x00000002;
  291. //从右到左显示
  292. /// <summary>
  293. /// The aw ver positive
  294. /// </summary>
  295. public const Int32 AW_VER_POSITIVE = 0x00000004;
  296. //从上到下显示
  297. /// <summary>
  298. /// The aw ver negative
  299. /// </summary>
  300. public const Int32 AW_VER_NEGATIVE = 0x00000008;
  301. //从下到上显示
  302. /// <summary>
  303. /// The aw center
  304. /// </summary>
  305. public const Int32 AW_CENTER = 0x00000010;
  306. //若使用了AW_HIDE标志,则使窗口向内重叠,即收缩窗口;否则使窗口向外扩展,即展开窗口
  307. /// <summary>
  308. /// The aw hide
  309. /// </summary>
  310. public const Int32 AW_HIDE = 0x00010000;
  311. //隐藏窗口,缺省则显示窗口
  312. /// <summary>
  313. /// The aw activate
  314. /// </summary>
  315. public const Int32 AW_ACTIVATE = 0x00020000;
  316. //激活窗口。在使用了AW_HIDE标志后不能使用这个标志
  317. /// <summary>
  318. /// The aw slide
  319. /// </summary>
  320. public const Int32 AW_SLIDE = 0x00040000;
  321. //使用滑动类型。缺省则为滚动动画类型。当使用AW_CENTER标志时,这个标志就被忽略
  322. /// <summary>
  323. /// The aw blend
  324. /// </summary>
  325. public const Int32 AW_BLEND = 0x00080000;
  326. //透明度从高到低
  327. #endregion
  328. #region 冻结控件
  329. /// <summary>
  330. /// The m LST freeze control
  331. /// </summary>
  332. static Dictionary<Control, bool> m_lstFreezeControl = new Dictionary<Control, bool>();
  333. /// <summary>
  334. /// 功能描述:停止更新控件
  335. /// 作  者:HZH
  336. /// 创建日期:2019-07-13 11:11:32
  337. /// 任务编号:POS
  338. /// </summary>
  339. /// <param name="control">control</param>
  340. /// <param name="blnToFreeze">是否停止更新</param>
  341. public static void FreezeControl(Control control, bool blnToFreeze)
  342. {
  343. if (blnToFreeze && control.IsHandleCreated && control.Visible && !control.IsDisposed && (!m_lstFreezeControl.ContainsKey(control) || (m_lstFreezeControl.ContainsKey(control) && m_lstFreezeControl[control] == false)))
  344. {
  345. m_lstFreezeControl[control] = true;
  346. control.Disposed += control_Disposed;
  347. NativeMethods.SendMessage(control.Handle, 11, 0, 0);
  348. }
  349. else if (!blnToFreeze && !control.IsDisposed && m_lstFreezeControl.ContainsKey(control) && m_lstFreezeControl[control] == true)
  350. {
  351. m_lstFreezeControl.Remove(control);
  352. NativeMethods.SendMessage(control.Handle, 11, 1, 0);
  353. control.Invalidate(true);
  354. }
  355. }
  356. /// <summary>
  357. /// Handles the Disposed event of the control control.
  358. /// </summary>
  359. /// <param name="sender">The source of the event.</param>
  360. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  361. static void control_Disposed(object sender, EventArgs e)
  362. {
  363. try
  364. {
  365. if (m_lstFreezeControl.ContainsKey((Control)sender))
  366. m_lstFreezeControl.Remove((Control)sender);
  367. }
  368. catch { }
  369. }
  370. #endregion
  371. /// <summary>
  372. /// 设置GDI高质量模式抗锯齿
  373. /// </summary>
  374. /// <param name="g">The g.</param>
  375. public static void SetGDIHigh(this Graphics g)
  376. {
  377. g.SmoothingMode = SmoothingMode.AntiAlias; //使绘图质量最高,即消除锯齿
  378. g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  379. g.CompositingQuality = CompositingQuality.HighQuality;
  380. }
  381. /// <summary>
  382. /// 根据矩形和圆得到一个圆角矩形Path
  383. /// </summary>
  384. /// <param name="rect">The rect.</param>
  385. /// <param name="cornerRadius">The corner radius.</param>
  386. /// <returns>GraphicsPath.</returns>
  387. public static GraphicsPath CreateRoundedRectanglePath(this Rectangle rect, int cornerRadius)
  388. {
  389. GraphicsPath roundedRect = new GraphicsPath();
  390. roundedRect.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
  391. roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);
  392. roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
  393. roundedRect.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);
  394. roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
  395. roundedRect.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
  396. roundedRect.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
  397. roundedRect.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);
  398. roundedRect.CloseFigure();
  399. return roundedRect;
  400. }
  401. /// <summary>
  402. /// Creates the rounded rectangle path.
  403. /// </summary>
  404. /// <param name="rect">The rect.</param>
  405. /// <param name="cornerRadius">The corner radius.</param>
  406. /// <returns>GraphicsPath.</returns>
  407. public static GraphicsPath CreateRoundedRectanglePath(this RectangleF rect, int cornerRadius)
  408. {
  409. GraphicsPath roundedRect = new GraphicsPath();
  410. roundedRect.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
  411. roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);
  412. roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
  413. roundedRect.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);
  414. roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
  415. roundedRect.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
  416. roundedRect.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
  417. roundedRect.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);
  418. roundedRect.CloseFigure();
  419. return roundedRect;
  420. }
  421. /// <summary>
  422. /// Gets the colors.
  423. /// </summary>
  424. /// <value>The colors.</value>
  425. public static Color[] Colors { get; private set; }
  426. static ControlHelper()
  427. {
  428. List<Color> list = new List<Color>();
  429. list.Add(Color.FromArgb(55, 162, 218));
  430. list.Add(Color.FromArgb(50, 197, 233));
  431. list.Add(Color.FromArgb(103, 224, 227));
  432. list.Add(Color.FromArgb(159, 230, 184));
  433. list.Add(Color.FromArgb(255, 219, 92));
  434. list.Add(Color.FromArgb(255, 159, 127));
  435. list.Add(Color.FromArgb(251, 114, 147));
  436. list.Add(Color.FromArgb(224, 98, 174));
  437. list.Add(Color.FromArgb(230, 144, 209));
  438. list.Add(Color.FromArgb(231, 188, 243));
  439. list.Add(Color.FromArgb(157, 150, 245));
  440. list.Add(Color.FromArgb(131, 120, 234));
  441. list.Add(Color.FromArgb(150, 191, 255));
  442. list.Add(Color.FromArgb(243, 67, 54));
  443. list.Add(Color.FromArgb(156, 39, 176));
  444. list.Add(Color.FromArgb(103, 58, 183));
  445. list.Add(Color.FromArgb(63, 81, 181));
  446. list.Add(Color.FromArgb(33, 150, 243));
  447. list.Add(Color.FromArgb(0, 188, 211));
  448. list.Add(Color.FromArgb(3, 169, 244));
  449. list.Add(Color.FromArgb(0, 150, 136));
  450. list.Add(Color.FromArgb(139, 195, 74));
  451. list.Add(Color.FromArgb(76, 175, 80));
  452. list.Add(Color.FromArgb(204, 219, 57));
  453. list.Add(Color.FromArgb(233, 30, 99));
  454. list.Add(Color.FromArgb(254, 234, 59));
  455. list.Add(Color.FromArgb(254, 192, 7));
  456. list.Add(Color.FromArgb(254, 152, 0));
  457. list.Add(Color.FromArgb(255, 87, 34));
  458. list.Add(Color.FromArgb(121, 85, 72));
  459. list.Add(Color.FromArgb(158, 158, 158));
  460. list.Add(Color.FromArgb(96, 125, 139));
  461. list.Add(Color.FromArgb(252, 117, 85));
  462. list.Add(Color.FromArgb(172, 113, 191));
  463. list.Add(Color.FromArgb(115, 131, 253));
  464. list.Add(Color.FromArgb(78, 206, 255));
  465. list.Add(Color.FromArgb(121, 195, 82));
  466. list.Add(Color.FromArgb(255, 163, 28));
  467. list.Add(Color.FromArgb(255, 185, 15));
  468. list.Add(Color.FromArgb(255, 181, 197));
  469. list.Add(Color.FromArgb(255, 110, 180));
  470. list.Add(Color.FromArgb(255, 69, 0));
  471. list.Add(Color.FromArgb(255, 48, 48));
  472. list.Add(Color.FromArgb(154, 205, 50));
  473. list.Add(Color.FromArgb(155, 205, 155));
  474. list.Add(Color.FromArgb(154, 50, 205));
  475. list.Add(Color.FromArgb(131, 111, 255));
  476. list.Add(Color.FromArgb(124, 205, 124));
  477. list.Add(Color.FromArgb(0, 206, 209));
  478. list.Add(Color.FromArgb(0, 178, 238));
  479. list.Add(Color.FromArgb(56, 142, 142));
  480. Type typeFromHandle = typeof(Color);
  481. PropertyInfo[] properties = typeFromHandle.GetProperties();
  482. PropertyInfo[] array = properties;
  483. for (int i = 0; i < array.Length; i++)
  484. {
  485. PropertyInfo propertyInfo = array[i];
  486. if (propertyInfo.PropertyType == typeof(Color) && (propertyInfo.Name.StartsWith("Dark") || propertyInfo.Name.StartsWith("Medium")))
  487. {
  488. object value = propertyInfo.GetValue(null, null);
  489. list.Add((Color)value);
  490. }
  491. }
  492. Colors = list.ToArray();
  493. }
  494. /// <summary>
  495. /// Draws the string.
  496. /// </summary>
  497. /// <param name="g">The g.</param>
  498. /// <param name="s">The s.</param>
  499. /// <param name="font">The font.</param>
  500. /// <param name="brush">The brush.</param>
  501. /// <param name="point">The point.</param>
  502. /// <param name="format">The format.</param>
  503. /// <param name="angle">The angle.</param>
  504. public static void DrawString(Graphics g, string s, Font font, Brush brush, PointF point, StringFormat format, float angle)
  505. {
  506. Matrix transform = g.Transform;
  507. Matrix transform2 = g.Transform;
  508. transform2.RotateAt(angle, point);
  509. g.Transform = transform2;
  510. g.DrawString(s, font, brush, point, format);
  511. g.Transform = transform;
  512. }
  513. /// <summary>
  514. /// Gets the rhombus from rectangle.
  515. /// </summary>
  516. /// <param name="rect">The rect.</param>
  517. /// <returns>Point[].</returns>
  518. public static Point[] GetRhombusFromRectangle(Rectangle rect)
  519. {
  520. return new Point[5]
  521. {
  522. new Point(rect.X, rect.Y + rect.Height / 2),
  523. new Point(rect.X + rect.Width / 2, rect.Y + rect.Height - 1),
  524. new Point(rect.X + rect.Width - 1, rect.Y + rect.Height / 2),
  525. new Point(rect.X + rect.Width / 2, rect.Y),
  526. new Point(rect.X, rect.Y + rect.Height / 2)
  527. };
  528. }
  529. /// <summary>
  530. /// Computes the paint location y.
  531. /// </summary>
  532. /// <param name="max">The maximum.</param>
  533. /// <param name="min">The minimum.</param>
  534. /// <param name="height">The height.</param>
  535. /// <param name="value">The value.</param>
  536. /// <returns>System.Single.</returns>
  537. public static float ComputePaintLocationY(int max, int min, int height, int value)
  538. {
  539. if ((float)(max - min) == 0f)
  540. {
  541. return height;
  542. }
  543. return (float)height - (float)(value - min) * 1f / (float)(max - min) * (float)height;
  544. }
  545. /// <summary>
  546. /// Computes the paint location y.
  547. /// </summary>
  548. /// <param name="max">The maximum.</param>
  549. /// <param name="min">The minimum.</param>
  550. /// <param name="height">The height.</param>
  551. /// <param name="value">The value.</param>
  552. /// <returns>System.Single.</returns>
  553. public static float ComputePaintLocationY(float max, float min, float height, float value)
  554. {
  555. if (max - min == 0f)
  556. {
  557. return height;
  558. }
  559. return height - (value - min) / (max - min) * height;
  560. }
  561. /// <summary>
  562. /// Paints the coordinate divide.
  563. /// </summary>
  564. /// <param name="g">The g.</param>
  565. /// <param name="penLine">The pen line.</param>
  566. /// <param name="penDash">The pen dash.</param>
  567. /// <param name="font">The font.</param>
  568. /// <param name="brush">The brush.</param>
  569. /// <param name="sf">The sf.</param>
  570. /// <param name="degree">The degree.</param>
  571. /// <param name="max">The maximum.</param>
  572. /// <param name="min">The minimum.</param>
  573. /// <param name="width">The width.</param>
  574. /// <param name="height">The height.</param>
  575. /// <param name="left">The left.</param>
  576. /// <param name="right">The right.</param>
  577. /// <param name="up">Up.</param>
  578. /// <param name="down">Down.</param>
  579. public static void PaintCoordinateDivide(Graphics g, System.Drawing.Pen penLine, System.Drawing.Pen penDash, Font font, System.Drawing.Brush brush, StringFormat sf, int degree, int max, int min, int width, int height, int left = 60, int right = 8, int up = 8, int down = 8)
  580. {
  581. for (int i = 0; i <= degree; i++)
  582. {
  583. int value = (max - min) * i / degree + min;
  584. int num = (int)ComputePaintLocationY(max, min, height - up - down, value) + up + 1;
  585. g.DrawLine(penLine, left - 1, num, left - 4, num);
  586. if (i != 0)
  587. {
  588. g.DrawLine(penDash, left, num, width - right, num);
  589. }
  590. g.DrawString(value.ToString(), font, brush, new Rectangle(-5, num - font.Height / 2, left, font.Height), sf);
  591. }
  592. }
  593. /// <summary>
  594. /// Adds the array data.
  595. /// </summary>
  596. /// <typeparam name="T"></typeparam>
  597. /// <param name="array">The array.</param>
  598. /// <param name="data">The data.</param>
  599. /// <param name="max">The maximum.</param>
  600. public static void AddArrayData<T>(ref T[] array, T[] data, int max)
  601. {
  602. if (data == null || data.Length == 0)
  603. {
  604. return;
  605. }
  606. if (array.Length == max)
  607. {
  608. Array.Copy(array, data.Length, array, 0, array.Length - data.Length);
  609. Array.Copy(data, 0, array, array.Length - data.Length, data.Length);
  610. }
  611. else if (array.Length + data.Length > max)
  612. {
  613. T[] array2 = new T[max];
  614. for (int i = 0; i < max - data.Length; i++)
  615. {
  616. array2[i] = array[i + (array.Length - max + data.Length)];
  617. }
  618. for (int j = 0; j < data.Length; j++)
  619. {
  620. array2[array2.Length - data.Length + j] = data[j];
  621. }
  622. array = array2;
  623. }
  624. else
  625. {
  626. T[] array3 = new T[array.Length + data.Length];
  627. for (int k = 0; k < array.Length; k++)
  628. {
  629. array3[k] = array[k];
  630. }
  631. for (int l = 0; l < data.Length; l++)
  632. {
  633. array3[array3.Length - data.Length + l] = data[l];
  634. }
  635. array = array3;
  636. }
  637. }
  638. /// <summary>
  639. /// Converts the size.
  640. /// </summary>
  641. /// <param name="size">The size.</param>
  642. /// <param name="angle">The angle.</param>
  643. /// <returns>SizeF.</returns>
  644. public static SizeF ConvertSize(SizeF size, float angle)
  645. {
  646. System.Drawing.Drawing2D.Matrix matrix = new System.Drawing.Drawing2D.Matrix();
  647. matrix.Rotate(angle);
  648. PointF[] array = new PointF[4];
  649. array[0].X = (0f - size.Width) / 2f;
  650. array[0].Y = (0f - size.Height) / 2f;
  651. array[1].X = (0f - size.Width) / 2f;
  652. array[1].Y = size.Height / 2f;
  653. array[2].X = size.Width / 2f;
  654. array[2].Y = size.Height / 2f;
  655. array[3].X = size.Width / 2f;
  656. array[3].Y = (0f - size.Height) / 2f;
  657. matrix.TransformPoints(array);
  658. float num = float.MaxValue;
  659. float num2 = float.MinValue;
  660. float num3 = float.MaxValue;
  661. float num4 = float.MinValue;
  662. PointF[] array2 = array;
  663. for (int i = 0; i < array2.Length; i++)
  664. {
  665. PointF pointF = array2[i];
  666. if (pointF.X < num)
  667. {
  668. num = pointF.X;
  669. }
  670. if (pointF.X > num2)
  671. {
  672. num2 = pointF.X;
  673. }
  674. if (pointF.Y < num3)
  675. {
  676. num3 = pointF.Y;
  677. }
  678. if (pointF.Y > num4)
  679. {
  680. num4 = pointF.Y;
  681. }
  682. }
  683. return new SizeF(num2 - num, num4 - num3);
  684. }
  685. /// <summary>
  686. /// Gets the pow.
  687. /// </summary>
  688. /// <param name="digit">The digit.</param>
  689. /// <returns>System.Int32.</returns>
  690. private static int GetPow(int digit)
  691. {
  692. int num = 1;
  693. for (int i = 0; i < digit; i++)
  694. {
  695. num *= 10;
  696. }
  697. return num;
  698. }
  699. /// <summary>
  700. /// Calculates the maximum section from.
  701. /// </summary>
  702. /// <param name="values">The values.</param>
  703. /// <returns>System.Int32.</returns>
  704. public static double CalculateMaxSectionFrom(double[] values)
  705. {
  706. double num = values.Max();
  707. return CalculateMaxSection(num);
  708. }
  709. public static double CalculateMaxSectionFrom(double[][] values)
  710. {
  711. double num = values.Max(p => p.Max());
  712. return CalculateMaxSection(num);
  713. }
  714. private static double CalculateMaxSection(double num)
  715. {
  716. if (num <= 5)
  717. {
  718. return 5;
  719. }
  720. if (num <= 10)
  721. {
  722. return 10;
  723. }
  724. int digit = num.ToString().Length - 2;
  725. int num2 = int.Parse(num.ToString().Substring(0, 2));
  726. if (num2 < 12)
  727. {
  728. return 12 * GetPow(digit);
  729. }
  730. if (num2 < 14)
  731. {
  732. return 14 * GetPow(digit);
  733. }
  734. if (num2 < 16)
  735. {
  736. return 16 * GetPow(digit);
  737. }
  738. if (num2 < 18)
  739. {
  740. return 18 * GetPow(digit);
  741. }
  742. if (num2 < 20)
  743. {
  744. return 20 * GetPow(digit);
  745. }
  746. if (num2 < 22)
  747. {
  748. return 22 * GetPow(digit);
  749. }
  750. if (num2 < 24)
  751. {
  752. return 24 * GetPow(digit);
  753. }
  754. if (num2 < 26)
  755. {
  756. return 26 * GetPow(digit);
  757. }
  758. if (num2 < 28)
  759. {
  760. return 28 * GetPow(digit);
  761. }
  762. if (num2 < 30)
  763. {
  764. return 30 * GetPow(digit);
  765. }
  766. if (num2 < 40)
  767. {
  768. return 40 * GetPow(digit);
  769. }
  770. if (num2 < 50)
  771. {
  772. return 50 * GetPow(digit);
  773. }
  774. if (num2 < 60)
  775. {
  776. return 60 * GetPow(digit);
  777. }
  778. if (num2 < 80)
  779. {
  780. return 80 * GetPow(digit);
  781. }
  782. return 100 * GetPow(digit);
  783. }
  784. /// <summary>
  785. /// Gets the color light.
  786. /// </summary>
  787. /// <param name="color">The color.</param>
  788. /// <returns>System.Drawing.Color.</returns>
  789. public static System.Drawing.Color GetColorLight(System.Drawing.Color color)
  790. {
  791. return System.Drawing.Color.FromArgb(color.R + (255 - color.R) * 40 / 100, color.G + (255 - color.G) * 40 / 100, color.B + (255 - color.B) * 40 / 100);
  792. }
  793. /// <summary>
  794. /// Gets the color light five.
  795. /// </summary>
  796. /// <param name="color">The color.</param>
  797. /// <returns>System.Drawing.Color.</returns>
  798. public static System.Drawing.Color GetColorLightFive(System.Drawing.Color color)
  799. {
  800. return System.Drawing.Color.FromArgb(color.R + (255 - color.R) * 50 / 100, color.G + (255 - color.G) * 50 / 100, color.B + (255 - color.B) * 50 / 100);
  801. }
  802. /// <summary>
  803. /// Gets the points from.
  804. /// </summary>
  805. /// <param name="points">The points.</param>
  806. /// <param name="soureWidth">Width of the soure.</param>
  807. /// <param name="sourceHeight">Height of the source.</param>
  808. /// <param name="width">The width.</param>
  809. /// <param name="height">The height.</param>
  810. /// <param name="dx">The dx.</param>
  811. /// <param name="dy">The dy.</param>
  812. /// <returns>PointF[].</returns>
  813. public static PointF[] GetPointsFrom(string points, float soureWidth, float sourceHeight, float width, float height, float dx = 0f, float dy = 0f)
  814. {
  815. string[] array = points.Split(new char[1]
  816. {
  817. ' '
  818. }, StringSplitOptions.RemoveEmptyEntries);
  819. PointF[] array2 = new PointF[array.Length];
  820. for (int i = 0; i < array.Length; i++)
  821. {
  822. int num = array[i].IndexOf(',');
  823. float num2 = Convert.ToSingle(array[i].Substring(0, num));
  824. float num3 = Convert.ToSingle(array[i].Substring(num + 1));
  825. array2[i] = new PointF(width * (num2 + dx) / soureWidth, height * (num3 + dy) / sourceHeight);
  826. }
  827. return array2;
  828. }
  829. public static bool IsDesignMode()
  830. {
  831. bool returnFlag = false;
  832. if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)
  833. {
  834. returnFlag = true;
  835. }
  836. else if (Process.GetCurrentProcess().ProcessName == "devenv")
  837. {
  838. returnFlag = true;
  839. }
  840. return returnFlag;
  841. }
  842. #region 滚动条 English:scroll bar
  843. static uint SB_HORZ = 0x0;
  844. static uint SB_VERT = 0x1;
  845. static uint SB_CTL = 0x2;
  846. static uint SB_BOTH = 0x3;
  847. [DllImport("user32.dll", SetLastError = true, EntryPoint = "GetScrollInfo")]
  848. private static extern int GetScrollInfo(IntPtr hWnd, uint fnBar, ref SCROLLINFO psbi);
  849. [DllImport("user32.dll")]//[return: MarshalAs(UnmanagedType.Bool)]
  850. private static extern int SetScrollInfo(IntPtr handle, uint fnBar, ref SCROLLINFO si, bool fRedraw);
  851. [DllImport("user32.dll", EntryPoint = "PostMessage")]
  852. private static extern bool PostMessage(IntPtr handle, int msg, uint wParam, uint lParam);
  853. [DllImport("User32.dll", EntryPoint = "SendMessage")]
  854. private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
  855. /// <summary>
  856. /// ShowScrollBar
  857. /// </summary>
  858. /// <param name="hWnd">hWnd</param>
  859. /// <param name="wBar">0:horizontal,1:vertical,3:both</param>
  860. /// <param name="bShow">bShow</param>
  861. /// <returns></returns>
  862. [DllImport("user32.dll")]
  863. [return: MarshalAs(UnmanagedType.Bool)]
  864. public static extern bool ShowScrollBar(IntPtr hWnd, int wBar, bool bShow);
  865. /// <summary>
  866. ///获取水平滚动条信息
  867. /// </summary>
  868. /// <param name="hWnd">The h WND.</param>
  869. /// <returns>Scrollbarinfo.</returns>
  870. public static SCROLLINFO GetHScrollBarInfo(IntPtr hWnd)
  871. {
  872. SCROLLINFO info = new SCROLLINFO();
  873. info.cbSize = (int)Marshal.SizeOf(info);
  874. info.fMask = (int)ScrollInfoMask.SIF_DISABLENOSCROLL | (int)ScrollInfoMask.SIF_ALL;
  875. int intRef = GetScrollInfo(hWnd, SB_HORZ, ref info);
  876. return info;
  877. }
  878. /// <summary>
  879. /// 获取垂直滚动条信息
  880. /// </summary>
  881. /// <param name="hWnd">The h WND.</param>
  882. /// <returns>Scrollbarinfo.</returns>
  883. public static SCROLLINFO GetVScrollBarInfo(IntPtr hWnd)
  884. {
  885. SCROLLINFO info = new SCROLLINFO();
  886. info.cbSize = (int)Marshal.SizeOf(info);
  887. info.fMask = (int)ScrollInfoMask.SIF_DISABLENOSCROLL | (int)ScrollInfoMask.SIF_ALL;
  888. int intRef = GetScrollInfo(hWnd, SB_VERT, ref info);
  889. return info;
  890. }
  891. public struct SCROLLINFO
  892. {
  893. public int cbSize;
  894. public int fMask;
  895. public int nMin;
  896. public int nMax;
  897. public int nPage;
  898. public int nPos;
  899. public int nTrackPos;
  900. public int ScrollMax { get { return nMax + 1 - nPage; } }
  901. }
  902. public enum ScrollInfoMask : uint
  903. {
  904. SIF_RANGE = 0x1,
  905. SIF_PAGE = 0x2,
  906. SIF_POS = 0x4,
  907. SIF_DISABLENOSCROLL = 0x8,
  908. SIF_TRACKPOS = 0x10,
  909. SIF_ALL = (SIF_RANGE | SIF_PAGE | SIF_POS | SIF_TRACKPOS),
  910. SB_THUMBTRACK = 5,
  911. WM_HSCROLL = 0x0114,
  912. WM_VSCROLL = 0x0115,
  913. SB_LINEUP = 0,
  914. SB_LINEDOWN = 1,
  915. SB_LINELEFT = 0,
  916. SB_LINERIGHT = 1,
  917. }
  918. public static void SetVScrollValue(IntPtr handle, int value)
  919. {
  920. var info = GetVScrollBarInfo(handle);
  921. info.nPos = value;
  922. SetScrollInfo(handle, SB_VERT, ref info, true);
  923. PostMessage(handle, (int)ScrollInfoMask.WM_VSCROLL, MakeLong((short)ScrollInfoMask.SB_THUMBTRACK, highPart: (short)info.nPos), 0);
  924. }
  925. public static void SetHScrollValue(IntPtr handle, int value)
  926. {
  927. var info = GetHScrollBarInfo(handle);
  928. info.nPos = value;
  929. SetScrollInfo(handle, SB_HORZ, ref info, true);
  930. PostMessage(handle, (int)ScrollInfoMask.WM_HSCROLL, MakeLong((short)ScrollInfoMask.SB_THUMBTRACK, highPart: (short)info.nPos), 0);
  931. }
  932. private static uint MakeLong(short lowPart, short highPart)
  933. {
  934. return (ushort)lowPart | (uint)(highPart << 16);
  935. }
  936. /// <summary>
  937. /// 控件向上滚动一个单位
  938. /// </summary>
  939. /// <param name="handle">控件句柄</param>
  940. public static void ScrollUp(IntPtr handle)
  941. {
  942. SendMessage(handle, (int)ScrollInfoMask.WM_VSCROLL, (int)ScrollInfoMask.SB_LINEUP, 0);
  943. }
  944. /// <summary>
  945. /// 控件向下滚动一个单位
  946. /// </summary>
  947. /// <param name="handle">控件句柄</param>
  948. public static void ScrollDown(IntPtr handle)
  949. {
  950. SendMessage(handle, (int)ScrollInfoMask.WM_VSCROLL, (int)ScrollInfoMask.SB_LINEDOWN, 0);
  951. }
  952. /// <summary>
  953. /// 控件向左滚动一个单位
  954. /// </summary>
  955. /// <param name="handle">控件句柄</param>
  956. public static void ScrollLeft(IntPtr handle)
  957. {
  958. SendMessage(handle, (int)ScrollInfoMask.WM_HSCROLL, (int)ScrollInfoMask.SB_LINELEFT, 0);
  959. }
  960. /// <summary>
  961. /// 控件向右滚动一个单位
  962. /// </summary>
  963. /// <param name="handle">控件句柄</param>
  964. public static void ScrollRight(IntPtr handle)
  965. {
  966. SendMessage(handle, (int)ScrollInfoMask.WM_VSCROLL, (int)ScrollInfoMask.SB_LINERIGHT, 0);
  967. }
  968. #endregion
  969. /// <summary>
  970. /// 返回指定图片中的非透明区域;
  971. /// </summary>
  972. /// <param name="img">位图</param>
  973. /// <returns></returns>
  974. public static GraphicsPath CalculateControlGraphicsPath(Bitmap bitmap, Color? colorTransparent = null)
  975. {
  976. // Create GraphicsPath for our bitmap calculation
  977. //创建 GraphicsPath
  978. GraphicsPath graphicsPath = new GraphicsPath();
  979. // Use the top left pixel as our transparent color
  980. //使用左上角的一点的颜色作为我们透明色
  981. Color _colorTransparent = bitmap.GetPixel(0, 0);
  982. if (colorTransparent != null && colorTransparent != Color.Transparent && colorTransparent != Color.Empty)
  983. _colorTransparent = colorTransparent.Value;
  984. // This is to store the column value where an opaque pixel is first found.
  985. // This value will determine where we start scanning for trailing opaque pixels.
  986. //第一个找到点的X
  987. int colOpaquePixel = 0;
  988. // Go through all rows (Y axis)
  989. // 偏历所有行(Y方向)
  990. for (int row = 0; row < bitmap.Height; row++)
  991. {
  992. // Reset value
  993. //重设
  994. colOpaquePixel = 0;
  995. // Go through all columns (X axis)
  996. //偏历所有列(X方向)
  997. for (int col = 0; col < bitmap.Width; col++)
  998. {
  999. // If this is an opaque pixel, mark it and search for anymore trailing behind
  1000. //如果是不需要透明处理的点则标记,然后继续偏历
  1001. if (bitmap.GetPixel(col, row) != _colorTransparent)
  1002. {
  1003. // Opaque pixel found, mark current position
  1004. //记录当前
  1005. colOpaquePixel = col;
  1006. // Create another variable to set the current pixel position
  1007. //建立新变量来记录当前点
  1008. int colNext = col;
  1009. // Starting from current found opaque pixel, search for anymore opaque pixels
  1010. // trailing behind, until a transparent pixel is found or minimum width is reached
  1011. ///从找到的不透明点开始,继续寻找不透明点,一直到找到或则达到图片宽度
  1012. for (colNext = colOpaquePixel; colNext < bitmap.Width; colNext++)
  1013. if (bitmap.GetPixel(colNext, row) == _colorTransparent)
  1014. break;
  1015. // Form a rectangle for line of opaque pixels found and add it to our graphics path
  1016. //将不透明点加到graphics path
  1017. graphicsPath.AddRectangle(new Rectangle(colOpaquePixel, row, colNext - colOpaquePixel, 1));
  1018. // No need to scan the line of opaque pixels just found
  1019. col = colNext;
  1020. }
  1021. }
  1022. }
  1023. // Return calculated graphics path
  1024. return graphicsPath;
  1025. }
  1026. /// <summary>
  1027. /// 颜色加深
  1028. /// </summary>
  1029. /// <param name="color"></param>
  1030. /// <param name="correctionFactor">-1.0f <= correctionFactor <= 1.0f</param>
  1031. /// <returns></returns>
  1032. public static Color ChangeColor(this Color color, float correctionFactor)
  1033. {
  1034. float red = (float)color.R;
  1035. float green = (float)color.G;
  1036. float blue = (float)color.B;
  1037. if (correctionFactor < 0)
  1038. {
  1039. correctionFactor = 1 + correctionFactor;
  1040. red *= correctionFactor;
  1041. green *= correctionFactor;
  1042. blue *= correctionFactor;
  1043. }
  1044. else
  1045. {
  1046. red = (255 - red) * correctionFactor + red;
  1047. green = (255 - green) * correctionFactor + green;
  1048. blue = (255 - blue) * correctionFactor + blue;
  1049. }
  1050. if (red < 0) red = 0;
  1051. if (red > 255) red = 255;
  1052. if (green < 0) green = 0;
  1053. if (green > 255) green = 255;
  1054. if (blue < 0) blue = 0;
  1055. if (blue > 255) blue = 255;
  1056. return Color.FromArgb(color.A, (int)red, (int)green, (int)blue);
  1057. }
  1058. /// <summary>
  1059. /// 相对于屏幕显示的位置
  1060. /// </summary>
  1061. /// <param name="screen">窗体需要显示的屏幕</param>
  1062. /// <param name="left">left</param>
  1063. /// <param name="top">top</param>
  1064. /// <returns></returns>
  1065. public static Point GetScreenLocation(Screen screen,int left,int top)
  1066. {
  1067. return new Point(screen.Bounds.Left + left, screen.Bounds.Top + top);
  1068. }
  1069. }
  1070. }