当前位置:   article > 正文

(五十一)c#Winform自定义控件-文字提示_c# 控件提示语

c# 控件提示语

前提

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

GitHubhttps://github.com/kwwwvagaa/NetWinformControl

码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

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

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

麻烦博客下方点个【推荐】,谢谢

NuGet

Install-Package HZH_Controls

目录

https://blog.csdn.net/kwwwvagaa/article/details/100586547

用处及效果

  1. 1 HZH_Controls.Forms.FrmAnchorTips.ShowTips(button1, "测试提示信息\nLEFT", AnchorTipsLocation.LEFT);
  2. 2 HZH_Controls.Forms.FrmAnchorTips.ShowTips(button1, "测试提示信息\nRIGHT", AnchorTipsLocation.RIGHT);
  3. 3 HZH_Controls.Forms.FrmAnchorTips.ShowTips(button1, "测试提示信息\nTOP", AnchorTipsLocation.TOP);
  4. 4 HZH_Controls.Forms.FrmAnchorTips.ShowTips(button1, "测试提示信息\nBOTTOM", AnchorTipsLocation.BOTTOM);

 

准备工作

依然是GDI+画图,不懂可以自行百度一下

开始

思路是:根据参数画图,根据图显示不规则窗体

添加一个窗体FrmAnchorTips

重写一些函数

复制代码

  1. 1 #region Override
  2. 2
  3. 3 protected override void OnClosing(CancelEventArgs e)
  4. 4 {
  5. 5 e.Cancel = true;
  6. 6 base.OnClosing(e);
  7. 7 haveHandle = false;
  8. 8 this.Dispose();
  9. 9 }
  10. 10
  11. 11 protected override void OnHandleCreated(EventArgs e)
  12. 12 {
  13. 13 InitializeStyles();
  14. 14 base.OnHandleCreated(e);
  15. 15 haveHandle = true;
  16. 16 }
  17. 17
  18. 18 protected override CreateParams CreateParams
  19. 19 {
  20. 20 get
  21. 21 {
  22. 22 CreateParams cParms = base.CreateParams;
  23. 23 cParms.ExStyle |= 0x00080000; // WS_EX_LAYERED
  24. 24 return cParms;
  25. 25 }
  26. 26 }
  27. 27
  28. 28 #endregion

复制代码

复制代码

  1. 1 private void InitializeStyles()
  2. 2 {
  3. 3 SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  4. 4 SetStyle(ControlStyles.UserPaint, true);
  5. 5 UpdateStyles();
  6. 6 }

复制代码

根据图片显示窗体,这个是网上copy的

复制代码

  1. 1 #region 根据图片显示窗体 English:Display Forms Based on Pictures
  2. 2 /// <summary>
  3. 3 /// 功能描述:根据图片显示窗体 English:Display Forms Based on Pictures
  4. 4 /// 作  者:HZH
  5. 5 /// 创建日期:2019-08-29 15:31:16
  6. 6 /// 任务编号:
  7. 7 /// </summary>
  8. 8 /// <param name="bitmap">bitmap</param>
  9. 9 private void SetBits(Bitmap bitmap)
  10. 10 {
  11. 11 if (!haveHandle) return;
  12. 12
  13. 13 if (!Bitmap.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Bitmap.IsAlphaPixelFormat(bitmap.PixelFormat))
  14. 14 throw new ApplicationException("The picture must be 32bit picture with alpha channel.");
  15. 15
  16. 16 IntPtr oldBits = IntPtr.Zero;
  17. 17 IntPtr screenDC = Win32.GetDC(IntPtr.Zero);
  18. 18 IntPtr hBitmap = IntPtr.Zero;
  19. 19 IntPtr memDc = Win32.CreateCompatibleDC(screenDC);
  20. 20
  21. 21 try
  22. 22 {
  23. 23 Win32.Point topLoc = new Win32.Point(Left, Top);
  24. 24 Win32.Size bitMapSize = new Win32.Size(bitmap.Width, bitmap.Height);
  25. 25 Win32.BLENDFUNCTION blendFunc = new Win32.BLENDFUNCTION();
  26. 26 Win32.Point srcLoc = new Win32.Point(0, 0);
  27. 27
  28. 28 hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
  29. 29 oldBits = Win32.SelectObject(memDc, hBitmap);
  30. 30
  31. 31 blendFunc.BlendOp = Win32.AC_SRC_OVER;
  32. 32 blendFunc.SourceConstantAlpha = 255;
  33. 33 blendFunc.AlphaFormat = Win32.AC_SRC_ALPHA;
  34. 34 blendFunc.BlendFlags = 0;
  35. 35
  36. 36 Win32.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, Win32.ULW_ALPHA);
  37. 37 }
  38. 38 finally
  39. 39 {
  40. 40 if (hBitmap != IntPtr.Zero)
  41. 41 {
  42. 42 Win32.SelectObject(memDc, oldBits);
  43. 43 Win32.DeleteObject(hBitmap);
  44. 44 }
  45. 45 Win32.ReleaseDC(IntPtr.Zero, screenDC);
  46. 46 Win32.DeleteDC(memDc);
  47. 47 }
  48. 48 }
  49. 49 #endregion

