前提

入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。

官网:https://www.hzhcontrols.cn

GitHub:https://github.com/kwwwvagaa/NetWinformControl

码云:HZHControls控件库: HZHControls控件库,c#的winform自定义控件,对触屏具有更好的操作支持,项目是基于framework4.0,完全原生控件开发,没有使用任何第三方控件,你可以放心的用在你的项目中(winfromcontrol/winformcontrol/.net)。还有更丰富的工业控件持续增加中~~~

如果觉得写的还行,请点个 star 支持一下吧

欢迎前来交流探讨: 企鹅群568015492 企鹅群568015492

目录

c#Winform自定义控件-目录_c#winform自定义控件-有图标的按钮-CSDN博客

准备工作

这个窗体继承子基类窗体FrmBase,如果你对FrmBase还不了解,请移步 (十七)c#Winform自定义控件-基类窗体 查看

气泡需要支持多个位置显示,也就是说四面八方都可以显示,并且支持样式,自定义大小,另外具有内置的4中模式(成功,错误,警告,正常)

开始

定义一些枚举

复制代码

 1  public enum TipsSizeMode
 2     {
 3         Small,
 4         Medium,
 5         Large,
 6         None
 7     }
 8     public enum TipsState
 9     {
10         Default = -1,
11         Success = -6566849,
12         Info = -12477983,
13         Warning = -357816,
14         Error = -1097849
15     }

复制代码

添加Form,命名FrmTips,继承自FrmBase

属性

复制代码

 1  private ContentAlignment m_showAlign = ContentAlignment.BottomLeft;
 2 
 3         /// <summary>
 4         /// 显示位置
 5         /// </summary>
 6         public ContentAlignment ShowAlign
 7         {
 8             get { return m_showAlign; }
 9             set { m_showAlign = value; }
10         }
11 
12         private static List<FrmTips> m_lstTips = new List<FrmTips>();
13 
14         private int m_CloseTime = 0;
15 
16         public int CloseTime
17         {
18             get { return m_CloseTime; }
19             set
20             {
21                 m_CloseTime = value;
22                 if (value > 0)
23                     timer1.Interval = value;
24             }
25         }

复制代码

提供一些公共函数

复制代码

 1    #region 清理提示框
 2         /// <summary>
 3         /// 功能描述:清理提示框
 4         /// 作  者:HZH
 5         /// 创建日期:2019-02-28 15:11:03
 6         /// 任务编号:POS
 7         /// </summary>
 8         public static void ClearTips()
 9         {
10             for (int i = m_lstTips.Count - 1; i >= 0; i--)
11             {
12                 FrmTips current = m_lstTips[i];
13                 if (!current.IsDisposed)
14                 {
15                     current.Close();
16                     current.Dispose();
17                 }
18             }
19             m_lstTips.Clear();
20         }
21         #endregion
22  
23         /// <summary>
24         /// 重置倒计时
25         /// </summary>
26         public void ResetTimer()
27         {
28             if (m_CloseTime > 0)
29             {
30                 timer1.Enabled = false;
31                 timer1.Enabled = true;
32             }
33         }

复制代码

提供的静态函数

