前提
入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
官网:https://www.hzhcontrols.cn
GitHub:https://github.com/kwwwvagaa/NetWinformControl
如果觉得写的还行,请点个 star 支持一下吧
麻烦博客下方点个【推荐】,谢谢
NuGet
Install-Package HZH_Controls
目录
c#Winform自定义控件-目录_c#winform自定义控件-有图标的按钮-CSDN博客
用处及效果
1 HZH_Controls.Forms.FrmAnchorTips.ShowTips(button1, "测试提示信息\nLEFT", AnchorTipsLocation.LEFT);
2 HZH_Controls.Forms.FrmAnchorTips.ShowTips(button1, "测试提示信息\nRIGHT", AnchorTipsLocation.RIGHT);
3 HZH_Controls.Forms.FrmAnchorTips.ShowTips(button1, "测试提示信息\nTOP", AnchorTipsLocation.TOP);
4 HZH_Controls.Forms.FrmAnchorTips.ShowTips(button1, "测试提示信息\nBOTTOM", AnchorTipsLocation.BOTTOM);
准备工作
依然是GDI+画图,不懂可以自行百度一下
开始
思路是:根据参数画图,根据图显示不规则窗体
添加一个窗体FrmAnchorTips
重写一些函数
1 #region Override
2
3 protected override void OnClosing(CancelEventArgs e)
4 {
5 e.Cancel = true;
6 base.OnClosing(e);
7 haveHandle = false;
8 this.Dispose();
9 }
10
11 protected override void OnHandleCreated(EventArgs e)
12 {
13 InitializeStyles();
14 base.OnHandleCreated(e);
15 haveHandle = true;
16 }
17
18 protected override CreateParams CreateParams
19 {
20 get
21 {
22 CreateParams cParms = base.CreateParams;
23 cParms.ExStyle |= 0x00080000; // WS_EX_LAYERED
24 return cParms;
25 }
26 }
27
28 #endregion
1 private void InitializeStyles()
2 {
3 SetStyle(ControlStyles.AllPaintingInWmPaint, true);
4 SetStyle(ControlStyles.UserPaint, true);
5 UpdateStyles();
6 }
根据图片显示窗体,这个是网上copy的
1 #region 根据图片显示窗体 English:Display Forms Based on Pictures
2 /// <summary>
3 /// 功能描述:根据图片显示窗体 English:Display Forms Based on Pictures
4 /// 作 者:HZH
5 /// 创建日期:2019-08-29 15:31:16
6 /// 任务编号:
7 /// </summary>
8 /// <param name="bitmap">bitmap</param>
9 private void SetBits(Bitmap bitmap)
10 {
11 if (!haveHandle) return;
12
13 if (!Bitmap.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Bitmap.IsAlphaPixelFormat(bitmap.PixelFormat))
14 throw new ApplicationException("The picture must be 32bit picture with alpha channel.");
15
16 IntPtr oldBits = IntPtr.Zero;
17 IntPtr screenDC = Win32.GetDC(IntPtr.Zero);
18 IntPtr hBitmap = IntPtr.Zero;
19 IntPtr memDc = Win32.CreateCompatibleDC(screenDC);
20
21 try
22 {
23 Win32.Point topLoc = new Win32.Point(Left, Top);
24 Win32.Size bitMapSize = new Win32.Size(bitmap.Width, bitmap.Height);
25 Win32.BLENDFUNCTION blendFunc = new Win32.BLENDFUNCTION();
26 Win32.Point srcLoc = new Win32.Point(0, 0);
27
28 hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
29 oldBits = Win32.SelectObject(memDc, hBitmap);
30
31 blendFunc.BlendOp = Win32.AC_SRC_OVER;
32 blendFunc.SourceConstantAlpha = 255;
33 blendFunc.AlphaFormat = Win32.AC_SRC_ALPHA;
34 blendFunc.BlendFlags = 0;
35
36 Win32.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, Win32.ULW_ALPHA);
37 }
38 finally
39 {
40 if (hBitmap != IntPtr.Zero)
41 {
42 Win32.SelectObject(memDc, oldBits);
43 Win32.DeleteObject(hBitmap);
44 }
45 Win32.ReleaseDC(IntPtr.Zero, screenDC);
46 Win32.DeleteDC(memDc);
47 }
48 }
49 #endregion
然后是win32类
1 class Win32
2 {
3 [StructLayout(LayoutKind.Sequential)]
4 public struct Size
5 {
6 public Int32 cx;
7 public Int32 cy;
8
9 public Size(Int32 x, Int32 y)
10 {
11 cx = x;
12 cy = y;
13 }
14 }
15
16 [StructLayout(LayoutKind.Sequential, Pack = 1)]
17 public struct BLENDFUNCTION
18 {
19 public byte BlendOp;
20 public byte BlendFlags;
21 public byte SourceConstantAlpha;
22 public byte AlphaFormat;
23 }
24
25 [StructLayout(LayoutKind.Sequential)]
26 public struct Point
27 {
28 public Int32 x;
29 public Int32 y;
30
31 public Point(Int32 x, Int32 y)
32 {
33 this.x = x;
34 this.y = y;
35 }
36 }
37
38 public const byte AC_SRC_OVER = 0;
39 public const Int32 ULW_ALPHA = 2;
40 public const byte AC_SRC_ALPHA = 1;
41
42 [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
43 public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
44
45 [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
46 public static extern IntPtr GetDC(IntPtr hWnd);
47
48 [DllImport("gdi32.dll", ExactSpelling = true)]
49 public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObj);
50
51 [DllImport("user32.dll", ExactSpelling = true)]
52 public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
53
54 [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
55 public static extern int DeleteDC(IntPtr hDC);
56
57 [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
58 public static extern int DeleteObject(IntPtr hObj);
59
60 [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
61 public static extern int UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref Point pptDst, ref Size psize, IntPtr hdcSrc, ref Point pptSrc, Int32 crKey, ref BLENDFUNCTION pblend, Int32 dwFlags);
62
63 [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
64 public static extern IntPtr ExtCreateRegion(IntPtr lpXform, uint nCount, IntPtr rgnData);
65 }
然后就是构造函数了,根据传入参数,画出图片并设置窗体
1 #region 构造函数 English:Constructor
2 /// <summary>
3 /// 功能描述:构造函数 English:Constructor
4 /// 作 者:HZH
5 /// 创建日期:2019-08-29 15:27:51
6 /// 任务编号:
7 /// </summary>
8 /// <param name="rectControl">停靠区域</param>
9 /// <param name="strMsg">消息</param>
10 /// <param name="location">显示方位</param>
11 /// <param name="background">背景色</param>
12 /// <param name="foreColor">文字颜色</param>
13 /// <param name="fontSize">文字大小</param>
14 /// <param name="autoCloseTime">自动关闭时间,当<=0时不自动关闭</param>
15 private FrmAnchorTips(
16 Rectangle rectControl,
17 string strMsg,
18 AnchorTipsLocation location = AnchorTipsLocation.RIGHT,
19 Color? background = null,
20 Color? foreColor = null,
21 int fontSize = 10,
22 int autoCloseTime = 5000)
23 {
24 InitializeComponent();
25 Graphics g = this.CreateGraphics();
26 Font _font = new Font("微软雅黑", fontSize);
27 Color _background = background == null ? Color.FromArgb(255, 77, 58) : background.Value;
28 Color _foreColor = foreColor == null ? Color.White : foreColor.Value;
29 System.Drawing.SizeF sizeText = g.MeasureString(strMsg, _font);
30 g.Dispose();
31 var formSize = new Size((int)sizeText.Width + 20, (int)sizeText.Height + 20);
32 if (formSize.Width < 20)
33 formSize.Width = 20;
34 if (formSize.Height < 20)
35 formSize.Height = 20;
36 if (location == AnchorTipsLocation.LEFT || location == AnchorTipsLocation.RIGHT)
37 {
38 formSize.Width += 20;
39 }
40 else
41 {
42 formSize.Height += 20;
43 }
44
45 #region 获取窗体path English:Get the form path
46 GraphicsPath path = new GraphicsPath();
47 Rectangle rect;
48 switch (location)
49 {
50 case AnchorTipsLocation.TOP:
51 rect = new Rectangle(1, 1, formSize.Width - 2, formSize.Height - 20 - 1);
52 this.Location = new Point(rectControl.X + (rectControl.Width - rect.Width) / 2, rectControl.Y - rect.Height - 20);
53 break;
54 case AnchorTipsLocation.RIGHT:
55 rect = new Rectangle(20, 1, formSize.Width - 20 - 1, formSize.Height - 2);
56 this.Location = new Point(rectControl.Right, rectControl.Y + (rectControl.Height - rect.Height) / 2);
57 break;
58 case AnchorTipsLocation.BOTTOM:
59 rect = new Rectangle(1, 20, formSize.Width - 2, formSize.Height - 20 - 1);
60 this.Location = new Point(rectControl.X + (rectControl.Width - rect.Width) / 2, rectControl.Bottom);
61 break;
62 default:
63 rect = new Rectangle(1, 1, formSize.Width - 20 - 1, formSize.Height - 2);
64 this.Location = new Point(rectControl.X - rect.Width - 20, rectControl.Y + (rectControl.Height - rect.Height) / 2);
65 break;
66 }
67 int cornerRadius = 2;
68
69 path.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);//左上角
70 #region 上边
71 if (location == AnchorTipsLocation.BOTTOM)
72 {
73 path.AddLine(rect.X + cornerRadius, rect.Y, rect.Left + rect.Width / 2 - 10, rect.Y);//上
74 path.AddLine(rect.Left + rect.Width / 2 - 10, rect.Y, rect.Left + rect.Width / 2, rect.Y - 19);//上
75 path.AddLine(rect.Left + rect.Width / 2, rect.Y - 19, rect.Left + rect.Width / 2 + 10, rect.Y);//上
76 path.AddLine(rect.Left + rect.Width / 2 + 10, rect.Y, rect.Right - cornerRadius * 2, rect.Y);//上
77 }
78 else
79 {
80 path.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);//上
81 }
82 #endregion
83 path.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);//右上角
84 #region 右边
85 if (location == AnchorTipsLocation.LEFT)
86 {
87 path.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height / 2 - 10);//右
88 path.AddLine(rect.Right, rect.Y + rect.Height / 2 - 10, rect.Right + 19, rect.Y + rect.Height / 2);//右
89 path.AddLine(rect.Right + 19, rect.Y + rect.Height / 2, rect.Right, rect.Y + rect.Height / 2 + 10);//右
90 path.AddLine(rect.Right, rect.Y + rect.Height / 2 + 10, rect.Right, rect.Y + rect.Height - cornerRadius * 2);//右
91 }
92 else
93 {
94 path.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);//右
95 }
96 #endregion
97 path.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);//右下角
98 #region 下边
99 if (location == AnchorTipsLocation.TOP)
100 {
101 path.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.Left + rect.Width / 2 + 10, rect.Bottom);
102 path.AddLine(rect.Left + rect.Width / 2 + 10, rect.Bottom, rect.Left + rect.Width / 2, rect.Bottom + 19);
103 path.AddLine(rect.Left + rect.Width / 2, rect.Bottom + 19, rect.Left + rect.Width / 2 - 10, rect.Bottom);
104 path.AddLine(rect.Left + rect.Width / 2 - 10, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
105 }
106 else
107 {
108 path.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
109 }
110 #endregion
111 path.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);//左下角
112 #region 左边
113 if (location == AnchorTipsLocation.RIGHT)
114 {
115 path.AddLine(rect.Left, rect.Y + cornerRadius * 2, rect.Left, rect.Y + rect.Height / 2 - 10);//左
116 path.AddLine(rect.Left, rect.Y + rect.Height / 2 - 10, rect.Left - 19, rect.Y + rect.Height / 2);//左
117 path.AddLine(rect.Left - 19, rect.Y + rect.Height / 2, rect.Left, rect.Y + rect.Height / 2 + 10);//左
118 path.AddLine(rect.Left, rect.Y + rect.Height / 2 + 10, rect.Left, rect.Y + rect.Height - cornerRadius * 2);//左
119 }
120 else
121 {
122 path.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);//左
123 }
124 #endregion
125 path.CloseFigure();
126 #endregion
127
128 Bitmap bit = new Bitmap(formSize.Width, formSize.Height);
129 this.Size = formSize;
130
131 #region 画图 English:Drawing
132 Graphics gBit = Graphics.FromImage(bit);
133 gBit.SetGDIHigh();
134 gBit.FillPath(new SolidBrush(_background), path);
135 gBit.DrawString(strMsg, _font, new SolidBrush(_foreColor), rect.Location + new Size(10, 10));
136 gBit.Dispose();
137 #endregion
138
139 SetBits(bit);
140 if (autoCloseTime > 0)
141 {
142 Timer t = new Timer();
143 t.Interval = autoCloseTime;
144 t.Tick += (a, b) =>
145 {
146 this.Close();
147 };
148 t.Enabled = true;
149 }
150 }
151 #endregion
再来2个静态函数以供调用
1 #region 显示一个提示 English:Show a hint
2 /// <summary>
3 /// 功能描述:显示一个提示 English:Show a hint
4 /// 作 者:HZH
5 /// 创建日期:2019-08-29 15:28:58
6 /// 任务编号:
7 /// </summary>
8 /// <param name="parentControl">停靠控件</param>
9 /// <param name="strMsg">消息</param>
10 /// <param name="location">显示方位</param>
11 /// <param name="background">背景色</param>
12 /// <param name="foreColor">文字颜色</param>
13 /// <param name="deviation">偏移量</param>
14 /// <param name="fontSize">文字大小</param>
15 /// <param name="autoCloseTime">自动关闭时间,当<=0时不自动关闭</param>
16 public static FrmAnchorTips ShowTips(
17 Control parentControl,
18 string strMsg,
19 AnchorTipsLocation location = AnchorTipsLocation.RIGHT,
20 Color? background = null,
21 Color? foreColor = null,
22 Size? deviation = null,
23 int fontSize = 10,
24 int autoCloseTime = 5000)
25 {
26 Point p;
27 if (parentControl is Form)
28 {
29 p = parentControl.Location;
30 }
31 else
32 {
33 p = parentControl.Parent.PointToScreen(parentControl.Location);
34 }
35 if (deviation != null)
36 {
37 p = p + deviation.Value;
38 }
39 return ShowTips(parentControl.FindForm(), new Rectangle(p, parentControl.Size), strMsg, location, background, foreColor, fontSize, autoCloseTime);
40 }
41 #endregion
42
43 #region 显示一个提示 English:Show a hint
44 /// <summary>
45 /// 功能描述:显示一个提示 English:Show a hint
46 /// 作 者:HZH
47 /// 创建日期:2019-08-29 15:29:07
48 /// 任务编号:
49 /// </summary>
50 /// <param name="parentForm">父窗体</param>
51 /// <param name="rectControl">停靠区域</param>
52 /// <param name="strMsg">消息</param>
53 /// <param name="location">显示方位</param>
54 /// <param name="background">背景色</param>
55 /// <param name="foreColor">文字颜色</param>
56 /// <param name="fontSize">文字大小</param>
57 /// <param name="autoCloseTime">自动关闭时间,当<=0时不自动关闭</param>
58 /// <returns>返回值</returns>
59 public static FrmAnchorTips ShowTips(
60 Form parentForm,
61 Rectangle rectControl,
62 string strMsg,
63 AnchorTipsLocation location = AnchorTipsLocation.RIGHT,
64 Color? background = null,
65 Color? foreColor = null,
66 int fontSize = 10,
67 int autoCloseTime = 5000)
68 {
69 FrmAnchorTips frm = new FrmAnchorTips(rectControl, strMsg, location, background, foreColor, fontSize, autoCloseTime);
70 frm.TopMost = true;
71 frm.Show(parentForm);
72 return frm;
73 }
74 #endregion
全部代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace HZH_Controls.Forms
{
public partial class FrmAnchorTips : Form
{
bool haveHandle = false;
#region 构造函数 English:Constructor
/// <summary>
/// 功能描述:构造函数 English:Constructor
/// 作 者:HZH
/// 创建日期:2019-08-29 15:27:51
/// 任务编号:
/// </summary>
/// <param name="rectControl">停靠区域</param>
/// <param name="strMsg">消息</param>
/// <param name="location">显示方位</param>
/// <param name="background">背景色</param>
/// <param name="foreColor">文字颜色</param>
/// <param name="fontSize">文字大小</param>
/// <param name="autoCloseTime">自动关闭时间,当<=0时不自动关闭</param>
private FrmAnchorTips(
Rectangle rectControl,
string strMsg,
AnchorTipsLocation location = AnchorTipsLocation.RIGHT,
Color? background = null,
Color? foreColor = null,
int fontSize = 10,
int autoCloseTime = 5000)
{
InitializeComponent();
Graphics g = this.CreateGraphics();
Font _font = new Font("微软雅黑", fontSize);
Color _background = background == null ? Color.FromArgb(255, 77, 58) : background.Value;
Color _foreColor = foreColor == null ? Color.White : foreColor.Value;
System.Drawing.SizeF sizeText = g.MeasureString(strMsg, _font);
g.Dispose();
var formSize = new Size((int)sizeText.Width + 20, (int)sizeText.Height + 20);
if (formSize.Width < 20)
formSize.Width = 20;
if (formSize.Height < 20)
formSize.Height = 20;
if (location == AnchorTipsLocation.LEFT || location == AnchorTipsLocation.RIGHT)
{
formSize.Width += 20;
}
else
{
formSize.Height += 20;
}
#region 获取窗体path English:Get the form path
GraphicsPath path = new GraphicsPath();
Rectangle rect;
switch (location)
{
case AnchorTipsLocation.TOP:
rect = new Rectangle(1, 1, formSize.Width - 2, formSize.Height - 20 - 1);
this.Location = new Point(rectControl.X + (rectControl.Width - rect.Width) / 2, rectControl.Y - rect.Height - 20);
break;
case AnchorTipsLocation.RIGHT:
rect = new Rectangle(20, 1, formSize.Width - 20 - 1, formSize.Height - 2);
this.Location = new Point(rectControl.Right, rectControl.Y + (rectControl.Height - rect.Height) / 2);
break;
case AnchorTipsLocation.BOTTOM:
rect = new Rectangle(1, 20, formSize.Width - 2, formSize.Height - 20 - 1);
this.Location = new Point(rectControl.X + (rectControl.Width - rect.Width) / 2, rectControl.Bottom);
break;
default:
rect = new Rectangle(1, 1, formSize.Width - 20 - 1, formSize.Height - 2);
this.Location = new Point(rectControl.X - rect.Width - 20, rectControl.Y + (rectControl.Height - rect.Height) / 2);
break;
}
int cornerRadius = 2;
path.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);//左上角
#region 上边
if (location == AnchorTipsLocation.BOTTOM)
{
path.AddLine(rect.X + cornerRadius, rect.Y, rect.Left + rect.Width / 2 - 10, rect.Y);//上
path.AddLine(rect.Left + rect.Width / 2 - 10, rect.Y, rect.Left + rect.Width / 2, rect.Y - 19);//上
path.AddLine(rect.Left + rect.Width / 2, rect.Y - 19, rect.Left + rect.Width / 2 + 10, rect.Y);//上
path.AddLine(rect.Left + rect.Width / 2 + 10, rect.Y, rect.Right - cornerRadius * 2, rect.Y);//上
}
else
{
path.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);//上
}
#endregion
path.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);//右上角
#region 右边
if (location == AnchorTipsLocation.LEFT)
{
path.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height / 2 - 10);//右
path.AddLine(rect.Right, rect.Y + rect.Height / 2 - 10, rect.Right + 19, rect.Y + rect.Height / 2);//右
path.AddLine(rect.Right + 19, rect.Y + rect.Height / 2, rect.Right, rect.Y + rect.Height / 2 + 10);//右
path.AddLine(rect.Right, rect.Y + rect.Height / 2 + 10, rect.Right, rect.Y + rect.Height - cornerRadius * 2);//右
}
else
{
path.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);//右
}
#endregion
path.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);//右下角
#region 下边
if (location == AnchorTipsLocation.TOP)
{
path.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.Left + rect.Width / 2 + 10, rect.Bottom);
path.AddLine(rect.Left + rect.Width / 2 + 10, rect.Bottom, rect.Left + rect.Width / 2, rect.Bottom + 19);
path.AddLine(rect.Left + rect.Width / 2, rect.Bottom + 19, rect.Left + rect.Width / 2 - 10, rect.Bottom);
path.AddLine(rect.Left + rect.Width / 2 - 10, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
}
else
{
path.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
}
#endregion
path.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);//左下角
#region 左边
if (location == AnchorTipsLocation.RIGHT)
{
path.AddLine(rect.Left, rect.Y + cornerRadius * 2, rect.Left, rect.Y + rect.Height / 2 - 10);//左
path.AddLine(rect.Left, rect.Y + rect.Height / 2 - 10, rect.Left - 19, rect.Y + rect.Height / 2);//左
path.AddLine(rect.Left - 19, rect.Y + rect.Height / 2, rect.Left, rect.Y + rect.Height / 2 + 10);//左
path.AddLine(rect.Left, rect.Y + rect.Height / 2 + 10, rect.Left, rect.Y + rect.Height - cornerRadius * 2);//左
}
else
{
path.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);//左
}
#endregion
path.CloseFigure();
#endregion
Bitmap bit = new Bitmap(formSize.Width, formSize.Height);
this.Size = formSize;
#region 画图 English:Drawing
Graphics gBit = Graphics.FromImage(bit);
gBit.SetGDIHigh();
gBit.FillPath(new SolidBrush(_background), path);
gBit.DrawString(strMsg, _font, new SolidBrush(_foreColor), rect.Location + new Size(10, 10));
gBit.Dispose();
#endregion
SetBits(bit);
if (autoCloseTime > 0)
{
Timer t = new Timer();
t.Interval = autoCloseTime;
t.Tick += (a, b) =>
{
this.Close();
};
t.Enabled = true;
}
}
#endregion
#region 显示一个提示 English:Show a hint
/// <summary>
/// 功能描述:显示一个提示 English:Show a hint
/// 作 者:HZH
/// 创建日期:2019-08-29 15:28:58
/// 任务编号:
/// </summary>
/// <param name="parentControl">停靠控件</param>
/// <param name="strMsg">消息</param>
/// <param name="location">显示方位</param>
/// <param name="background">背景色</param>
/// <param name="foreColor">文字颜色</param>
/// <param name="deviation">偏移量</param>
/// <param name="fontSize">文字大小</param>
/// <param name="autoCloseTime">自动关闭时间,当<=0时不自动关闭</param>
public static FrmAnchorTips ShowTips(
Control parentControl,
string strMsg,
AnchorTipsLocation location = AnchorTipsLocation.RIGHT,
Color? background = null,
Color? foreColor = null,
Size? deviation = null,
int fontSize = 10,
int autoCloseTime = 5000)
{
Point p;
if (parentControl is Form)
{
p = parentControl.Location;
}
else
{
p = parentControl.Parent.PointToScreen(parentControl.Location);
}
if (deviation != null)
{
p = p + deviation.Value;
}
return ShowTips(parentControl.FindForm(), new Rectangle(p, parentControl.Size), strMsg, location, background, foreColor, fontSize, autoCloseTime);
}
#endregion
#region 显示一个提示 English:Show a hint
/// <summary>
/// 功能描述:显示一个提示 English:Show a hint
/// 作 者:HZH
/// 创建日期:2019-08-29 15:29:07
/// 任务编号:
/// </summary>
/// <param name="parentForm">父窗体</param>
/// <param name="rectControl">停靠区域</param>
/// <param name="strMsg">消息</param>
/// <param name="location">显示方位</param>
/// <param name="background">背景色</param>
/// <param name="foreColor">文字颜色</param>
/// <param name="fontSize">文字大小</param>
/// <param name="autoCloseTime">自动关闭时间,当<=0时不自动关闭</param>
/// <returns>返回值</returns>
public static FrmAnchorTips ShowTips(
Form parentForm,
Rectangle rectControl,
string strMsg,
AnchorTipsLocation location = AnchorTipsLocation.RIGHT,
Color? background = null,
Color? foreColor = null,
int fontSize = 10,
int autoCloseTime = 5000)
{
FrmAnchorTips frm = new FrmAnchorTips(rectControl, strMsg, location, background, foreColor, fontSize, autoCloseTime);
frm.TopMost = true;
frm.Show(parentForm);
return frm;
}
#endregion
#region Override
protected override void OnClosing(CancelEventArgs e)
{
e.Cancel = true;
base.OnClosing(e);
haveHandle = false;
this.Dispose();
}
protected override void OnHandleCreated(EventArgs e)
{
InitializeStyles();
base.OnHandleCreated(e);
haveHandle = true;
}
protected override CreateParams CreateParams
{
get
{
CreateParams cParms = base.CreateParams;
cParms.ExStyle |= 0x00080000; // WS_EX_LAYERED
return cParms;
}
}
#endregion
private void InitializeStyles()
{
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.UserPaint, true);
UpdateStyles();
}
#region 根据图片显示窗体 English:Display Forms Based on Pictures
/// <summary>
/// 功能描述:根据图片显示窗体 English:Display Forms Based on Pictures
/// 作 者:HZH
/// 创建日期:2019-08-29 15:31:16
/// 任务编号:
/// </summary>
/// <param name="bitmap">bitmap</param>
private void SetBits(Bitmap bitmap)
{
if (!haveHandle) return;
if (!Bitmap.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Bitmap.IsAlphaPixelFormat(bitmap.PixelFormat))
throw new ApplicationException("The picture must be 32bit picture with alpha channel.");
IntPtr oldBits = IntPtr.Zero;
IntPtr screenDC = Win32.GetDC(IntPtr.Zero);
IntPtr hBitmap = IntPtr.Zero;
IntPtr memDc = Win32.CreateCompatibleDC(screenDC);
try
{
Win32.Point topLoc = new Win32.Point(Left, Top);
Win32.Size bitMapSize = new Win32.Size(bitmap.Width, bitmap.Height);
Win32.BLENDFUNCTION blendFunc = new Win32.BLENDFUNCTION();
Win32.Point srcLoc = new Win32.Point(0, 0);
hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
oldBits = Win32.SelectObject(memDc, hBitmap);
blendFunc.BlendOp = Win32.AC_SRC_OVER;
blendFunc.SourceConstantAlpha = 255;
blendFunc.AlphaFormat = Win32.AC_SRC_ALPHA;
blendFunc.BlendFlags = 0;
Win32.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, Win32.ULW_ALPHA);
}
finally
{
if (hBitmap != IntPtr.Zero)
{
Win32.SelectObject(memDc, oldBits);
Win32.DeleteObject(hBitmap);
}
Win32.ReleaseDC(IntPtr.Zero, screenDC);
Win32.DeleteDC(memDc);
}
}
#endregion
}
public enum AnchorTipsLocation
{
LEFT,
TOP,
RIGHT,
BOTTOM
}
class Win32
{
[StructLayout(LayoutKind.Sequential)]
public struct Size
{
public Int32 cx;
public Int32 cy;
public Size(Int32 x, Int32 y)
{
cx = x;
cy = y;
}
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct BLENDFUNCTION
{
public byte BlendOp;
public byte BlendFlags;
public byte SourceConstantAlpha;
public byte AlphaFormat;
}
[StructLayout(LayoutKind.Sequential)]
public struct Point
{
public Int32 x;
public Int32 y;
public Point(Int32 x, Int32 y)
{
this.x = x;
this.y = y;
}
}
public const byte AC_SRC_OVER = 0;
public const Int32 ULW_ALPHA = 2;
public const byte AC_SRC_ALPHA = 1;
[DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
public static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("gdi32.dll", ExactSpelling = true)]
public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObj);
[DllImport("user32.dll", ExactSpelling = true)]
public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
public static extern int DeleteDC(IntPtr hDC);
[DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
public static extern int DeleteObject(IntPtr hObj);
[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
public static extern int UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref Point pptDst, ref Size psize, IntPtr hdcSrc, ref Point pptSrc, Int32 crKey, ref BLENDFUNCTION pblend, Int32 dwFlags);
[DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
public static extern IntPtr ExtCreateRegion(IntPtr lpXform, uint nCount, IntPtr rgnData);
}
}
namespace HZH_Controls.Forms
{
partial class FrmAnchorTips
{
/// <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.SuspendLayout();
//
// FrmAnchorTips
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(226, 83);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "FrmAnchorTips";
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.Text = "FrmAnchorTips";
this.ResumeLayout(false);
}
#endregion
}
}