复制代码

然后是win32类

复制代码

  1. 1 class Win32
  2. 2 {
  3. 3 [StructLayout(LayoutKind.Sequential)]
  4. 4 public struct Size
  5. 5 {
  6. 6 public Int32 cx;
  7. 7 public Int32 cy;
  8. 8
  9. 9 public Size(Int32 x, Int32 y)
  10. 10 {
  11. 11 cx = x;
  12. 12 cy = y;
  13. 13 }
  14. 14 }
  15. 15
  16. 16 [StructLayout(LayoutKind.Sequential, Pack = 1)]
  17. 17 public struct BLENDFUNCTION
  18. 18 {
  19. 19 public byte BlendOp;
  20. 20 public byte BlendFlags;
  21. 21 public byte SourceConstantAlpha;
  22. 22 public byte AlphaFormat;
  23. 23 }
  24. 24
  25. 25 [StructLayout(LayoutKind.Sequential)]
  26. 26 public struct Point
  27. 27 {
  28. 28 public Int32 x;
  29. 29 public Int32 y;
  30. 30
  31. 31 public Point(Int32 x, Int32 y)
  32. 32 {
  33. 33 this.x = x;
  34. 34 this.y = y;
  35. 35 }
  36. 36 }
  37. 37
  38. 38 public const byte AC_SRC_OVER = 0;
  39. 39 public const Int32 ULW_ALPHA = 2;
  40. 40 public const byte AC_SRC_ALPHA = 1;
  41. 41
  42. 42 [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
  43. 43 public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
  44. 44
  45. 45 [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
  46. 46 public static extern IntPtr GetDC(IntPtr hWnd);
  47. 47
  48. 48 [DllImport("gdi32.dll", ExactSpelling = true)]
  49. 49 public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObj);
  50. 50
  51. 51 [DllImport("user32.dll", ExactSpelling = true)]
  52. 52 public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
  53. 53
  54. 54 [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
  55. 55 public static extern int DeleteDC(IntPtr hDC);
  56. 56
  57. 57 [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
  58. 58 public static extern int DeleteObject(IntPtr hObj);
  59. 59
  60. 60 [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
  61. 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. 62
  63. 63 [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
  64. 64 public static extern IntPtr ExtCreateRegion(IntPtr lpXform, uint nCount, IntPtr rgnData);
  65. 65 }

复制代码

然后就是构造函数了,根据传入参数,画出图片并设置窗体

复制代码

  1. 1 #region 构造函数 English:Constructor
  2. 2 /// <summary>
  3. 3 /// 功能描述:构造函数 English:Constructor
  4. 4 /// 作  者:HZH
  5. 5 /// 创建日期:2019-08-29 15:27:51
  6. 6 /// 任务编号:
  7. 7 /// </summary>
  8. 8 /// <param name="rectControl">停靠区域</param>
  9. 9 /// <param name="strMsg">消息</param>
  10. 10 /// <param name="location">显示方位</param>
  11. 11 /// <param name="background">背景色</param>
  12. 12 /// <param name="foreColor">文字颜色</param>
  13. 13 /// <param name="fontSize">文字大小</param>
  14. 14 /// <param name="autoCloseTime">自动关闭时间,当<=0时不自动关闭</param>
  15. 15 private FrmAnchorTips(
  16. 16 Rectangle rectControl,
  17. 17 string strMsg,
  18. 18 AnchorTipsLocation location = AnchorTipsLocation.RIGHT,
  19. 19 Color? background = null,
  20. 20 Color? foreColor = null,
  21. 21 int fontSize = 10,
  22. 22 int autoCloseTime = 5000)
  23. 23 {
  24. 24 InitializeComponent();
  25. 25 Graphics g = this.CreateGraphics();
  26. 26 Font _font = new Font("微软雅黑", fontSize);
  27. 27 Color _background = background == null ? Color.FromArgb(255, 77, 58) : background.Value;
  28. 28 Color _foreColor = foreColor == null ? Color.White : foreColor.Value;
  29. 29 System.Drawing.SizeF sizeText = g.MeasureString(strMsg, _font);
  30. 30 g.Dispose();
  31. 31 var formSize = new Size((int)sizeText.Width + 20, (int)sizeText.Height + 20);
  32. 32 if (formSize.Width < 20)
  33. 33 formSize.Width = 20;
  34. 34 if (formSize.Height < 20)
  35. 35 formSize.Height = 20;
  36. 36 if (location == AnchorTipsLocation.LEFT || location == AnchorTipsLocation.RIGHT)
  37. 37 {
  38. 38 formSize.Width += 20;
  39. 39 }
  40. 40 else
  41. 41 {
  42. 42 formSize.Height += 20;
  43. 43 }
  44. 44
  45. 45 #region 获取窗体path English:Get the form path
  46. 46 GraphicsPath path = new GraphicsPath();
  47. 47 Rectangle rect;
  48. 48 switch (location)
  49. 49 {
  50. 50 case AnchorTipsLocation.TOP:
  51. 51 rect = new Rectangle(1, 1, formSize.Width - 2, formSize.Height - 20 - 1);
  52. 52 this.Location = new Point(rectControl.X + (rectControl.Width - rect.Width) / 2, rectControl.Y - rect.Height - 20);
  53. 53 break;
  54. 54 case AnchorTipsLocation.RIGHT:
  55. 55 rect = new Rectangle(20, 1, formSize.Width - 20 - 1, formSize.Height - 2);
  56. 56 this.Location = new Point(rectControl.Right, rectControl.Y + (rectControl.Height - rect.Height) / 2);
  57. 57 break;
  58. 58 case AnchorTipsLocation.BOTTOM:
  59. 59 rect = new Rectangle(1, 20, formSize.Width - 2, formSize.Height - 20 - 1);
  60. 60 this.Location = new Point(rectControl.X + (rectControl.Width - rect.Width) / 2, rectControl.Bottom);
  61. 61 break;
  62. 62 default:
  63. 63 rect = new Rectangle(1, 1, formSize.Width - 20 - 1, formSize.Height - 2);
  64. 64 this.Location = new Point(rectControl.X - rect.Width - 20, rectControl.Y + (rectControl.Height - rect.Height) / 2);
  65. 65 break;
  66. 66 }
  67. 67 int cornerRadius = 2;
  68. 68
  69. 69 path.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);//左上角
  70. 70 #region 上边
  71. 71 if (location == AnchorTipsLocation.BOTTOM)
  72. 72 {
  73. 73 path.AddLine(rect.X + cornerRadius, rect.Y, rect.Left + rect.Width / 2 - 10, rect.Y);//上
  74. 74 path.AddLine(rect.Left + rect.Width / 2 - 10, rect.Y, rect.Left + rect.Width / 2, rect.Y - 19);//上
  75. 75 path.AddLine(rect.Left + rect.Width / 2, rect.Y - 19, rect.Left + rect.Width / 2 + 10, rect.Y);//上
  76. 76 path.AddLine(rect.Left + rect.Width / 2 + 10, rect.Y, rect.Right - cornerRadius * 2, rect.Y);//上
  77. 77 }
  78. 78 else
  79. 79 {
  80. 80 path.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);//上
  81. 81 }
  82. 82 #endregion
  83. 83 path.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);//右上角
  84. 84 #region 右边
  85. 85 if (location == AnchorTipsLocation.LEFT)
  86. 86 {
  87. 87 path.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height / 2 - 10);//右
  88. 88 path.AddLine(rect.Right, rect.Y + rect.Height / 2 - 10, rect.Right + 19, rect.Y + rect.Height / 2);//右
  89. 89 path.AddLine(rect.Right + 19, rect.Y + rect.Height / 2, rect.Right, rect.Y + rect.Height / 2 + 10);//右
  90. 90 path.AddLine(rect.Right, rect.Y + rect.Height / 2 + 10, rect.Right, rect.Y + rect.Height - cornerRadius * 2);//右
  91. 91 }
  92. 92 else
  93. 93 {
  94. 94 path.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);//右
  95. 95 }
  96. 96 #endregion
  97. 97 path.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);//右下角
  98. 98 #region 下边
  99. 99 if (location == AnchorTipsLocation.TOP)
  100. 100 {
  101. 101 path.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.Left + rect.Width / 2 + 10, rect.Bottom);
  102. 102 path.AddLine(rect.Left + rect.Width / 2 + 10, rect.Bottom, rect.Left + rect.Width / 2, rect.Bottom + 19);
  103. 103 path.AddLine(rect.Left + rect.Width / 2, rect.Bottom + 19, rect.Left + rect.Width / 2 - 10, rect.Bottom);
  104. 104 path.AddLine(rect.Left + rect.Width / 2 - 10, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
  105. 105 }
  106. 106 else
  107. 107 {
  108. 108 path.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
  109. 109 }
  110. 110 #endregion
  111. 111 path.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);//左下角
  112. 112 #region 左边
  113. 113 if (location == AnchorTipsLocation.RIGHT)
  114. 114 {
  115. 115 path.AddLine(rect.Left, rect.Y + cornerRadius * 2, rect.Left, rect.Y + rect.Height / 2 - 10);//左
  116. 116 path.AddLine(rect.Left, rect.Y + rect.Height / 2 - 10, rect.Left - 19, rect.Y + rect.Height / 2);//左
  117. 117 path.AddLine(rect.Left - 19, rect.Y + rect.Height / 2, rect.Left, rect.Y + rect.Height / 2 + 10);//左
  118. 118 path.AddLine(rect.Left, rect.Y + rect.Height / 2 + 10, rect.Left, rect.Y + rect.Height - cornerRadius * 2);//左
  119. 119 }
  120. 120 else
  121. 121 {
  122. 122 path.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);//左
  123. 123 }
  124. 124 #endregion
  125. 125 path.CloseFigure();
  126. 126 #endregion
  127. 127
  128. 128 Bitmap bit = new Bitmap(formSize.Width, formSize.Height);
  129. 129 this.Size = formSize;
  130. 130
  131. 131 #region 画图 English:Drawing
  132. 132 Graphics gBit = Graphics.FromImage(bit);
  133. 133 gBit.SetGDIHigh();
  134. 134 gBit.FillPath(new SolidBrush(_background), path);
  135. 135 gBit.DrawString(strMsg, _font, new SolidBrush(_foreColor), rect.Location + new Size(10, 10));
  136. 136 gBit.Dispose();
  137. 137 #endregion
  138. 138
  139. 139 SetBits(bit);
  140. 140 if (autoCloseTime > 0)
  141. 141 {
  142. 142 Timer t = new Timer();
  143. 143 t.Interval = autoCloseTime;
  144. 144 t.Tick += (a, b) =>
  145. 145 {
  146. 146 this.Close();
  147. 147 };
  148. 148 t.Enabled = true;
  149. 149 }
  150. 150 }
  151. 151 #endregion

