Global.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using System.Windows.Forms;
  9. namespace Common
  10. {
  11. public class Global
  12. {
  13. // 显示提示的方法
  14. public enum MSGTYPE { Info = 1, Fail = 2, Warn = 3, Sucess = 4 }
  15. /// <summary>
  16. /// 提示信息,不要超过30个字
  17. /// </summary>
  18. /// <param name="menuStrip"></param>
  19. /// <param name="msg"></param>
  20. public static void ShowTips(string msg, MSGTYPE type,MenuStrip mstrip)
  21. {
  22. MenuStrip menuStrip = null;
  23. menuStrip = mstrip;
  24. if (msg.Length > 30)
  25. {
  26. msg = msg.Substring(0, 25) + "...";
  27. }
  28. Graphics g = menuStrip.CreateGraphics();
  29. Font f = new Font(menuStrip.Font.Name, 11);
  30. Size SizeParent = menuStrip.Size;
  31. Size sizeMSG = TextRenderer.MeasureText(g, msg, f, new Size(0, 0), TextFormatFlags.NoPadding);
  32. int x = (SizeParent.Width - sizeMSG.Width) / 2;
  33. int y = 0;
  34. if (type == MSGTYPE.Sucess || type == MSGTYPE.Info)
  35. {
  36. g.FillRectangle(new SolidBrush(Color.ForestGreen), new RectangleF(x - 10, y + 5, sizeMSG.Width + 20, sizeMSG.Height));
  37. g.DrawString(msg, f, new SolidBrush(Color.White), new Point(x, y + 5));
  38. }
  39. if (type == MSGTYPE.Fail)
  40. {
  41. g.FillRectangle(new SolidBrush(Color.OrangeRed), new RectangleF(x - 10, y + 5, sizeMSG.Width + 20, sizeMSG.Height));
  42. g.DrawString(msg, f, new SolidBrush(Color.White), new Point(x, y + 5));
  43. }
  44. if (type == MSGTYPE.Warn)
  45. {
  46. g.FillRectangle(new SolidBrush(Color.Orange), new RectangleF(x - 10, y + 5, sizeMSG.Width + 20, sizeMSG.Height));
  47. g.DrawString(msg, f, new SolidBrush(Color.White), new Point(x, y + 5));
  48. }
  49. try
  50. {
  51. ////延时 1500 消除提示
  52. Task.Factory.StartNew(() =>
  53. {
  54. try
  55. {
  56. Thread.Sleep(1500);
  57. /*开始执行线程代码*/
  58. menuStrip.Invoke(new MethodInvoker(() =>
  59. {
  60. menuStrip.Refresh();
  61. }));
  62. }
  63. catch (Exception) { }
  64. });
  65. }
  66. catch (Exception) { }
  67. g.Dispose();
  68. }
  69. }
  70. }