复制代码

  1         #region 清理提示框
  2         /// <summary>
  3         /// 功能描述:清理提示框
  4         /// 作  者:HZH
  5         /// 创建日期:2019-02-28 15:11:03
  6         /// 任务编号:POS
  7         /// </summary>
  8         public static void ClearTips()
  9         {
 10             for (int i = m_lstTips.Count - 1; i >= 0; i--)
 11             {
 12                 FrmTips current = m_lstTips[i];
 13                 if (!current.IsDisposed)
 14                 {
 15                     current.Close();
 16                     current.Dispose();
 17                 }
 18             }
 19             m_lstTips.Clear();
 20         }
 21         #endregion
 22 
 23   
 24         private static KeyValuePair<string, FrmTips> m_lastTips = new KeyValuePair<string, FrmTips>();
 25 
 26         public static FrmTips ShowTips(
 27             Form frm,
 28             string strMsg,
 29             int intAutoColseTime = 0,
 30             bool blnShowCoseBtn = true,
 31             ContentAlignment align = ContentAlignment.BottomLeft,
 32             Point? point = null,
 33             TipsSizeMode mode = TipsSizeMode.Small,
 34             Size? size = null,
 35             TipsState state = TipsState.Default)
 36         {
 37 
 38             if (m_lastTips.Key == strMsg + state && !m_lastTips.Value.IsDisposed && m_lastTips.Value.Visible)
 39             {
 40                 m_lastTips.Value.ResetTimer();
 41                 return m_lastTips.Value;
 42             }
 43             else
 44             {
 45                 FrmTips frmTips = new FrmTips();
 46                 switch (mode)
 47                 {
 48                     case TipsSizeMode.Small:
 49                         frmTips.Size = new Size(350, 35);
 50                         break;
 51                     case TipsSizeMode.Medium:
 52                         frmTips.Size = new Size(350, 50);
 53                         break;
 54                     case TipsSizeMode.Large:
 55                         frmTips.Size = new Size(350, 65);
 56                         break;
 57                     case TipsSizeMode.None:
 58                         if (!size.HasValue)
 59                         {
 60                             frmTips.Size = new Size(350, 35);
 61                         }
 62                         else
 63                         {
 64                             frmTips.Size = size.Value;
 65                         }
 66                         break;
 67                 }
 68                 frmTips.BackColor = Color.FromArgb((int)state);
 69 
 70                 if (state == TipsState.Default)
 71                 {
 72                     frmTips.lblMsg.ForeColor = SystemColors.ControlText;
 73                 }
 74                 else
 75                 {
 76                     frmTips.lblMsg.ForeColor = Color.White;
 77                 }
 78                 switch (state)
 79                 {
 80                     case TipsState.Default:
 81                         frmTips.pctStat.Image = HZH_Controls.Properties.Resources.alarm;
 82                         break;
 83                     case TipsState.Success:
 84                         frmTips.pctStat.Image = HZH_Controls.Properties.Resources.success;
 85                         break;
 86                     case TipsState.Info:
 87                         frmTips.pctStat.Image = HZH_Controls.Properties.Resources.alarm;
 88                         break;
 89                     case TipsState.Warning:
 90                         frmTips.pctStat.Image = HZH_Controls.Properties.Resources.warning;
 91                         break;
 92                     case TipsState.Error:
 93                         frmTips.pctStat.Image = HZH_Controls.Properties.Resources.error;
 94                         break;
 95                     default:
 96                         frmTips.pctStat.Image = HZH_Controls.Properties.Resources.alarm;
 97                         break;
 98                 }
 99 
100                 frmTips.lblMsg.Text = strMsg;
101                 frmTips.CloseTime = intAutoColseTime;
102                 frmTips.btnClose.Visible = blnShowCoseBtn;
103 
104 
105                 frmTips.ShowAlign = align;
106                 frmTips.Owner = frm;
107                 FrmTips.m_lstTips.Add(frmTips);
108                 FrmTips.ReshowTips();
109                 frmTips.Show(frm);
110                 if (frm != null && !frm.IsDisposed)
111                 {
112                     ControlHelper.SetForegroundWindow(frm.Handle);
113                 }
114                 //frmTips.BringToFront();
115                 m_lastTips = new KeyValuePair<string, FrmTips>(strMsg + state, frmTips);
116                 return frmTips;
117             }
118         }
119 
120         #region 刷新显示
121         /// <summary>
122         /// 功能描述:刷新显示
123         /// 作  者:HZH
124         /// 创建日期:2019-02-28 15:33:06
125         /// 任务编号:POS
126         /// </summary>
127         public static void ReshowTips()
128         {
129             lock (FrmTips.m_lstTips)
130             {
131                 FrmTips.m_lstTips.RemoveAll(p => p.IsDisposed == true);
132                 var enumerable = from p in FrmTips.m_lstTips
133                                  group p by new
134                                  {
135                                      p.ShowAlign
136                                  };
137                 Size size = Screen.PrimaryScreen.Bounds.Size;
138                 foreach (var item in enumerable)
139                 {
140                     List<FrmTips> list = FrmTips.m_lstTips.FindAll((FrmTips p) => p.ShowAlign == item.Key.ShowAlign);
141                     for (int i = 0; i < list.Count; i++)
142                     {
143                         FrmTips frmTips = list[i];
144                         if (frmTips.InvokeRequired)
145                         {
146                             frmTips.BeginInvoke(new MethodInvoker(delegate()
147                             {
148                                 switch (item.Key.ShowAlign)
149                                 {
150                                     case ContentAlignment.BottomCenter:
151                                         frmTips.Location = new Point((size.Width - frmTips.Width) / 2, size.Height - 100 - (i + 1) * (frmTips.Height + 10));
152                                         break;
153                                     case ContentAlignment.BottomLeft:
154                                         frmTips.Location = new Point(10, size.Height - 100 - (i + 1) * (frmTips.Height + 10));
155                                         break;
156                                     case ContentAlignment.BottomRight:
157                                         frmTips.Location = new Point(size.Width - frmTips.Width - 10, size.Height - 100 - (i + 1) * (frmTips.Height + 10));
158                                         break;
159                                     case ContentAlignment.MiddleCenter:
160                                         frmTips.Location = new Point((size.Width - frmTips.Width) / 2, size.Height - (size.Height - list.Count * (frmTips.Height + 10)) / 2 - (i + 1) * (frmTips.Height + 10));
161                                         break;
162                                     case ContentAlignment.MiddleLeft:
163                                         frmTips.Location = new Point(10, size.Height - (size.Height - list.Count * (frmTips.Height + 10)) / 2 - (i + 1) * (frmTips.Height + 10));
164                                         break;
165                                     case ContentAlignment.MiddleRight:
166                                         frmTips.Location = new Point(size.Width - frmTips.Width - 10, size.Height - (size.Height - list.Count * (frmTips.Height + 10)) / 2 - (i + 1) * (frmTips.Height + 10));
167                                         break;
168                                     case ContentAlignment.TopCenter:
169                                         frmTips.Location = new Point((size.Width - frmTips.Width) / 2, 10 + (i + 1) * (frmTips.Height + 10));
170                                         break;
171                                     case ContentAlignment.TopLeft:
172                                         frmTips.Location = new Point(10, 10 + (i + 1) * (frmTips.Height + 10));
173                                         break;
174                                     case ContentAlignment.TopRight:
175                                         frmTips.Location = new Point(size.Width - frmTips.Width - 10, 10 + (i + 1) * (frmTips.Height + 10));
176                                         break;
177                                     default:
178                                         break;
179                                 }
180                             }));
181                         }
182                         else
183                         {
184                             switch (item.Key.ShowAlign)
185                             {
186                                 case ContentAlignment.BottomCenter:
187                                     frmTips.Location = new Point((size.Width - frmTips.Width) / 2, size.Height - 100 - (i + 1) * (frmTips.Height + 10));
188                                     break;
189                                 case ContentAlignment.BottomLeft:
190                                     frmTips.Location = new Point(10, size.Height - 100 - (i + 1) * (frmTips.Height + 10));
191                                     break;
192                                 case ContentAlignment.BottomRight:
193                                     frmTips.Location = new Point(size.Width - frmTips.Width - 10, size.Height - 100 - (i + 1) * (frmTips.Height + 10));
194                                     break;
195                                 case ContentAlignment.MiddleCenter:
196                                     frmTips.Location = new Point((size.Width - frmTips.Width) / 2, size.Height - (size.Height - list.Count * (frmTips.Height + 10)) / 2 - (i + 1) * (frmTips.Height + 10));
197                                     break;
198                                 case ContentAlignment.MiddleLeft:
199                                     frmTips.Location = new Point(10, size.Height - (size.Height - list.Count * (frmTips.Height + 10)) / 2 - (i + 1) * (frmTips.Height + 10));
200                                     break;
201                                 case ContentAlignment.MiddleRight:
202                                     frmTips.Location = new Point(size.Width - frmTips.Width - 10, size.Height - (size.Height - list.Count * (frmTips.Height + 10)) / 2 - (i + 1) * (frmTips.Height + 10));
203                                     break;
204                                 case ContentAlignment.TopCenter:
205                                     frmTips.Location = new Point((size.Width - frmTips.Width) / 2, 10 + (i + 1) * (frmTips.Height + 10));
206                                     break;
207                                 case ContentAlignment.TopLeft:
208                                     frmTips.Location = new Point(10, 10 + (i + 1) * (frmTips.Height + 10));
209                                     break;
210                                 case ContentAlignment.TopRight:
211                                     frmTips.Location = new Point(size.Width - frmTips.Width - 10, 10 + (i + 1) * (frmTips.Height + 10));
212                                     break;
213                                 default:
214                                     break;
215                             }
216                         }
217 
218                     }
219                 }
220             }
221         }
222         #endregion
223 
224 public static FrmTips ShowTipsSuccess(Form frm, string strMsg)
225         {
226             return FrmTips.ShowTips(frm, strMsg, 3000, false, ContentAlignment.BottomCenter, null, TipsSizeMode.Large, null, TipsState.Success);
227         }
228 
229         public static FrmTips ShowTipsError(Form frm, string strMsg)
230         {
231             return FrmTips.ShowTips(frm, strMsg, 3000, false, ContentAlignment.BottomCenter, null, TipsSizeMode.Large, null, TipsState.Error);
232         }
233 
234         public static FrmTips ShowTipsInfo(Form frm, string strMsg)
235         {
236             return FrmTips.ShowTips(frm, strMsg, 3000, false, ContentAlignment.BottomCenter, null, TipsSizeMode.Large, null, TipsState.Info);
237         }
238         public static FrmTips ShowTipsWarning(Form frm, string strMsg)
239         {
240             return FrmTips.ShowTips(frm, strMsg, 3000, false, ContentAlignment.BottomCenter, null, TipsSizeMode.Large, null, TipsState.Warning);
241         }