复制代码

再来2个静态函数以供调用

复制代码

  1. 1 #region 显示一个提示 English:Show a hint
  2. 2 /// <summary>
  3. 3 /// 功能描述:显示一个提示 English:Show a hint
  4. 4 /// 作  者:HZH
  5. 5 /// 创建日期:2019-08-29 15:28:58
  6. 6 /// 任务编号:
  7. 7 /// </summary>
  8. 8 /// <param name="parentControl">停靠控件</param>
  9. 9 /// <param name="strMsg">消息</param>
  10. 10 /// <param name="location">显示方位</param>
  11. 11 /// <param name="background">背景色</param>
  12. 12 /// <param name="foreColor">文字颜色</param>
  13. 13 /// <param name="deviation">偏移量</param>
  14. 14 /// <param name="fontSize">文字大小</param>
  15. 15 /// <param name="autoCloseTime">自动关闭时间,当<=0时不自动关闭</param>
  16. 16 public static FrmAnchorTips ShowTips(
  17. 17 Control parentControl,
  18. 18 string strMsg,
  19. 19 AnchorTipsLocation location = AnchorTipsLocation.RIGHT,
  20. 20 Color? background = null,
  21. 21 Color? foreColor = null,
  22. 22 Size? deviation = null,
  23. 23 int fontSize = 10,
  24. 24 int autoCloseTime = 5000)
  25. 25 {
  26. 26 Point p;
  27. 27 if (parentControl is Form)
  28. 28 {
  29. 29 p = parentControl.Location;
  30. 30 }
  31. 31 else
  32. 32 {
  33. 33 p = parentControl.Parent.PointToScreen(parentControl.Location);
  34. 34 }
  35. 35 if (deviation != null)
  36. 36 {
  37. 37 p = p + deviation.Value;
  38. 38 }
  39. 39 return ShowTips(parentControl.FindForm(), new Rectangle(p, parentControl.Size), strMsg, location, background, foreColor, fontSize, autoCloseTime);
  40. 40 }
  41. 41 #endregion
  42. 42
  43. 43 #region 显示一个提示 English:Show a hint
  44. 44 /// <summary>
  45. 45 /// 功能描述:显示一个提示 English:Show a hint
  46. 46 /// 作  者:HZH
  47. 47 /// 创建日期:2019-08-29 15:29:07
  48. 48 /// 任务编号:
  49. 49 /// </summary>
  50. 50 /// <param name="parentForm">父窗体</param>
  51. 51 /// <param name="rectControl">停靠区域</param>
  52. 52 /// <param name="strMsg">消息</param>
  53. 53 /// <param name="location">显示方位</param>
  54. 54 /// <param name="background">背景色</param>
  55. 55 /// <param name="foreColor">文字颜色</param>
  56. 56 /// <param name="fontSize">文字大小</param>
  57. 57 /// <param name="autoCloseTime">自动关闭时间,当<=0时不自动关闭</param>
  58. 58 /// <returns>返回值</returns>
  59. 59 public static FrmAnchorTips ShowTips(
  60. 60 Form parentForm,
  61. 61 Rectangle rectControl,
  62. 62 string strMsg,
  63. 63 AnchorTipsLocation location = AnchorTipsLocation.RIGHT,
  64. 64 Color? background = null,
  65. 65 Color? foreColor = null,
  66. 66 int fontSize = 10,
  67. 67 int autoCloseTime = 5000)
  68. 68 {
  69. 69 FrmAnchorTips frm = new FrmAnchorTips(rectControl, strMsg, location, background, foreColor, fontSize, autoCloseTime);
  70. 70 frm.TopMost = true;
  71. 71 frm.Show(parentForm);
  72. 72 return frm;
  73. 73 }
  74. 74 #endregion

