1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace Common
- {
- public class Global
- {
- // 显示提示的方法
- public enum MSGTYPE { Info = 1, Fail = 2, Warn = 3, Sucess = 4 }
- /// <summary>
- /// 提示信息,不要超过30个字
- /// </summary>
- /// <param name="menuStrip"></param>
- /// <param name="msg"></param>
- public static void ShowTips(string msg, MSGTYPE type,MenuStrip mstrip)
- {
- MenuStrip menuStrip = null;
- menuStrip = mstrip;
- if (msg.Length > 30)
- {
- msg = msg.Substring(0, 25) + "...";
- }
- Graphics g = menuStrip.CreateGraphics();
- Font f = new Font(menuStrip.Font.Name, 11);
- Size SizeParent = menuStrip.Size;
- Size sizeMSG = TextRenderer.MeasureText(g, msg, f, new Size(0, 0), TextFormatFlags.NoPadding);
- int x = (SizeParent.Width - sizeMSG.Width) / 2;
- int y = 0;
- if (type == MSGTYPE.Sucess || type == MSGTYPE.Info)
- {
- g.FillRectangle(new SolidBrush(Color.ForestGreen), new RectangleF(x - 10, y + 5, sizeMSG.Width + 20, sizeMSG.Height));
- g.DrawString(msg, f, new SolidBrush(Color.White), new Point(x, y + 5));
- }
- if (type == MSGTYPE.Fail)
- {
- g.FillRectangle(new SolidBrush(Color.OrangeRed), new RectangleF(x - 10, y + 5, sizeMSG.Width + 20, sizeMSG.Height));
- g.DrawString(msg, f, new SolidBrush(Color.White), new Point(x, y + 5));
- }
- if (type == MSGTYPE.Warn)
- {
- g.FillRectangle(new SolidBrush(Color.Orange), new RectangleF(x - 10, y + 5, sizeMSG.Width + 20, sizeMSG.Height));
- g.DrawString(msg, f, new SolidBrush(Color.White), new Point(x, y + 5));
- }
- try
- {
- ////延时 1500 消除提示
- Task.Factory.StartNew(() =>
- {
- try
- {
- Thread.Sleep(1500);
- /*开始执行线程代码*/
- menuStrip.Invoke(new MethodInvoker(() =>
- {
- menuStrip.Refresh();
- }));
- }
- catch (Exception) { }
- });
- }
- catch (Exception) { }
- g.Dispose();
- }
- }
- }
|