复制代码

一些事件处理

复制代码

 1  private void FrmTips_FormClosing(object sender, FormClosingEventArgs e)
 2         {
 3             if (m_lastTips.Value == this)
 4                 m_lastTips = new KeyValuePair<string, FrmTips>();
 5             m_lstTips.Remove(this);
 6             ReshowTips();
 7 
 8             for (int i = Application.OpenForms.Count - 1; i >= 0; i--)
 9             {
10                 if (Application.OpenForms[i].IsDisposed || !Application.OpenForms[i].Visible || Application.OpenForms[i] is FrmTips)
11                 {
12                     continue;
13                 }
14                 else
15                 {
16                     Timer t = new Timer();
17                     t.Interval = 100;
18                     var frm = Application.OpenForms[i];
19                     t.Tick += (a, b) =>
20                     {
21                         t.Enabled = false;
22                         if (!frm.IsDisposed)
23                             ControlHelper.SetForegroundWindow(frm.Handle);
24                     };
25                     t.Enabled = true;
26                     break;
27                 }
28             }
29         }
30 
31         private void FrmTips_Load(object sender, EventArgs e)
32         {
33             if (m_CloseTime > 0)
34             {
35                 this.timer1.Interval = m_CloseTime;
36                 this.timer1.Enabled = true;
37             }
38         }
39 
40         private void timer1_Tick(object sender, EventArgs e)
41         {
42             this.timer1.Enabled = false;
43             this.Close();
44         }
45 
46         private void btnClose_MouseDown(object sender, MouseEventArgs e)
47         {
48             this.timer1.Enabled = false;
49             this.Close();
50         }
51   private void FrmTips_FormClosed(object sender, FormClosedEventArgs e)
52         {
53             this.Dispose();
54             GC.Collect();
55         }