复制代码

全部代码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using System.Linq;
  8. using System.Runtime.InteropServices;
  9. using System.Text;
  10. using System.Windows.Forms;
  11. namespace HZH_Controls.Forms
  12. {
  13. public partial class FrmAnchorTips : Form
  14. {
  15. bool haveHandle = false;
  16. #region 构造函数 English:Constructor
  17. /// <summary>
  18. /// 功能描述:构造函数 English:Constructor
  19. /// 作  者:HZH
  20. /// 创建日期:2019-08-29 15:27:51
  21. /// 任务编号:
  22. /// </summary>
  23. /// <param name="rectControl">停靠区域</param>
  24. /// <param name="strMsg">消息</param>
  25. /// <param name="location">显示方位</param>
  26. /// <param name="background">背景色</param>
  27. /// <param name="foreColor">文字颜色</param>
  28. /// <param name="fontSize">文字大小</param>
  29. /// <param name="autoCloseTime">自动关闭时间,当<=0时不自动关闭</param>
  30. private FrmAnchorTips(
  31. Rectangle rectControl,
  32. string strMsg,
  33. AnchorTipsLocation location = AnchorTipsLocation.RIGHT,
  34. Color? background = null,
  35. Color? foreColor = null,
  36. int fontSize = 10,
  37. int autoCloseTime = 5000)
  38. {
  39. InitializeComponent();
  40. Graphics g = this.CreateGraphics();
  41. Font _font = new Font("微软雅黑", fontSize);
  42. Color _background = background == null ? Color.FromArgb(255, 77, 58) : background.Value;
  43. Color _foreColor = foreColor == null ? Color.White : foreColor.Value;
  44. System.Drawing.SizeF sizeText = g.MeasureString(strMsg, _font);
  45. g.Dispose();
  46. var formSize = new Size((int)sizeText.Width + 20, (int)sizeText.Height + 20);
  47. if (formSize.Width < 20)
  48. formSize.Width = 20;
  49. if (formSize.Height < 20)
  50. formSize.Height = 20;
  51. if (location == AnchorTipsLocation.LEFT || location == AnchorTipsLocation.RIGHT)
  52. {
  53. formSize.Width += 20;
  54. }
  55. else
  56. {
  57. formSize.Height += 20;
  58. }
  59. #region 获取窗体path English:Get the form path
  60. GraphicsPath path = new GraphicsPath();
  61. Rectangle rect;
  62. switch (location)
  63. {
  64. case AnchorTipsLocation.TOP:
  65. rect = new Rectangle(1, 1, formSize.Width - 2, formSize.Height - 20 - 1);
  66. this.Location = new Point(rectControl.X + (rectControl.Width - rect.Width) / 2, rectControl.Y - rect.Height - 20);
  67. break;
  68. case AnchorTipsLocation.RIGHT:
  69. rect = new Rectangle(20, 1, formSize.Width - 20 - 1, formSize.Height - 2);
  70. this.Location = new Point(rectControl.Right, rectControl.Y + (rectControl.Height - rect.Height) / 2);
  71. break;
  72. case AnchorTipsLocation.BOTTOM:
  73. rect = new Rectangle(1, 20, formSize.Width - 2, formSize.Height - 20 - 1);
  74. this.Location = new Point(rectControl.X + (rectControl.Width - rect.Width) / 2, rectControl.Bottom);
  75. break;
  76. default:
  77. rect = new Rectangle(1, 1, formSize.Width - 20 - 1, formSize.Height - 2);
  78. this.Location = new Point(rectControl.X - rect.Width - 20, rectControl.Y + (rectControl.Height - rect.Height) / 2);
  79. break;
  80. }
  81. int cornerRadius = 2;
  82. path.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);//左上角
  83. #region 上边
  84. if (location == AnchorTipsLocation.BOTTOM)
  85. {
  86. path.AddLine(rect.X + cornerRadius, rect.Y, rect.Left + rect.Width / 2 - 10, rect.Y);//上
  87. path.AddLine(rect.Left + rect.Width / 2 - 10, rect.Y, rect.Left + rect.Width / 2, rect.Y - 19);//上
  88. path.AddLine(rect.Left + rect.Width / 2, rect.Y - 19, rect.Left + rect.Width / 2 + 10, rect.Y);//上
  89. path.AddLine(rect.Left + rect.Width / 2 + 10, rect.Y, rect.Right - cornerRadius * 2, rect.Y);//上
  90. }
  91. else
  92. {
  93. path.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);//上
  94. }
  95. #endregion
  96. path.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);//右上角
  97. #region 右边
  98. if (location == AnchorTipsLocation.LEFT)
  99. {
  100. path.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height / 2 - 10);//右
  101. path.AddLine(rect.Right, rect.Y + rect.Height / 2 - 10, rect.Right + 19, rect.Y + rect.Height / 2);//右
  102. path.AddLine(rect.Right + 19, rect.Y + rect.Height / 2, rect.Right, rect.Y + rect.Height / 2 + 10);//右
  103. path.AddLine(rect.Right, rect.Y + rect.Height / 2 + 10, rect.Right, rect.Y + rect.Height - cornerRadius * 2);//右
  104. }
  105. else
  106. {
  107. path.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);//右
  108. }
  109. #endregion
  110. path.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);//右下角
  111. #region 下边
  112. if (location == AnchorTipsLocation.TOP)
  113. {
  114. path.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.Left + rect.Width / 2 + 10, rect.Bottom);
  115. path.AddLine(rect.Left + rect.Width / 2 + 10, rect.Bottom, rect.Left + rect.Width / 2, rect.Bottom + 19);
  116. path.AddLine(rect.Left + rect.Width / 2, rect.Bottom + 19, rect.Left + rect.Width / 2 - 10, rect.Bottom);
  117. path.AddLine(rect.Left + rect.Width / 2 - 10, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
  118. }
  119. else
  120. {
  121. path.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
  122. }
  123. #endregion
  124. path.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);//左下角
  125. #region 左边
  126. if (location == AnchorTipsLocation.RIGHT)
  127. {
  128. path.AddLine(rect.Left, rect.Y + cornerRadius * 2, rect.Left, rect.Y + rect.Height / 2 - 10);//左
  129. path.AddLine(rect.Left, rect.Y + rect.Height / 2 - 10, rect.Left - 19, rect.Y + rect.Height / 2);//左
  130. path.AddLine(rect.Left - 19, rect.Y + rect.Height / 2, rect.Left, rect.Y + rect.Height / 2 + 10);//左
  131. path.AddLine(rect.Left, rect.Y + rect.Height / 2 + 10, rect.Left, rect.Y + rect.Height - cornerRadius * 2);//左
  132. }
  133. else
  134. {
  135. path.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);//左
  136. }
  137. #endregion
  138. path.CloseFigure();
  139. #endregion
  140. Bitmap bit = new Bitmap(formSize.Width, formSize.Height);
  141. this.Size = formSize;
  142. #region 画图 English:Drawing
  143. Graphics gBit = Graphics.FromImage(bit);
  144. gBit.SetGDIHigh();
  145. gBit.FillPath(new SolidBrush(_background), path);
  146. gBit.DrawString(strMsg, _font, new SolidBrush(_foreColor), rect.Location + new Size(10, 10));
  147. gBit.Dispose();
  148. #endregion
  149. SetBits(bit);
  150. if (autoCloseTime > 0)
  151. {
  152. Timer t = new Timer();
  153. t.Interval = autoCloseTime;
  154. t.Tick += (a, b) =>
  155. {
  156. this.Close();
  157. };
  158. t.Enabled = true;
  159. }
  160. }
  161. #endregion
  162. #region 显示一个提示 English:Show a hint
  163. /// <summary>
  164. /// 功能描述:显示一个提示 English:Show a hint
  165. /// 作  者:HZH
  166. /// 创建日期:2019-08-29 15:28:58
  167. /// 任务编号:
  168. /// </summary>
  169. /// <param name="parentControl">停靠控件</param>
  170. /// <param name="strMsg">消息</param>
  171. /// <param name="location">显示方位</param>
  172. /// <param name="background">背景色</param>
  173. /// <param name="foreColor">文字颜色</param>
  174. /// <param name="deviation">偏移量</param>
  175. /// <param name="fontSize">文字大小</param>
  176. /// <param name="autoCloseTime">自动关闭时间,当<=0时不自动关闭</param>
  177. public static FrmAnchorTips ShowTips(
  178. Control parentControl,
  179. string strMsg,
  180. AnchorTipsLocation location = AnchorTipsLocation.RIGHT,
  181. Color? background = null,
  182. Color? foreColor = null,
  183. Size? deviation = null,
  184. int fontSize = 10,
  185. int autoCloseTime = 5000)
  186. {
  187. Point p;
  188. if (parentControl is Form)
  189. {
  190. p = parentControl.Location;
  191. }
  192. else
  193. {
  194. p = parentControl.Parent.PointToScreen(parentControl.Location);
  195. }
  196. if (deviation != null)
  197. {
  198. p = p + deviation.Value;
  199. }
  200. return ShowTips(parentControl.FindForm(), new Rectangle(p, parentControl.Size), strMsg, location, background, foreColor, fontSize, autoCloseTime);
  201. }
  202. #endregion
  203. #region 显示一个提示 English:Show a hint
  204. /// <summary>
  205. /// 功能描述:显示一个提示 English:Show a hint
  206. /// 作  者:HZH
  207. /// 创建日期:2019-08-29 15:29:07
  208. /// 任务编号:
  209. /// </summary>
  210. /// <param name="parentForm">父窗体</param>
  211. /// <param name="rectControl">停靠区域</param>
  212. /// <param name="strMsg">消息</param>
  213. /// <param name="location">显示方位</param>
  214. /// <param name="background">背景色</param>
  215. /// <param name="foreColor">文字颜色</param>
  216. /// <param name="fontSize">文字大小</param>
  217. /// <param name="autoCloseTime">自动关闭时间,当<=0时不自动关闭</param>
  218. /// <returns>返回值</returns>
  219. public static FrmAnchorTips ShowTips(
  220. Form parentForm,
  221. Rectangle rectControl,
  222. string strMsg,
  223. AnchorTipsLocation location = AnchorTipsLocation.RIGHT,
  224. Color? background = null,
  225. Color? foreColor = null,
  226. int fontSize = 10,
  227. int autoCloseTime = 5000)
  228. {
  229. FrmAnchorTips frm = new FrmAnchorTips(rectControl, strMsg, location, background, foreColor, fontSize, autoCloseTime);
  230. frm.TopMost = true;
  231. frm.Show(parentForm);
  232. return frm;
  233. }
  234. #endregion
  235. #region Override
  236. protected override void OnClosing(CancelEventArgs e)
  237. {
  238. e.Cancel = true;
  239. base.OnClosing(e);
  240. haveHandle = false;
  241. this.Dispose();
  242. }
  243. protected override void OnHandleCreated(EventArgs e)
  244. {
  245. InitializeStyles();
  246. base.OnHandleCreated(e);
  247. haveHandle = true;
  248. }
  249. protected override CreateParams CreateParams
  250. {
  251. get
  252. {
  253. CreateParams cParms = base.CreateParams;
  254. cParms.ExStyle |= 0x00080000; // WS_EX_LAYERED
  255. return cParms;
  256. }
  257. }
  258. #endregion
  259. private void InitializeStyles()
  260. {
  261. SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  262. SetStyle(ControlStyles.UserPaint, true);
  263. UpdateStyles();
  264. }
  265. #region 根据图片显示窗体 English:Display Forms Based on Pictures
  266. /// <summary>
  267. /// 功能描述:根据图片显示窗体 English:Display Forms Based on Pictures
  268. /// 作  者:HZH
  269. /// 创建日期:2019-08-29 15:31:16
  270. /// 任务编号:
  271. /// </summary>
  272. /// <param name="bitmap">bitmap</param>
  273. private void SetBits(Bitmap bitmap)
  274. {
  275. if (!haveHandle) return;
  276. if (!Bitmap.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Bitmap.IsAlphaPixelFormat(bitmap.PixelFormat))
  277. throw new ApplicationException("The picture must be 32bit picture with alpha channel.");
  278. IntPtr oldBits = IntPtr.Zero;
  279. IntPtr screenDC = Win32.GetDC(IntPtr.Zero);
  280. IntPtr hBitmap = IntPtr.Zero;
  281. IntPtr memDc = Win32.CreateCompatibleDC(screenDC);
  282. try
  283. {
  284. Win32.Point topLoc = new Win32.Point(Left, Top);
  285. Win32.Size bitMapSize = new Win32.Size(bitmap.Width, bitmap.Height);
  286. Win32.BLENDFUNCTION blendFunc = new Win32.BLENDFUNCTION();
  287. Win32.Point srcLoc = new Win32.Point(0, 0);
  288. hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
  289. oldBits = Win32.SelectObject(memDc, hBitmap);
  290. blendFunc.BlendOp = Win32.AC_SRC_OVER;
  291. blendFunc.SourceConstantAlpha = 255;
  292. blendFunc.AlphaFormat = Win32.AC_SRC_ALPHA;
  293. blendFunc.BlendFlags = 0;
  294. Win32.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, Win32.ULW_ALPHA);
  295. }
  296. finally
  297. {
  298. if (hBitmap != IntPtr.Zero)
  299. {
  300. Win32.SelectObject(memDc, oldBits);
  301. Win32.DeleteObject(hBitmap);
  302. }
  303. Win32.ReleaseDC(IntPtr.Zero, screenDC);
  304. Win32.DeleteDC(memDc);
  305. }
  306. }
  307. #endregion
  308. }
  309. public enum AnchorTipsLocation
  310. {
  311. LEFT,
  312. TOP,
  313. RIGHT,
  314. BOTTOM
  315. }
  316. class Win32
  317. {
  318. [StructLayout(LayoutKind.Sequential)]
  319. public struct Size
  320. {
  321. public Int32 cx;
  322. public Int32 cy;
  323. public Size(Int32 x, Int32 y)
  324. {
  325. cx = x;
  326. cy = y;
  327. }
  328. }
  329. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  330. public struct BLENDFUNCTION
  331. {
  332. public byte BlendOp;
  333. public byte BlendFlags;
  334. public byte SourceConstantAlpha;
  335. public byte AlphaFormat;
  336. }
  337. [StructLayout(LayoutKind.Sequential)]
  338. public struct Point
  339. {
  340. public Int32 x;
  341. public Int32 y;
  342. public Point(Int32 x, Int32 y)
  343. {
  344. this.x = x;
  345. this.y = y;
  346. }
  347. }
  348. public const byte AC_SRC_OVER = 0;
  349. public const Int32 ULW_ALPHA = 2;
  350. public const byte AC_SRC_ALPHA = 1;
  351. [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
  352. public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
  353. [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
  354. public static extern IntPtr GetDC(IntPtr hWnd);
  355. [DllImport("gdi32.dll", ExactSpelling = true)]
  356. public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObj);
  357. [DllImport("user32.dll", ExactSpelling = true)]
  358. public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
  359. [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
  360. public static extern int DeleteDC(IntPtr hDC);
  361. [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
  362. public static extern int DeleteObject(IntPtr hObj);
  363. [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
  364. 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);
  365. [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
  366. public static extern IntPtr ExtCreateRegion(IntPtr lpXform, uint nCount, IntPtr rgnData);
  367. }
  368. }
  1. namespace HZH_Controls.Forms
  2. {
  3. partial class FrmAnchorTips
  4. {
  5. /// <summary>
  6. /// Required designer variable.
  7. /// </summary>
  8. private System.ComponentModel.IContainer components = null;
  9. /// <summary>
  10. /// Clean up any resources being used.
  11. /// </summary>
  12. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  13. protected override void Dispose(bool disposing)
  14. {
  15. if (disposing && (components != null))
  16. {
  17. components.Dispose();
  18. }
  19. base.Dispose(disposing);
  20. }
  21. #region Windows Form Designer generated code
  22. /// <summary>
  23. /// Required method for Designer support - do not modify
  24. /// the contents of this method with the code editor.
  25. /// </summary>
  26. private void InitializeComponent()
  27. {
  28. this.SuspendLayout();
  29. //
  30. // FrmAnchorTips
  31. //
  32. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
  33. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  34. this.ClientSize = new System.Drawing.Size(226, 83);
  35. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
  36. this.Name = "FrmAnchorTips";
  37. this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
  38. this.Text = "FrmAnchorTips";
  39. this.ResumeLayout(false);
  40. }
  41. #endregion
  42. }
  43. }

最后的话

如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/137911
推荐阅读
相关标签
  

闽ICP备14008679号