ControlHelper.cs 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 08-08-2019
  4. //
  5. // ***********************************************************************
  6. // <copyright file="ControlHelper.cs">
  7. // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
  8. // </copyright>
  9. //
  10. // Blog: https://www.cnblogs.com/bfyx
  11. // GitHub:https://github.com/kwwwvagaa/NetWinformControl
  12. // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
  13. //
  14. // If you use this code, please keep this note.
  15. // ***********************************************************************
  16. using System;
  17. using System.Collections.Generic;
  18. using System.ComponentModel;
  19. using System.Diagnostics;
  20. using System.Drawing;
  21. using System.Drawing.Drawing2D;
  22. using System.Linq;
  23. using System.Reflection;
  24. using System.Runtime.InteropServices;
  25. using System.Text;
  26. using System.Text.RegularExpressions;
  27. using System.Threading;
  28. using System.Windows.Forms;
  29. using HZH_Controls.Controls;
  30. namespace HZH_Controls
  31. {
  32. /// <summary>
  33. /// Class ControlHelper.
  34. /// </summary>
  35. public static class ControlHelper
  36. {
  37. #region 设置控件Enabled,切不改变控件颜色
  38. /// <summary>
  39. /// 功能描述:设置控件Enabled,切不改变控件颜色
  40. /// 作  者:HZH
  41. /// 创建日期:2019-03-04 13:43:32
  42. /// 任务编号:POS
  43. /// </summary>
  44. /// <param name="c">c</param>
  45. /// <param name="enabled">enabled</param>
  46. public static void SetControlEnabled(this Control c, bool enabled)
  47. {
  48. if (!c.IsDisposed)
  49. {
  50. if (enabled)
  51. {
  52. ControlHelper.SetWindowLong(c.Handle, -16, -134217729 & ControlHelper.GetWindowLong(c.Handle, -16));
  53. }
  54. else
  55. {
  56. ControlHelper.SetWindowLong(c.Handle, -16, 134217728 + ControlHelper.GetWindowLong(c.Handle, -16));
  57. }
  58. }
  59. }
  60. /// <summary>
  61. /// 功能描述:设置控件Enabled,切不改变控件颜色
  62. /// 作  者:HZH
  63. /// 创建日期:2019-03-04 13:43:32
  64. /// 任务编号:POS
  65. /// </summary>
  66. /// <param name="cs">cs</param>
  67. /// <param name="enabled">enabled</param>
  68. public static void SetControlEnableds(Control[] cs, bool enabled)
  69. {
  70. for (int i = 0; i < cs.Length; i++)
  71. {
  72. Control c = cs[i];
  73. SetControlEnabled(c, enabled);
  74. }
  75. }
  76. #endregion
  77. /// <summary>
  78. /// Sets the window long.
  79. /// </summary>
  80. /// <param name="hWnd">The h WND.</param>
  81. /// <param name="nIndex">Index of the n.</param>
  82. /// <param name="wndproc">The wndproc.</param>
  83. /// <returns>System.Int32.</returns>
  84. [DllImport("user32.dll ")]
  85. public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int wndproc);
  86. /// <summary>
  87. /// Gets the window long.
  88. /// </summary>
  89. /// <param name="hWnd">The h WND.</param>
  90. /// <param name="nIndex">Index of the n.</param>
  91. /// <returns>System.Int32.</returns>
  92. [DllImport("user32.dll ")]
  93. public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
  94. /// <summary>
  95. /// Gets the foreground window.
  96. /// </summary>
  97. /// <returns>IntPtr.</returns>
  98. [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
  99. public static extern IntPtr GetForegroundWindow();
  100. /// <summary>
  101. /// Threads the base call back.
  102. /// </summary>
  103. /// <param name="parent">The parent.</param>
  104. /// <param name="obj">The object.</param>
  105. private static void ThreadBaseCallBack(Control parent, object obj)
  106. {
  107. if (obj is Exception)
  108. {
  109. if (parent != null)
  110. {
  111. ThreadInvokerControl(parent, delegate
  112. {
  113. Exception ex = obj as Exception;
  114. });
  115. }
  116. }
  117. }
  118. /// <summary>
  119. /// 委托调用主线程控件
  120. /// </summary>
  121. /// <param name="parent">主线程控件</param>
  122. /// <param name="action">修改控件方法</param>
  123. public static void ThreadInvokerControl(Control parent, Action action)
  124. {
  125. if (parent != null)
  126. {
  127. if (parent.InvokeRequired)
  128. {
  129. parent.BeginInvoke(action);
  130. }
  131. else
  132. {
  133. action();
  134. SetForegroundWindow(parent.Handle);
  135. }
  136. }
  137. }
  138. /// <summary>
  139. /// 使用一个线程执行一个操作
  140. /// </summary>
  141. /// <param name="parent">父控件</param>
  142. /// <param name="func">执行内容</param>
  143. /// <param name="callback">执行后回调</param>
  144. /// <param name="enableControl">执行期间禁用控件列表</param>
  145. /// <param name="blnShowSplashScreen">执行期间是否显示等待提示</param>
  146. /// <param name="strMsg">执行期间等待提示内容,默认为“正在处理,请稍候...”</param>
  147. /// <param name="intSplashScreenDelayTime">延迟显示等待提示时间</param>
  148. public static void ThreadRunExt(
  149. Control parent,
  150. Action func,
  151. Action<object> callback,
  152. Control enableControl = null,
  153. bool blnShowSplashScreen = true,
  154. string strMsg = null,
  155. int intSplashScreenDelayTime = 200)
  156. {
  157. ThreadRunExt(parent, func, callback, new Control[] { enableControl }, blnShowSplashScreen, strMsg, intSplashScreenDelayTime);
  158. }
  159. /// <summary>
  160. /// 使用一个线程执行一个操作
  161. /// </summary>
  162. /// <param name="parent">父控件</param>
  163. /// <param name="func">执行内容</param>
  164. /// <param name="callback">执行后回调</param>
  165. /// <param name="enableControl">执行期间禁用控件列表</param>
  166. /// <param name="blnShowSplashScreen">执行期间是否显示等待提示</param>
  167. /// <param name="strMsg">执行期间等待提示内容,默认为“正在处理,请稍候...”</param>
  168. /// <param name="intSplashScreenDelayTime">延迟显示等待提示时间</param>
  169. public static void ThreadRunExt(
  170. Control parent,
  171. Action func,
  172. Action<object> callback,
  173. Control[] enableControl = null,
  174. bool blnShowSplashScreen = true,
  175. string strMsg = null,
  176. int intSplashScreenDelayTime = 200)
  177. {
  178. if (blnShowSplashScreen)
  179. {
  180. if (string.IsNullOrEmpty(strMsg))
  181. {
  182. strMsg = "正在处理,请稍候...";
  183. }
  184. if (parent != null)
  185. {
  186. ShowProcessPanel(parent, strMsg, intSplashScreenDelayTime);
  187. }
  188. }
  189. if (enableControl != null)
  190. {
  191. List<Control> lstCs = new List<Control>();
  192. foreach (var c in enableControl)
  193. {
  194. if (c == null)
  195. continue;
  196. if (c is Form)
  197. {
  198. lstCs.AddRange(c.Controls.ToArray());
  199. }
  200. else
  201. {
  202. lstCs.Add(c);
  203. }
  204. }
  205. SetControlEnableds(lstCs.ToArray(), false);
  206. }
  207. ThreadPool.QueueUserWorkItem(delegate(object a)
  208. {
  209. try
  210. {
  211. func();
  212. if (callback != null)
  213. {
  214. callback(null);
  215. }
  216. }
  217. catch (Exception obj)
  218. {
  219. if (callback != null)
  220. {
  221. callback(obj);
  222. }
  223. else
  224. {
  225. ThreadBaseCallBack(parent, obj);
  226. }
  227. }
  228. finally
  229. {
  230. if (parent != null)
  231. {
  232. ThreadInvokerControl(parent, delegate
  233. {
  234. CloseProcessPanel(parent);
  235. SetForegroundWindow(parent.Handle);
  236. });
  237. }
  238. if (enableControl != null)
  239. {
  240. if (parent != null)
  241. {
  242. ThreadInvokerControl(parent, delegate
  243. {
  244. List<Control> lstCs = new List<Control>();
  245. foreach (var c in enableControl)
  246. {
  247. if (c == null)
  248. continue;
  249. if (c is Form)
  250. {
  251. lstCs.AddRange(c.Controls.ToArray());
  252. }
  253. else
  254. {
  255. lstCs.Add(c);
  256. }
  257. }
  258. SetControlEnableds(lstCs.ToArray(), true);
  259. });
  260. }
  261. }
  262. }
  263. });
  264. }
  265. /// <summary>
  266. /// Sets the foreground window.
  267. /// </summary>
  268. /// <param name="hWnd">The h WND.</param>
  269. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
  270. [DllImport("user32.dll")]
  271. public static extern bool SetForegroundWindow(IntPtr hWnd);
  272. /// <summary>
  273. /// Shows the process panel.
  274. /// </summary>
  275. /// <param name="parent">The parent.</param>
  276. /// <param name="strMessage">The string message.</param>
  277. /// <param name="intSplashScreenDelayTime">The int splash screen delay time.</param>
  278. public static void ShowProcessPanel(Control parent, string strMessage, int intSplashScreenDelayTime = 0)
  279. {
  280. if (parent.InvokeRequired)
  281. {
  282. parent.BeginInvoke(new MethodInvoker(delegate
  283. {
  284. ShowProcessPanel(parent, strMessage, intSplashScreenDelayTime);
  285. }));
  286. }
  287. else
  288. {
  289. parent.VisibleChanged -= new EventHandler(parent_VisibleChanged);
  290. parent.VisibleChanged += new EventHandler(parent_VisibleChanged);
  291. parent.FindForm().FormClosing -= ControlHelper_FormClosing;
  292. parent.FindForm().FormClosing += ControlHelper_FormClosing;
  293. Control control = null;
  294. lock (parent)
  295. {
  296. control = HaveProcessPanelControl(parent);
  297. if (control == null)
  298. {
  299. control = CreateProgressPanel();
  300. parent.Controls.Add(control);
  301. }
  302. }
  303. Forms.FrmWaiting frmWaitingEx = control.Tag as Forms.FrmWaiting;
  304. frmWaitingEx.Msg = strMessage;
  305. frmWaitingEx.ShowForm(intSplashScreenDelayTime);
  306. }
  307. }
  308. /// <summary>
  309. /// Handles the FormClosing event of the ControlHelper control.
  310. /// </summary>
  311. /// <param name="sender">The source of the event.</param>
  312. /// <param name="e">The <see cref="FormClosingEventArgs" /> instance containing the event data.</param>
  313. static void ControlHelper_FormClosing(object sender, FormClosingEventArgs e)
  314. {
  315. Control control = sender as Control;
  316. control.FindForm().FormClosing -= ControlHelper_FormClosing;
  317. CloseWaiting(control);
  318. }
  319. /// <summary>
  320. /// Handles the VisibleChanged event of the parent control.
  321. /// </summary>
  322. /// <param name="sender">The source of the event.</param>
  323. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  324. private static void parent_VisibleChanged(object sender, EventArgs e)
  325. {
  326. Control control = sender as Control;
  327. control.VisibleChanged -= new EventHandler(parent_VisibleChanged);
  328. if (!control.Visible)
  329. {
  330. CloseWaiting(control);
  331. }
  332. }
  333. /// <summary>
  334. /// Closes the waiting.
  335. /// </summary>
  336. /// <param name="control">The control.</param>
  337. private static void CloseWaiting(Control control)
  338. {
  339. Control[] array = control.Controls.Find("myprogressPanelext", false);
  340. if (array.Length > 0)
  341. {
  342. Control control2 = array[0];
  343. if (control2.Tag != null && control2.Tag is Forms.FrmWaiting)
  344. {
  345. Forms.FrmWaiting frmWaitingEx = control2.Tag as Forms.FrmWaiting;
  346. if (frmWaitingEx != null && !frmWaitingEx.IsDisposed && frmWaitingEx.Visible)
  347. {
  348. frmWaitingEx.Hide();
  349. }
  350. }
  351. }
  352. }
  353. /// <summary>
  354. /// Closes the process panel.
  355. /// </summary>
  356. /// <param name="parent">The parent.</param>
  357. public static void CloseProcessPanel(Control parent)
  358. {
  359. if (parent.InvokeRequired)
  360. {
  361. parent.BeginInvoke(new MethodInvoker(delegate
  362. {
  363. CloseProcessPanel(parent);
  364. }));
  365. }
  366. else if (parent != null)
  367. {
  368. Control control = HaveProcessPanelControl(parent);
  369. if (control != null)
  370. {
  371. Form frm = control.Tag as Form;
  372. if (frm != null && !frm.IsDisposed && frm.Visible)
  373. {
  374. if (frm.InvokeRequired)
  375. {
  376. frm.BeginInvoke(new MethodInvoker(delegate
  377. {
  378. frm.Hide();
  379. }));
  380. }
  381. else
  382. {
  383. frm.Hide();
  384. }
  385. }
  386. }
  387. }
  388. }
  389. /// <summary>
  390. /// Haves the process panel control.
  391. /// </summary>
  392. /// <param name="parent">The parent.</param>
  393. /// <returns>Control.</returns>
  394. public static Control HaveProcessPanelControl(Control parent)
  395. {
  396. Control[] array = parent.Controls.Find("myprogressPanelext", false);
  397. Control result;
  398. if (array.Length > 0)
  399. {
  400. result = array[0];
  401. }
  402. else
  403. {
  404. result = null;
  405. }
  406. return result;
  407. }
  408. /// <summary>
  409. /// Creates the progress panel.
  410. /// </summary>
  411. /// <returns>Control.</returns>
  412. public static Control CreateProgressPanel()
  413. {
  414. return new Label
  415. {
  416. Name = "myprogressPanelext",
  417. Visible = false,
  418. Tag = new Forms.FrmWaiting
  419. {
  420. TopMost = true,
  421. Opacity = 0.0
  422. }
  423. };
  424. }
  425. /// <summary>
  426. /// Converts to array.
  427. /// </summary>
  428. /// <param name="controls">The controls.</param>
  429. /// <returns>Control[].</returns>
  430. public static Control[] ToArray(this System.Windows.Forms.Control.ControlCollection controls)
  431. {
  432. if (controls == null || controls.Count <= 0)
  433. return new Control[0];
  434. List<Control> lst = new List<Control>();
  435. foreach (Control item in controls)
  436. {
  437. lst.Add(item);
  438. }
  439. return lst.ToArray();
  440. }
  441. #region 根据控件宽度截取字符串
  442. /// <summary>
  443. /// 功能描述:根据控件宽度截取字符串
  444. /// 作  者:HZH
  445. /// 创建日期:2019-06-27 10:49:10
  446. /// 任务编号:POS
  447. /// </summary>
  448. /// <param name="strSource">字符串</param>
  449. /// <param name="fltControlWidth">控件宽度</param>
  450. /// <param name="g">Graphics</param>
  451. /// <param name="font">字体</param>
  452. /// <returns>截取后的字符串</returns>
  453. public static string GetSubString(
  454. string strSource,
  455. float fltControlWidth,
  456. System.Drawing.Graphics g,
  457. System.Drawing.Font font)
  458. {
  459. try
  460. {
  461. fltControlWidth = fltControlWidth - 20;
  462. strSource = strSource.Trim();
  463. while (true)
  464. {
  465. System.Drawing.SizeF sizeF = g.MeasureString(strSource.Replace(" ", "A"), font);
  466. if (sizeF.Width > fltControlWidth)
  467. {
  468. strSource = strSource.TrimEnd('…');
  469. if (strSource.Length <= 1)
  470. return "";
  471. strSource = strSource.Substring(0, strSource.Length - 1).Trim() + "…";
  472. }
  473. else
  474. {
  475. return strSource;
  476. }
  477. }
  478. }
  479. finally
  480. {
  481. g.Dispose();
  482. }
  483. }
  484. #endregion
  485. #region 获取字符串宽度
  486. /// <summary>
  487. /// 功能描述:获取字符串宽度
  488. /// 作  者:HZH
  489. /// 创建日期:2019-06-27 11:54:50
  490. /// 任务编号:POS
  491. /// </summary>
  492. /// <param name="strSource">strSource</param>
  493. /// <param name="g">g</param>
  494. /// <param name="font">font</param>
  495. /// <returns>返回值</returns>
  496. public static int GetStringWidth(
  497. string strSource,
  498. System.Drawing.Graphics g,
  499. System.Drawing.Font font)
  500. {
  501. string[] strs = strSource.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
  502. float fltWidth = 0;
  503. foreach (var item in strs)
  504. {
  505. System.Drawing.SizeF sizeF = g.MeasureString(strSource.Replace(" ", "A"), font);
  506. if (sizeF.Width > fltWidth)
  507. fltWidth = sizeF.Width;
  508. }
  509. return (int)fltWidth;
  510. }
  511. #endregion
  512. #region 动画特效
  513. /// <summary>
  514. /// Animates the window.
  515. /// </summary>
  516. /// <param name="whnd">The WHND.</param>
  517. /// <param name="dwtime">The dwtime.</param>
  518. /// <param name="dwflag">The dwflag.</param>
  519. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
  520. [DllImport("user32.dll")]
  521. public static extern bool AnimateWindow(IntPtr whnd, int dwtime, int dwflag);
  522. //dwflag的取值如下
  523. /// <summary>
  524. /// The aw hor positive
  525. /// </summary>
  526. public const Int32 AW_HOR_POSITIVE = 0x00000001;
  527. //从左到右显示
  528. /// <summary>
  529. /// The aw hor negative
  530. /// </summary>
  531. public const Int32 AW_HOR_NEGATIVE = 0x00000002;
  532. //从右到左显示
  533. /// <summary>
  534. /// The aw ver positive
  535. /// </summary>
  536. public const Int32 AW_VER_POSITIVE = 0x00000004;
  537. //从上到下显示
  538. /// <summary>
  539. /// The aw ver negative
  540. /// </summary>
  541. public const Int32 AW_VER_NEGATIVE = 0x00000008;
  542. //从下到上显示
  543. /// <summary>
  544. /// The aw center
  545. /// </summary>
  546. public const Int32 AW_CENTER = 0x00000010;
  547. //若使用了AW_HIDE标志,则使窗口向内重叠,即收缩窗口;否则使窗口向外扩展,即展开窗口
  548. /// <summary>
  549. /// The aw hide
  550. /// </summary>
  551. public const Int32 AW_HIDE = 0x00010000;
  552. //隐藏窗口,缺省则显示窗口
  553. /// <summary>
  554. /// The aw activate
  555. /// </summary>
  556. public const Int32 AW_ACTIVATE = 0x00020000;
  557. //激活窗口。在使用了AW_HIDE标志后不能使用这个标志
  558. /// <summary>
  559. /// The aw slide
  560. /// </summary>
  561. public const Int32 AW_SLIDE = 0x00040000;
  562. //使用滑动类型。缺省则为滚动动画类型。当使用AW_CENTER标志时,这个标志就被忽略
  563. /// <summary>
  564. /// The aw blend
  565. /// </summary>
  566. public const Int32 AW_BLEND = 0x00080000;
  567. //透明度从高到低
  568. #endregion
  569. #region 检查文本控件输入类型是否有效
  570. /// <summary>
  571. /// 功能描述:检查文本控件输入类型是否有效
  572. /// 作  者:HZH
  573. /// 创建日期:2019-02-28 10:23:34
  574. /// 任务编号:POS
  575. /// </summary>
  576. /// <param name="strValue">值</param>
  577. /// <param name="inputType">控制类型</param>
  578. /// <param name="decMaxValue">最大值</param>
  579. /// <param name="decMinValue">最小值</param>
  580. /// <param name="intLength">小数位长度</param>
  581. /// <param name="strRegexPattern">正则</param>
  582. /// <returns>返回值</returns>
  583. public static bool CheckInputType(
  584. string strValue,
  585. TextInputType inputType,
  586. decimal decMaxValue = default(decimal),
  587. decimal decMinValue = default(decimal),
  588. int intLength = 2,
  589. string strRegexPattern = null)
  590. {
  591. bool result;
  592. switch (inputType)
  593. {
  594. case TextInputType.NotControl:
  595. result = true;
  596. return result;
  597. case TextInputType.UnsignNumber:
  598. if (string.IsNullOrEmpty(strValue))
  599. {
  600. result = true;
  601. return result;
  602. }
  603. else
  604. {
  605. if (strValue.IndexOf("-") >= 0)
  606. {
  607. result = false;
  608. return result;
  609. }
  610. }
  611. break;
  612. case TextInputType.Number:
  613. if (string.IsNullOrEmpty(strValue))
  614. {
  615. result = true;
  616. return result;
  617. }
  618. else
  619. {
  620. if (!Regex.IsMatch(strValue, "^-?\\d*(\\.?\\d*)?$"))
  621. {
  622. result = false;
  623. return result;
  624. }
  625. }
  626. break;
  627. case TextInputType.Integer:
  628. if (string.IsNullOrEmpty(strValue))
  629. {
  630. result = true;
  631. return result;
  632. }
  633. else
  634. {
  635. if (!Regex.IsMatch(strValue, "^-?\\d*$"))
  636. {
  637. result = false;
  638. return result;
  639. }
  640. }
  641. break;
  642. case TextInputType.PositiveInteger:
  643. if (string.IsNullOrEmpty(strValue))
  644. {
  645. result = true;
  646. return result;
  647. }
  648. else
  649. {
  650. if (!Regex.IsMatch(strValue, "^\\d+$"))
  651. {
  652. result = false;
  653. return result;
  654. }
  655. }
  656. break;
  657. case TextInputType.Regex:
  658. result = (string.IsNullOrEmpty(strRegexPattern) || Regex.IsMatch(strValue, strRegexPattern));
  659. return result;
  660. }
  661. if (strValue == "-")
  662. {
  663. return true;
  664. }
  665. decimal d;
  666. if (!decimal.TryParse(strValue, out d))
  667. {
  668. result = false;
  669. }
  670. else if (d < decMinValue || d > decMaxValue)
  671. {
  672. result = false;
  673. }
  674. else
  675. {
  676. if (inputType == TextInputType.Number || inputType == TextInputType.UnsignNumber || inputType == TextInputType.PositiveNumber)
  677. {
  678. if (strValue.IndexOf(".") >= 0)
  679. {
  680. string text = strValue.Substring(strValue.IndexOf("."));
  681. if (text.Length > intLength + 1)
  682. {
  683. result = false;
  684. return result;
  685. }
  686. }
  687. }
  688. result = true;
  689. }
  690. return result;
  691. }
  692. #endregion
  693. #region 冻结控件
  694. /// <summary>
  695. /// The m LST freeze control
  696. /// </summary>
  697. static Dictionary<Control, bool> m_lstFreezeControl = new Dictionary<Control, bool>();
  698. /// <summary>
  699. /// 功能描述:停止更新控件
  700. /// 作  者:HZH
  701. /// 创建日期:2019-07-13 11:11:32
  702. /// 任务编号:POS
  703. /// </summary>
  704. /// <param name="control">control</param>
  705. /// <param name="blnToFreeze">是否停止更新</param>
  706. public static void FreezeControl(Control control, bool blnToFreeze)
  707. {
  708. if (blnToFreeze && control.IsHandleCreated && control.Visible && !control.IsDisposed && (!m_lstFreezeControl.ContainsKey(control) || (m_lstFreezeControl.ContainsKey(control) && m_lstFreezeControl[control] == false)))
  709. {
  710. m_lstFreezeControl[control] = true;
  711. control.Disposed += control_Disposed;
  712. NativeMethods.SendMessage(control.Handle, 11, 0, 0);
  713. }
  714. else if (!blnToFreeze && !control.IsDisposed && m_lstFreezeControl.ContainsKey(control) && m_lstFreezeControl[control] == true)
  715. {
  716. m_lstFreezeControl.Remove(control);
  717. NativeMethods.SendMessage(control.Handle, 11, 1, 0);
  718. control.Invalidate(true);
  719. }
  720. }
  721. /// <summary>
  722. /// Handles the Disposed event of the control control.
  723. /// </summary>
  724. /// <param name="sender">The source of the event.</param>
  725. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  726. static void control_Disposed(object sender, EventArgs e)
  727. {
  728. try
  729. {
  730. if (m_lstFreezeControl.ContainsKey((Control)sender))
  731. m_lstFreezeControl.Remove((Control)sender);
  732. }
  733. catch { }
  734. }
  735. #endregion
  736. /// <summary>
  737. /// 设置GDI高质量模式抗锯齿
  738. /// </summary>
  739. /// <param name="g">The g.</param>
  740. public static void SetGDIHigh(this Graphics g)
  741. {
  742. g.SmoothingMode = SmoothingMode.AntiAlias; //使绘图质量最高,即消除锯齿
  743. g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  744. g.CompositingQuality = CompositingQuality.HighQuality;
  745. }
  746. /// <summary>
  747. /// 根据矩形和圆得到一个圆角矩形Path
  748. /// </summary>
  749. /// <param name="rect">The rect.</param>
  750. /// <param name="cornerRadius">The corner radius.</param>
  751. /// <returns>GraphicsPath.</returns>
  752. public static GraphicsPath CreateRoundedRectanglePath(this Rectangle rect, int cornerRadius)
  753. {
  754. GraphicsPath roundedRect = new GraphicsPath();
  755. roundedRect.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
  756. roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);
  757. roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
  758. roundedRect.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);
  759. roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
  760. roundedRect.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
  761. roundedRect.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
  762. roundedRect.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);
  763. roundedRect.CloseFigure();
  764. return roundedRect;
  765. }
  766. /// <summary>
  767. /// Creates the rounded rectangle path.
  768. /// </summary>
  769. /// <param name="rect">The rect.</param>
  770. /// <param name="cornerRadius">The corner radius.</param>
  771. /// <returns>GraphicsPath.</returns>
  772. public static GraphicsPath CreateRoundedRectanglePath(this RectangleF rect, int cornerRadius)
  773. {
  774. GraphicsPath roundedRect = new GraphicsPath();
  775. roundedRect.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
  776. roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);
  777. roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
  778. roundedRect.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);
  779. roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
  780. roundedRect.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
  781. roundedRect.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
  782. roundedRect.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);
  783. roundedRect.CloseFigure();
  784. return roundedRect;
  785. }
  786. /// <summary>
  787. /// Gets the colors.
  788. /// </summary>
  789. /// <value>The colors.</value>
  790. public static Color[] Colors { get; private set; }
  791. static ControlHelper()
  792. {
  793. List<Color> list = new List<Color>();
  794. list.Add(Color.FromArgb(55, 162, 218));
  795. list.Add(Color.FromArgb(50, 197, 233));
  796. list.Add(Color.FromArgb(103, 224, 227));
  797. list.Add(Color.FromArgb(159, 230, 184));
  798. list.Add(Color.FromArgb(255, 219, 92));
  799. list.Add(Color.FromArgb(255, 159, 127));
  800. list.Add(Color.FromArgb(251, 114, 147));
  801. list.Add(Color.FromArgb(224, 98, 174));
  802. list.Add(Color.FromArgb(230, 144, 209));
  803. list.Add(Color.FromArgb(231, 188, 243));
  804. list.Add(Color.FromArgb(157, 150, 245));
  805. list.Add(Color.FromArgb(131, 120, 234));
  806. list.Add(Color.FromArgb(150, 191, 255));
  807. list.Add(Color.FromArgb(243, 67, 54));
  808. list.Add(Color.FromArgb(156, 39, 176));
  809. list.Add(Color.FromArgb(103, 58, 183));
  810. list.Add(Color.FromArgb(63, 81, 181));
  811. list.Add(Color.FromArgb(33, 150, 243));
  812. list.Add(Color.FromArgb(0, 188, 211));
  813. list.Add(Color.FromArgb(3, 169, 244));
  814. list.Add(Color.FromArgb(0, 150, 136));
  815. list.Add(Color.FromArgb(139, 195, 74));
  816. list.Add(Color.FromArgb(76, 175, 80));
  817. list.Add(Color.FromArgb(204, 219, 57));
  818. list.Add(Color.FromArgb(233, 30, 99));
  819. list.Add(Color.FromArgb(254, 234, 59));
  820. list.Add(Color.FromArgb(254, 192, 7));
  821. list.Add(Color.FromArgb(254, 152, 0));
  822. list.Add(Color.FromArgb(255, 87, 34));
  823. list.Add(Color.FromArgb(121, 85, 72));
  824. list.Add(Color.FromArgb(158, 158, 158));
  825. list.Add(Color.FromArgb(96, 125, 139));
  826. list.Add(Color.FromArgb(252, 117, 85));
  827. list.Add(Color.FromArgb(172, 113, 191));
  828. list.Add(Color.FromArgb(115, 131, 253));
  829. list.Add(Color.FromArgb(78, 206, 255));
  830. list.Add(Color.FromArgb(121, 195, 82));
  831. list.Add(Color.FromArgb(255, 163, 28));
  832. list.Add(Color.FromArgb(255, 185, 15));
  833. list.Add(Color.FromArgb(255, 181, 197));
  834. list.Add(Color.FromArgb(255, 110, 180));
  835. list.Add(Color.FromArgb(255, 69, 0));
  836. list.Add(Color.FromArgb(255, 48, 48));
  837. list.Add(Color.FromArgb(154, 205, 50));
  838. list.Add(Color.FromArgb(155, 205, 155));
  839. list.Add(Color.FromArgb(154, 50, 205));
  840. list.Add(Color.FromArgb(131, 111, 255));
  841. list.Add(Color.FromArgb(124, 205, 124));
  842. list.Add(Color.FromArgb(0, 206, 209));
  843. list.Add(Color.FromArgb(0, 178, 238));
  844. list.Add(Color.FromArgb(56, 142, 142));
  845. Type typeFromHandle = typeof(Color);
  846. PropertyInfo[] properties = typeFromHandle.GetProperties();
  847. PropertyInfo[] array = properties;
  848. for (int i = 0; i < array.Length; i++)
  849. {
  850. PropertyInfo propertyInfo = array[i];
  851. if (propertyInfo.PropertyType == typeof(Color) && (propertyInfo.Name.StartsWith("Dark") || propertyInfo.Name.StartsWith("Medium")))
  852. {
  853. object value = propertyInfo.GetValue(null, null);
  854. list.Add((Color)value);
  855. }
  856. }
  857. Colors = list.ToArray();
  858. }
  859. /// <summary>
  860. /// Draws the string.
  861. /// </summary>
  862. /// <param name="g">The g.</param>
  863. /// <param name="s">The s.</param>
  864. /// <param name="font">The font.</param>
  865. /// <param name="brush">The brush.</param>
  866. /// <param name="point">The point.</param>
  867. /// <param name="format">The format.</param>
  868. /// <param name="angle">The angle.</param>
  869. public static void DrawString(Graphics g, string s, Font font, Brush brush, PointF point, StringFormat format, float angle)
  870. {
  871. Matrix transform = g.Transform;
  872. Matrix transform2 = g.Transform;
  873. transform2.RotateAt(angle, point);
  874. g.Transform = transform2;
  875. g.DrawString(s, font, brush, point, format);
  876. g.Transform = transform;
  877. }
  878. /// <summary>
  879. /// Gets the rhombus from rectangle.
  880. /// </summary>
  881. /// <param name="rect">The rect.</param>
  882. /// <returns>Point[].</returns>
  883. public static Point[] GetRhombusFromRectangle(Rectangle rect)
  884. {
  885. return new Point[5]
  886. {
  887. new Point(rect.X, rect.Y + rect.Height / 2),
  888. new Point(rect.X + rect.Width / 2, rect.Y + rect.Height - 1),
  889. new Point(rect.X + rect.Width - 1, rect.Y + rect.Height / 2),
  890. new Point(rect.X + rect.Width / 2, rect.Y),
  891. new Point(rect.X, rect.Y + rect.Height / 2)
  892. };
  893. }
  894. /// <summary>
  895. /// Computes the paint location y.
  896. /// </summary>
  897. /// <param name="max">The maximum.</param>
  898. /// <param name="min">The minimum.</param>
  899. /// <param name="height">The height.</param>
  900. /// <param name="value">The value.</param>
  901. /// <returns>System.Single.</returns>
  902. public static float ComputePaintLocationY(int max, int min, int height, int value)
  903. {
  904. if ((float)(max - min) == 0f)
  905. {
  906. return height;
  907. }
  908. return (float)height - (float)(value - min) * 1f / (float)(max - min) * (float)height;
  909. }
  910. /// <summary>
  911. /// Computes the paint location y.
  912. /// </summary>
  913. /// <param name="max">The maximum.</param>
  914. /// <param name="min">The minimum.</param>
  915. /// <param name="height">The height.</param>
  916. /// <param name="value">The value.</param>
  917. /// <returns>System.Single.</returns>
  918. public static float ComputePaintLocationY(float max, float min, float height, float value)
  919. {
  920. if (max - min == 0f)
  921. {
  922. return height;
  923. }
  924. return height - (value - min) / (max - min) * height;
  925. }
  926. /// <summary>
  927. /// Paints the coordinate divide.
  928. /// </summary>
  929. /// <param name="g">The g.</param>
  930. /// <param name="penLine">The pen line.</param>
  931. /// <param name="penDash">The pen dash.</param>
  932. /// <param name="font">The font.</param>
  933. /// <param name="brush">The brush.</param>
  934. /// <param name="sf">The sf.</param>
  935. /// <param name="degree">The degree.</param>
  936. /// <param name="max">The maximum.</param>
  937. /// <param name="min">The minimum.</param>
  938. /// <param name="width">The width.</param>
  939. /// <param name="height">The height.</param>
  940. /// <param name="left">The left.</param>
  941. /// <param name="right">The right.</param>
  942. /// <param name="up">Up.</param>
  943. /// <param name="down">Down.</param>
  944. 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)
  945. {
  946. for (int i = 0; i <= degree; i++)
  947. {
  948. int value = (max - min) * i / degree + min;
  949. int num = (int)ComputePaintLocationY(max, min, height - up - down, value) + up + 1;
  950. g.DrawLine(penLine, left - 1, num, left - 4, num);
  951. if (i != 0)
  952. {
  953. g.DrawLine(penDash, left, num, width - right, num);
  954. }
  955. g.DrawString(value.ToString(), font, brush, new Rectangle(-5, num - font.Height / 2, left, font.Height), sf);
  956. }
  957. }
  958. /// <summary>
  959. /// Paints the triangle.
  960. /// </summary>
  961. /// <param name="g">The g.</param>
  962. /// <param name="brush">The brush.</param>
  963. /// <param name="point">The point.</param>
  964. /// <param name="size">The size.</param>
  965. /// <param name="direction">The direction.</param>
  966. public static void PaintTriangle(Graphics g, System.Drawing.Brush brush, Point point, int size, GraphDirection direction)
  967. {
  968. Point[] array = new Point[4];
  969. switch (direction)
  970. {
  971. case GraphDirection.Leftward:
  972. array[0] = new Point(point.X, point.Y - size);
  973. array[1] = new Point(point.X, point.Y + size);
  974. array[2] = new Point(point.X - 2 * size, point.Y);
  975. break;
  976. case GraphDirection.Rightward:
  977. array[0] = new Point(point.X, point.Y - size);
  978. array[1] = new Point(point.X, point.Y + size);
  979. array[2] = new Point(point.X + 2 * size, point.Y);
  980. break;
  981. case GraphDirection.Upward:
  982. array[0] = new Point(point.X - size, point.Y);
  983. array[1] = new Point(point.X + size, point.Y);
  984. array[2] = new Point(point.X, point.Y - 2 * size);
  985. break;
  986. default:
  987. array[0] = new Point(point.X - size, point.Y);
  988. array[1] = new Point(point.X + size, point.Y);
  989. array[2] = new Point(point.X, point.Y + 2 * size);
  990. break;
  991. }
  992. array[3] = array[0];
  993. g.FillPolygon(brush, array);
  994. }
  995. /// <summary>
  996. /// Paints the triangle.
  997. /// </summary>
  998. /// <param name="g">The g.</param>
  999. /// <param name="brush">The brush.</param>
  1000. /// <param name="point">The point.</param>
  1001. /// <param name="size">The size.</param>
  1002. /// <param name="direction">The direction.</param>
  1003. public static void PaintTriangle(Graphics g, System.Drawing.Brush brush, PointF point, int size, GraphDirection direction)
  1004. {
  1005. PointF[] array = new PointF[4];
  1006. switch (direction)
  1007. {
  1008. case GraphDirection.Leftward:
  1009. array[0] = new PointF(point.X, point.Y - (float)size);
  1010. array[1] = new PointF(point.X, point.Y + (float)size);
  1011. array[2] = new PointF(point.X - (float)(2 * size), point.Y);
  1012. break;
  1013. case GraphDirection.Rightward:
  1014. array[0] = new PointF(point.X, point.Y - (float)size);
  1015. array[1] = new PointF(point.X, point.Y + (float)size);
  1016. array[2] = new PointF(point.X + (float)(2 * size), point.Y);
  1017. break;
  1018. case GraphDirection.Upward:
  1019. array[0] = new PointF(point.X - (float)size, point.Y);
  1020. array[1] = new PointF(point.X + (float)size, point.Y);
  1021. array[2] = new PointF(point.X, point.Y - (float)(2 * size));
  1022. break;
  1023. default:
  1024. array[0] = new PointF(point.X - (float)size, point.Y);
  1025. array[1] = new PointF(point.X + (float)size, point.Y);
  1026. array[2] = new PointF(point.X, point.Y + (float)(2 * size));
  1027. break;
  1028. }
  1029. array[3] = array[0];
  1030. g.FillPolygon(brush, array);
  1031. }
  1032. /// <summary>
  1033. /// Adds the array data.
  1034. /// </summary>
  1035. /// <typeparam name="T"></typeparam>
  1036. /// <param name="array">The array.</param>
  1037. /// <param name="data">The data.</param>
  1038. /// <param name="max">The maximum.</param>
  1039. public static void AddArrayData<T>(ref T[] array, T[] data, int max)
  1040. {
  1041. if (data == null || data.Length == 0)
  1042. {
  1043. return;
  1044. }
  1045. if (array.Length == max)
  1046. {
  1047. Array.Copy(array, data.Length, array, 0, array.Length - data.Length);
  1048. Array.Copy(data, 0, array, array.Length - data.Length, data.Length);
  1049. }
  1050. else if (array.Length + data.Length > max)
  1051. {
  1052. T[] array2 = new T[max];
  1053. for (int i = 0; i < max - data.Length; i++)
  1054. {
  1055. array2[i] = array[i + (array.Length - max + data.Length)];
  1056. }
  1057. for (int j = 0; j < data.Length; j++)
  1058. {
  1059. array2[array2.Length - data.Length + j] = data[j];
  1060. }
  1061. array = array2;
  1062. }
  1063. else
  1064. {
  1065. T[] array3 = new T[array.Length + data.Length];
  1066. for (int k = 0; k < array.Length; k++)
  1067. {
  1068. array3[k] = array[k];
  1069. }
  1070. for (int l = 0; l < data.Length; l++)
  1071. {
  1072. array3[array3.Length - data.Length + l] = data[l];
  1073. }
  1074. array = array3;
  1075. }
  1076. }
  1077. /// <summary>
  1078. /// Converts the size.
  1079. /// </summary>
  1080. /// <param name="size">The size.</param>
  1081. /// <param name="angle">The angle.</param>
  1082. /// <returns>SizeF.</returns>
  1083. public static SizeF ConvertSize(SizeF size, float angle)
  1084. {
  1085. System.Drawing.Drawing2D.Matrix matrix = new System.Drawing.Drawing2D.Matrix();
  1086. matrix.Rotate(angle);
  1087. PointF[] array = new PointF[4];
  1088. array[0].X = (0f - size.Width) / 2f;
  1089. array[0].Y = (0f - size.Height) / 2f;
  1090. array[1].X = (0f - size.Width) / 2f;
  1091. array[1].Y = size.Height / 2f;
  1092. array[2].X = size.Width / 2f;
  1093. array[2].Y = size.Height / 2f;
  1094. array[3].X = size.Width / 2f;
  1095. array[3].Y = (0f - size.Height) / 2f;
  1096. matrix.TransformPoints(array);
  1097. float num = float.MaxValue;
  1098. float num2 = float.MinValue;
  1099. float num3 = float.MaxValue;
  1100. float num4 = float.MinValue;
  1101. PointF[] array2 = array;
  1102. for (int i = 0; i < array2.Length; i++)
  1103. {
  1104. PointF pointF = array2[i];
  1105. if (pointF.X < num)
  1106. {
  1107. num = pointF.X;
  1108. }
  1109. if (pointF.X > num2)
  1110. {
  1111. num2 = pointF.X;
  1112. }
  1113. if (pointF.Y < num3)
  1114. {
  1115. num3 = pointF.Y;
  1116. }
  1117. if (pointF.Y > num4)
  1118. {
  1119. num4 = pointF.Y;
  1120. }
  1121. }
  1122. return new SizeF(num2 - num, num4 - num3);
  1123. }
  1124. /// <summary>
  1125. /// Gets the pow.
  1126. /// </summary>
  1127. /// <param name="digit">The digit.</param>
  1128. /// <returns>System.Int32.</returns>
  1129. private static int GetPow(int digit)
  1130. {
  1131. int num = 1;
  1132. for (int i = 0; i < digit; i++)
  1133. {
  1134. num *= 10;
  1135. }
  1136. return num;
  1137. }
  1138. /// <summary>
  1139. /// Calculates the maximum section from.
  1140. /// </summary>
  1141. /// <param name="values">The values.</param>
  1142. /// <returns>System.Int32.</returns>
  1143. public static double CalculateMaxSectionFrom(double[] values)
  1144. {
  1145. double num = values.Max();
  1146. return CalculateMaxSection(num);
  1147. }
  1148. public static double CalculateMaxSectionFrom(double[][] values)
  1149. {
  1150. double num = values.Max(p => p.Max());
  1151. return CalculateMaxSection(num);
  1152. }
  1153. private static double CalculateMaxSection(double num)
  1154. {
  1155. if (num <= 5)
  1156. {
  1157. return 5;
  1158. }
  1159. if (num <= 10)
  1160. {
  1161. return 10;
  1162. }
  1163. int digit = num.ToString().Length - 2;
  1164. int num2 = int.Parse(num.ToString().Substring(0, 2));
  1165. if (num2 < 12)
  1166. {
  1167. return 12 * GetPow(digit);
  1168. }
  1169. if (num2 < 14)
  1170. {
  1171. return 14 * GetPow(digit);
  1172. }
  1173. if (num2 < 16)
  1174. {
  1175. return 16 * GetPow(digit);
  1176. }
  1177. if (num2 < 18)
  1178. {
  1179. return 18 * GetPow(digit);
  1180. }
  1181. if (num2 < 20)
  1182. {
  1183. return 20 * GetPow(digit);
  1184. }
  1185. if (num2 < 22)
  1186. {
  1187. return 22 * GetPow(digit);
  1188. }
  1189. if (num2 < 24)
  1190. {
  1191. return 24 * GetPow(digit);
  1192. }
  1193. if (num2 < 26)
  1194. {
  1195. return 26 * GetPow(digit);
  1196. }
  1197. if (num2 < 28)
  1198. {
  1199. return 28 * GetPow(digit);
  1200. }
  1201. if (num2 < 30)
  1202. {
  1203. return 30 * GetPow(digit);
  1204. }
  1205. if (num2 < 40)
  1206. {
  1207. return 40 * GetPow(digit);
  1208. }
  1209. if (num2 < 50)
  1210. {
  1211. return 50 * GetPow(digit);
  1212. }
  1213. if (num2 < 60)
  1214. {
  1215. return 60 * GetPow(digit);
  1216. }
  1217. if (num2 < 80)
  1218. {
  1219. return 80 * GetPow(digit);
  1220. }
  1221. return 100 * GetPow(digit);
  1222. }
  1223. /// <summary>
  1224. /// Gets the color light.
  1225. /// </summary>
  1226. /// <param name="color">The color.</param>
  1227. /// <returns>System.Drawing.Color.</returns>
  1228. public static System.Drawing.Color GetColorLight(System.Drawing.Color color)
  1229. {
  1230. 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);
  1231. }
  1232. /// <summary>
  1233. /// Gets the color light five.
  1234. /// </summary>
  1235. /// <param name="color">The color.</param>
  1236. /// <returns>System.Drawing.Color.</returns>
  1237. public static System.Drawing.Color GetColorLightFive(System.Drawing.Color color)
  1238. {
  1239. 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);
  1240. }
  1241. /// <summary>
  1242. /// Gets the points from.
  1243. /// </summary>
  1244. /// <param name="points">The points.</param>
  1245. /// <param name="soureWidth">Width of the soure.</param>
  1246. /// <param name="sourceHeight">Height of the source.</param>
  1247. /// <param name="width">The width.</param>
  1248. /// <param name="height">The height.</param>
  1249. /// <param name="dx">The dx.</param>
  1250. /// <param name="dy">The dy.</param>
  1251. /// <returns>PointF[].</returns>
  1252. public static PointF[] GetPointsFrom(string points, float soureWidth, float sourceHeight, float width, float height, float dx = 0f, float dy = 0f)
  1253. {
  1254. string[] array = points.Split(new char[1]
  1255. {
  1256. ' '
  1257. }, StringSplitOptions.RemoveEmptyEntries);
  1258. PointF[] array2 = new PointF[array.Length];
  1259. for (int i = 0; i < array.Length; i++)
  1260. {
  1261. int num = array[i].IndexOf(',');
  1262. float num2 = Convert.ToSingle(array[i].Substring(0, num));
  1263. float num3 = Convert.ToSingle(array[i].Substring(num + 1));
  1264. array2[i] = new PointF(width * (num2 + dx) / soureWidth, height * (num3 + dy) / sourceHeight);
  1265. }
  1266. return array2;
  1267. }
  1268. public static bool IsDesignMode()
  1269. {
  1270. bool returnFlag = false;
  1271. if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)
  1272. {
  1273. returnFlag = true;
  1274. }
  1275. else if (Process.GetCurrentProcess().ProcessName == "devenv")
  1276. {
  1277. returnFlag = true;
  1278. }
  1279. return returnFlag;
  1280. }
  1281. #region 滚动条 English:scroll bar
  1282. static uint SB_HORZ = 0x0;
  1283. static uint SB_VERT = 0x1;
  1284. static uint SB_CTL = 0x2;
  1285. static uint SB_BOTH = 0x3;
  1286. [DllImport("user32.dll", SetLastError = true, EntryPoint = "GetScrollInfo")]
  1287. private static extern int GetScrollInfo(IntPtr hWnd, uint idObject, ref SCROLLINFO psbi);
  1288. /// <summary>
  1289. ///获取水平滚动条信息
  1290. /// </summary>
  1291. /// <param name="hWnd">The h WND.</param>
  1292. /// <returns>Scrollbarinfo.</returns>
  1293. public static SCROLLINFO GetHScrollBarInfo(IntPtr hWnd)
  1294. {
  1295. SCROLLINFO info = new SCROLLINFO();
  1296. info.cbSize = (uint)Marshal.SizeOf(info);
  1297. info.fMask = (int)ScrollInfoMask.SIF_ALL;
  1298. int intRef = GetScrollInfo(hWnd, SB_HORZ, ref info);
  1299. return info;
  1300. }
  1301. /// <summary>
  1302. /// 获取垂直滚动条信息
  1303. /// </summary>
  1304. /// <param name="hWnd">The h WND.</param>
  1305. /// <returns>Scrollbarinfo.</returns>
  1306. public static SCROLLINFO GetVScrollBarInfo(IntPtr hWnd)
  1307. {
  1308. SCROLLINFO info = new SCROLLINFO();
  1309. info.cbSize = (uint)Marshal.SizeOf(info);
  1310. info.fMask = (int)ScrollInfoMask.SIF_ALL;
  1311. int intRef = GetScrollInfo(hWnd, SB_VERT, ref info);
  1312. return info;
  1313. }
  1314. public struct SCROLLINFO
  1315. {
  1316. public uint cbSize;
  1317. public uint fMask;
  1318. public int nMin;
  1319. public int nMax;
  1320. public uint nPage;
  1321. public int nPos;
  1322. public int nTrackPos;
  1323. }
  1324. public enum ScrollInfoMask : uint
  1325. {
  1326. SIF_RANGE = 0x1,
  1327. SIF_PAGE = 0x2,
  1328. SIF_POS = 0x4,
  1329. SIF_DISABLENOSCROLL = 0x8,
  1330. SIF_TRACKPOS = 0x10,
  1331. SIF_ALL = (SIF_RANGE | SIF_PAGE | SIF_POS | SIF_TRACKPOS),
  1332. }
  1333. #endregion
  1334. /// <summary>
  1335. /// 返回指定图片中的非透明区域;
  1336. /// </summary>
  1337. /// <param name="img">位图</param>
  1338. /// <returns></returns>
  1339. public static GraphicsPath CalculateControlGraphicsPath(Bitmap bitmap, Color? colorTransparent = null)
  1340. {
  1341. // Create GraphicsPath for our bitmap calculation
  1342. //创建 GraphicsPath
  1343. GraphicsPath graphicsPath = new GraphicsPath();
  1344. // Use the top left pixel as our transparent color
  1345. //使用左上角的一点的颜色作为我们透明色
  1346. Color _colorTransparent = bitmap.GetPixel(0, 0);
  1347. if (colorTransparent != null && colorTransparent != Color.Transparent && colorTransparent != Color.Empty)
  1348. _colorTransparent = colorTransparent.Value;
  1349. // This is to store the column value where an opaque pixel is first found.
  1350. // This value will determine where we start scanning for trailing opaque pixels.
  1351. //第一个找到点的X
  1352. int colOpaquePixel = 0;
  1353. // Go through all rows (Y axis)
  1354. // 偏历所有行(Y方向)
  1355. for (int row = 0; row < bitmap.Height; row++)
  1356. {
  1357. // Reset value
  1358. //重设
  1359. colOpaquePixel = 0;
  1360. // Go through all columns (X axis)
  1361. //偏历所有列(X方向)
  1362. for (int col = 0; col < bitmap.Width; col++)
  1363. {
  1364. // If this is an opaque pixel, mark it and search for anymore trailing behind
  1365. //如果是不需要透明处理的点则标记,然后继续偏历
  1366. if (bitmap.GetPixel(col, row) != _colorTransparent)
  1367. {
  1368. // Opaque pixel found, mark current position
  1369. //记录当前
  1370. colOpaquePixel = col;
  1371. // Create another variable to set the current pixel position
  1372. //建立新变量来记录当前点
  1373. int colNext = col;
  1374. // Starting from current found opaque pixel, search for anymore opaque pixels
  1375. // trailing behind, until a transparent pixel is found or minimum width is reached
  1376. ///从找到的不透明点开始,继续寻找不透明点,一直到找到或则达到图片宽度
  1377. for (colNext = colOpaquePixel; colNext < bitmap.Width; colNext++)
  1378. if (bitmap.GetPixel(colNext, row) == _colorTransparent)
  1379. break;
  1380. // Form a rectangle for line of opaque pixels found and add it to our graphics path
  1381. //将不透明点加到graphics path
  1382. graphicsPath.AddRectangle(new Rectangle(colOpaquePixel, row, colNext - colOpaquePixel, 1));
  1383. // No need to scan the line of opaque pixels just found
  1384. col = colNext;
  1385. }
  1386. }
  1387. }
  1388. // Return calculated graphics path
  1389. return graphicsPath;
  1390. }
  1391. }
  1392. }