复制代码

最后看一下完整代码

// 版权所有  黄正辉  交流群:568015492   QQ:623128629
// 文件名称:FrmTips.cs
// 创建日期:2019-08-15 16:04:54
// 功能描述:FrmTips
// 项目地址:https://gitee.com/kwwwvagaa/net_winform_custom_control
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HZH_Controls.Forms
{
    public partial class FrmTips : FrmBase
    {
        private ContentAlignment m_showAlign = ContentAlignment.BottomLeft;

        /// <summary>
        /// 显示位置
        /// </summary>
        public ContentAlignment ShowAlign
        {
            get { return m_showAlign; }
            set { m_showAlign = value; }
        }

        private static List<FrmTips> m_lstTips = new List<FrmTips>();

        private int m_CloseTime = 0;

        public int CloseTime
        {
            get { return m_CloseTime; }
            set
            {
                m_CloseTime = value;
                if (value > 0)
                    timer1.Interval = value;
            }
        }

        public FrmTips()
        {
            base.SetStyle(ControlStyles.UserPaint, true);
            base.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            base.SetStyle(ControlStyles.DoubleBuffer, true);
            InitializeComponent();
        }

        #region 清理提示框
        /// <summary>
        /// 功能描述:清理提示框
        /// 作  者:HZH
        /// 创建日期:2019-02-28 15:11:03
        /// 任务编号:POS
        /// </summary>
        public static void ClearTips()
        {
            for (int i = m_lstTips.Count - 1; i >= 0; i--)
            {
                FrmTips current = m_lstTips[i];
                if (!current.IsDisposed)
                {
                    current.Close();
                    current.Dispose();
                }
            }
            m_lstTips.Clear();
        }
        #endregion

        /// <summary>
        /// 重置倒计时
        /// </summary>
        public void ResetTimer()
        {
            if (m_CloseTime > 0)
            {
                timer1.Enabled = false;
                timer1.Enabled = true;
            }
        }
        private static KeyValuePair<string, FrmTips> m_lastTips = new KeyValuePair<string, FrmTips>();

        public static FrmTips ShowTips(
            Form frm,
            string strMsg,
            int intAutoColseTime = 0,
            bool blnShowCoseBtn = true,
            ContentAlignment align = ContentAlignment.BottomLeft,
            Point? point = null,
            TipsSizeMode mode = TipsSizeMode.Small,
            Size? size = null,
            TipsState state = TipsState.Default)
        {

            if (m_lastTips.Key == strMsg + state && !m_lastTips.Value.IsDisposed && m_lastTips.Value.Visible)
            {
                m_lastTips.Value.ResetTimer();
                return m_lastTips.Value;
            }
            else
            {
                FrmTips frmTips = new FrmTips();
                switch (mode)
                {
                    case TipsSizeMode.Small:
                        frmTips.Size = new Size(350, 35);
                        break;
                    case TipsSizeMode.Medium:
                        frmTips.Size = new Size(350, 50);
                        break;
                    case TipsSizeMode.Large:
                        frmTips.Size = new Size(350, 65);
                        break;
                    case TipsSizeMode.None:
                        if (!size.HasValue)
                        {
                            frmTips.Size = new Size(350, 35);
                        }
                        else
                        {
                            frmTips.Size = size.Value;
                        }
                        break;
                }
                frmTips.BackColor = Color.FromArgb((int)state);

                if (state == TipsState.Default)
                {
                    frmTips.lblMsg.ForeColor = SystemColors.ControlText;
                }
                else
                {
                    frmTips.lblMsg.ForeColor = Color.White;
                }
                switch (state)
                {
                    case TipsState.Default:
                        frmTips.pctStat.Image = HZH_Controls.Properties.Resources.alarm;
                        break;
                    case TipsState.Success:
                        frmTips.pctStat.Image = HZH_Controls.Properties.Resources.success;
                        break;
                    case TipsState.Info:
                        frmTips.pctStat.Image = HZH_Controls.Properties.Resources.alarm;
                        break;
                    case TipsState.Warning:
                        frmTips.pctStat.Image = HZH_Controls.Properties.Resources.warning;
                        break;
                    case TipsState.Error:
                        frmTips.pctStat.Image = HZH_Controls.Properties.Resources.error;
                        break;
                    default:
                        frmTips.pctStat.Image = HZH_Controls.Properties.Resources.alarm;
                        break;
                }

                frmTips.lblMsg.Text = strMsg;
                frmTips.CloseTime = intAutoColseTime;
                frmTips.btnClose.Visible = blnShowCoseBtn;


                frmTips.ShowAlign = align;
                frmTips.Owner = frm;
                FrmTips.m_lstTips.Add(frmTips);
                FrmTips.ReshowTips();
                frmTips.Show(frm);
                if (frm != null && !frm.IsDisposed)
                {
                    ControlHelper.SetForegroundWindow(frm.Handle);
                }
                //frmTips.BringToFront();
                m_lastTips = new KeyValuePair<string, FrmTips>(strMsg + state, frmTips);
                return frmTips;
            }
        }

        #region 刷新显示
        /// <summary>
        /// 功能描述:刷新显示
        /// 作  者:HZH
        /// 创建日期:2019-02-28 15:33:06
        /// 任务编号:POS
        /// </summary>
        public static void ReshowTips()
        {
            lock (FrmTips.m_lstTips)
            {
                FrmTips.m_lstTips.RemoveAll(p => p.IsDisposed == true);
                var enumerable = from p in FrmTips.m_lstTips
                                 group p by new
                                 {
                                     p.ShowAlign
                                 };
                Size size = Screen.PrimaryScreen.Bounds.Size;
                foreach (var item in enumerable)
                {
                    List<FrmTips> list = FrmTips.m_lstTips.FindAll((FrmTips p) => p.ShowAlign == item.Key.ShowAlign);
                    for (int i = 0; i < list.Count; i++)
                    {
                        FrmTips frmTips = list[i];
                        if (frmTips.InvokeRequired)
                        {
                            frmTips.BeginInvoke(new MethodInvoker(delegate()
                            {
                                switch (item.Key.ShowAlign)
                                {
                                    case ContentAlignment.BottomCenter:
                                        frmTips.Location = new Point((size.Width - frmTips.Width) / 2, size.Height - 100 - (i + 1) * (frmTips.Height + 10));
                                        break;
                                    case ContentAlignment.BottomLeft:
                                        frmTips.Location = new Point(10, size.Height - 100 - (i + 1) * (frmTips.Height + 10));
                                        break;
                                    case ContentAlignment.BottomRight:
                                        frmTips.Location = new Point(size.Width - frmTips.Width - 10, size.Height - 100 - (i + 1) * (frmTips.Height + 10));
                                        break;
                                    case ContentAlignment.MiddleCenter:
                                        frmTips.Location = new Point((size.Width - frmTips.Width) / 2, size.Height - (size.Height - list.Count * (frmTips.Height + 10)) / 2 - (i + 1) * (frmTips.Height + 10));
                                        break;
                                    case ContentAlignment.MiddleLeft:
                                        frmTips.Location = new Point(10, size.Height - (size.Height - list.Count * (frmTips.Height + 10)) / 2 - (i + 1) * (frmTips.Height + 10));
                                        break;
                                    case ContentAlignment.MiddleRight:
                                        frmTips.Location = new Point(size.Width - frmTips.Width - 10, size.Height - (size.Height - list.Count * (frmTips.Height + 10)) / 2 - (i + 1) * (frmTips.Height + 10));
                                        break;
                                    case ContentAlignment.TopCenter:
                                        frmTips.Location = new Point((size.Width - frmTips.Width) / 2, 10 + (i + 1) * (frmTips.Height + 10));
                                        break;
                                    case ContentAlignment.TopLeft:
                                        frmTips.Location = new Point(10, 10 + (i + 1) * (frmTips.Height + 10));
                                        break;
                                    case ContentAlignment.TopRight:
                                        frmTips.Location = new Point(size.Width - frmTips.Width - 10, 10 + (i + 1) * (frmTips.Height + 10));
                                        break;
                                    default:
                                        break;
                                }
                            }));
                        }
                        else
                        {
                            switch (item.Key.ShowAlign)
                            {
                                case ContentAlignment.BottomCenter:
                                    frmTips.Location = new Point((size.Width - frmTips.Width) / 2, size.Height - 100 - (i + 1) * (frmTips.Height + 10));
                                    break;
                                case ContentAlignment.BottomLeft:
                                    frmTips.Location = new Point(10, size.Height - 100 - (i + 1) * (frmTips.Height + 10));
                                    break;
                                case ContentAlignment.BottomRight:
                                    frmTips.Location = new Point(size.Width - frmTips.Width - 10, size.Height - 100 - (i + 1) * (frmTips.Height + 10));
                                    break;
                                case ContentAlignment.MiddleCenter:
                                    frmTips.Location = new Point((size.Width - frmTips.Width) / 2, size.Height - (size.Height - list.Count * (frmTips.Height + 10)) / 2 - (i + 1) * (frmTips.Height + 10));
                                    break;
                                case ContentAlignment.MiddleLeft:
                                    frmTips.Location = new Point(10, size.Height - (size.Height - list.Count * (frmTips.Height + 10)) / 2 - (i + 1) * (frmTips.Height + 10));
                                    break;
                                case ContentAlignment.MiddleRight:
                                    frmTips.Location = new Point(size.Width - frmTips.Width - 10, size.Height - (size.Height - list.Count * (frmTips.Height + 10)) / 2 - (i + 1) * (frmTips.Height + 10));
                                    break;
                                case ContentAlignment.TopCenter:
                                    frmTips.Location = new Point((size.Width - frmTips.Width) / 2, 10 + (i + 1) * (frmTips.Height + 10));
                                    break;
                                case ContentAlignment.TopLeft:
                                    frmTips.Location = new Point(10, 10 + (i + 1) * (frmTips.Height + 10));
                                    break;
                                case ContentAlignment.TopRight:
                                    frmTips.Location = new Point(size.Width - frmTips.Width - 10, 10 + (i + 1) * (frmTips.Height + 10));
                                    break;
                                default:
                                    break;
                            }
                        }

                    }
                }
            }
        }
        #endregion

        private void FrmTips_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (m_lastTips.Value == this)
                m_lastTips = new KeyValuePair<string, FrmTips>();
            m_lstTips.Remove(this);
            ReshowTips();

            for (int i = Application.OpenForms.Count - 1; i >= 0; i--)
            {
                if (Application.OpenForms[i].IsDisposed || !Application.OpenForms[i].Visible || Application.OpenForms[i] is FrmTips)
                {
                    continue;
                }
                else
                {
                    Timer t = new Timer();
                    t.Interval = 100;
                    var frm = Application.OpenForms[i];
                    t.Tick += (a, b) =>
                    {
                        t.Enabled = false;
                        if (!frm.IsDisposed)
                            ControlHelper.SetForegroundWindow(frm.Handle);
                    };
                    t.Enabled = true;
                    break;
                }
            }
        }

        private void FrmTips_Load(object sender, EventArgs e)
        {
            if (m_CloseTime > 0)
            {
                this.timer1.Interval = m_CloseTime;
                this.timer1.Enabled = true;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.timer1.Enabled = false;
            this.Close();
        }

        private void btnClose_MouseDown(object sender, MouseEventArgs e)
        {
            this.timer1.Enabled = false;
            this.Close();
        }

        public static FrmTips ShowTipsSuccess(Form frm, string strMsg)
        {
            return FrmTips.ShowTips(frm, strMsg, 3000, false, ContentAlignment.BottomCenter, null, TipsSizeMode.Large, null, TipsState.Success);
        }

        public static FrmTips ShowTipsError(Form frm, string strMsg)
        {
            return FrmTips.ShowTips(frm, strMsg, 3000, false, ContentAlignment.BottomCenter, null, TipsSizeMode.Large, null, TipsState.Error);
        }

        public static FrmTips ShowTipsInfo(Form frm, string strMsg)
        {
            return FrmTips.ShowTips(frm, strMsg, 3000, false, ContentAlignment.BottomCenter, null, TipsSizeMode.Large, null, TipsState.Info);
        }
        public static FrmTips ShowTipsWarning(Form frm, string strMsg)
        {
            return FrmTips.ShowTips(frm, strMsg, 3000, false, ContentAlignment.BottomCenter, null, TipsSizeMode.Large, null, TipsState.Warning);
        }

        private void FrmTips_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Dispose();
            GC.Collect();
        }

    }

    public enum TipsSizeMode
    {
        Small,
        Medium,
        Large,
        None
    }
    public enum TipsState
    {
        Default = -1,
        Success = -6566849,
        Info = -12477983,
        Warning = -357816,
        Error = -1097849
    }
}
namespace HZH_Controls.Forms
{
    partial class FrmTips
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmTips));
            this.lblMsg = new System.Windows.Forms.Label();
            this.btnClose = new System.Windows.Forms.PictureBox();
            this.pctStat = new System.Windows.Forms.PictureBox();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            ((System.ComponentModel.ISupportInitialize)(this.btnClose)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.pctStat)).BeginInit();
            this.SuspendLayout();
            // 
            // lblMsg
            // 
            this.lblMsg.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.lblMsg.BackColor = System.Drawing.Color.Transparent;
            this.lblMsg.Font = new System.Drawing.Font("微软雅黑", 12F);
            this.lblMsg.Location = new System.Drawing.Point(32, 0);
            this.lblMsg.Name = "lblMsg";
            this.lblMsg.Size = new System.Drawing.Size(279, 46);
            this.lblMsg.TabIndex = 1;
            this.lblMsg.Text = "提示信息";
            this.lblMsg.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // btnClose
            // 
            this.btnClose.Anchor = System.Windows.Forms.AnchorStyles.Right;
            this.btnClose.BackColor = System.Drawing.Color.Transparent;
            this.btnClose.Image = global::HZH_Controls.Properties.Resources.qty_delete;
            this.btnClose.Location = new System.Drawing.Point(313, 11);
            this.btnClose.Name = "btnClose";
            this.btnClose.Size = new System.Drawing.Size(25, 25);
            this.btnClose.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
            this.btnClose.TabIndex = 2;
            this.btnClose.TabStop = false;
            this.btnClose.MouseDown += new System.Windows.Forms.MouseEventHandler(this.btnClose_MouseDown);
            // 
            // pctStat
            // 
            this.pctStat.Anchor = System.Windows.Forms.AnchorStyles.Left;
            this.pctStat.BackColor = System.Drawing.Color.Transparent;
            this.pctStat.Image = global::HZH_Controls.Properties.Resources.alarm;
            this.pctStat.Location = new System.Drawing.Point(7, 13);
            this.pctStat.Name = "pctStat";
            this.pctStat.Size = new System.Drawing.Size(20, 20);
            this.pctStat.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
            this.pctStat.TabIndex = 0;
            this.pctStat.TabStop = false;
            // 
            // timer1
            // 
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            // 
            // FrmTips
            // 
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
            this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(247)))), ((int)(((byte)(247)))), ((int)(((byte)(247)))));
            this.ClientSize = new System.Drawing.Size(340, 47);
            this.Controls.Add(this.btnClose);
            this.Controls.Add(this.lblMsg);
            this.Controls.Add(this.pctStat);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.IsFullSize = false;
            this.IsShowRegion = true;
            this.Name = "FrmTips";
            this.ShowIcon = false;
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
            this.Text = "FrmTips";
            this.TopMost = true;
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.FrmTips_FormClosing);
            this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.FrmTips_FormClosed);
            this.Load += new System.EventHandler(this.FrmTips_Load);
            ((System.ComponentModel.ISupportInitialize)(this.btnClose)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pctStat)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.PictureBox pctStat;
        private System.Windows.Forms.Label lblMsg;
        private System.Windows.Forms.PictureBox btnClose;
        private System.Windows.Forms.Timer timer1;
    }
}

用处及效果

用处:向用户提示一些信息,但是这些信息又不是非常重要,不需要用户确定的时候可以用到这个东西

效果:

调用示例

1             FrmTips.ShowTipsError(this, "Error提示信息");
2             FrmTips.ShowTipsInfo(this, "Info提示信息");
3             FrmTips.ShowTipsSuccess(this, "Success提示信息");
4             FrmTips.ShowTipsWarning(this, "Warning提示信息");

最后的话

如果你喜欢的话,请到 HZHControls控件库: HZHControls控件库,c#的winform自定义控件,对触屏具有更好的操作支持,项目是基于framework4.0,完全原生控件开发,没有使用任何第三方控件,你可以放心的用在你的项目中(winfromcontrol/winformcontrol/.net)。还有更丰富的工业控件持续增加中~~~ 点个星 星吧

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