(五十一)c#Winform自定义控件-文字提示-HZHControls
官网
前提
入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
如果觉得写的还行,请点个 star 支持一下吧
欢迎前来交流探讨: 企鹅群568015492 
麻烦博客下方点个【推荐】,谢谢
NuGet
Install-Package HZH_Controls
目录
https://www.cnblogs.com/bfyx/p/11364884.html
用处及效果

HZH_Controls.Forms.FrmAnchorTips.ShowTips(button1, "测试提示信息\nLEFT", AnchorTipsLocation.LEFT);
HZH_Controls.Forms.FrmAnchorTips.ShowTips(button1, "测试提示信息\nRIGHT", AnchorTipsLocation.RIGHT);
HZH_Controls.Forms.FrmAnchorTips.ShowTips(button1, "测试提示信息\nTOP", AnchorTipsLocation.TOP);
HZH_Controls.Forms.FrmAnchorTips.ShowTips(button1, "测试提示信息\nBOTTOM", AnchorTipsLocation.BOTTOM);
准备工作
依然是GDI+画图,不懂可以自行百度一下
开始
思路是:根据参数画图,根据图显示不规则窗体
添加一个窗体FrmAnchorTips
重写一些函数
#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();
}
根据图片显示窗体,这个是网上copy的
#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(, ); hBitmap = bitmap.GetHbitmap(Color.FromArgb());
oldBits = Win32.SelectObject(memDc, hBitmap); blendFunc.BlendOp = Win32.AC_SRC_OVER;
blendFunc.SourceConstantAlpha = ;
blendFunc.AlphaFormat = Win32.AC_SRC_ALPHA;
blendFunc.BlendFlags = ; Win32.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, , 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
然后是win32类
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 = )]
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 = ;
public const Int32 ULW_ALPHA = ;
public const byte AC_SRC_ALPHA = ; [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);
}
然后就是构造函数了,根据传入参数,画出图片并设置窗体
#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 = ,
int autoCloseTime = )
{
InitializeComponent();
Graphics g = this.CreateGraphics();
Font _font = new Font("微软雅黑", fontSize);
Color _background = background == null ? Color.FromArgb(, , ) : 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 + , (int)sizeText.Height + );
if (formSize.Width < )
formSize.Width = ;
if (formSize.Height < )
formSize.Height = ;
if (location == AnchorTipsLocation.LEFT || location == AnchorTipsLocation.RIGHT)
{
formSize.Width += ;
}
else
{
formSize.Height += ;
} #region 获取窗体path English:Get the form path
GraphicsPath path = new GraphicsPath();
Rectangle rect;
switch (location)
{
case AnchorTipsLocation.TOP:
rect = new Rectangle(, , formSize.Width - , formSize.Height - - );
this.Location = new Point(rectControl.X + (rectControl.Width - rect.Width) / , rectControl.Y - rect.Height - );
break;
case AnchorTipsLocation.RIGHT:
rect = new Rectangle(, , formSize.Width - - , formSize.Height - );
this.Location = new Point(rectControl.Right, rectControl.Y + (rectControl.Height - rect.Height) / );
break;
case AnchorTipsLocation.BOTTOM:
rect = new Rectangle(, , formSize.Width - , formSize.Height - - );
this.Location = new Point(rectControl.X + (rectControl.Width - rect.Width) / , rectControl.Bottom);
break;
default:
rect = new Rectangle(, , formSize.Width - - , formSize.Height - );
this.Location = new Point(rectControl.X - rect.Width - , rectControl.Y + (rectControl.Height - rect.Height) / );
break;
}
int cornerRadius = ; path.AddArc(rect.X, rect.Y, cornerRadius * , cornerRadius * , , );//左上角
#region 上边
if (location == AnchorTipsLocation.BOTTOM)
{
path.AddLine(rect.X + cornerRadius, rect.Y, rect.Left + rect.Width / - , rect.Y);//上
path.AddLine(rect.Left + rect.Width / - , rect.Y, rect.Left + rect.Width / , rect.Y - );//上
path.AddLine(rect.Left + rect.Width / , rect.Y - , rect.Left + rect.Width / + , rect.Y);//上
path.AddLine(rect.Left + rect.Width / + , rect.Y, rect.Right - cornerRadius * , rect.Y);//上
}
else
{
path.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * , rect.Y);//上
}
#endregion
path.AddArc(rect.X + rect.Width - cornerRadius * , rect.Y, cornerRadius * , cornerRadius * , , );//右上角
#region 右边
if (location == AnchorTipsLocation.LEFT)
{
path.AddLine(rect.Right, rect.Y + cornerRadius * , rect.Right, rect.Y + rect.Height / - );//右
path.AddLine(rect.Right, rect.Y + rect.Height / - , rect.Right + , rect.Y + rect.Height / );//右
path.AddLine(rect.Right + , rect.Y + rect.Height / , rect.Right, rect.Y + rect.Height / + );//右
path.AddLine(rect.Right, rect.Y + rect.Height / + , rect.Right, rect.Y + rect.Height - cornerRadius * );//右
}
else
{
path.AddLine(rect.Right, rect.Y + cornerRadius * , rect.Right, rect.Y + rect.Height - cornerRadius * );//右
}
#endregion
path.AddArc(rect.X + rect.Width - cornerRadius * , rect.Y + rect.Height - cornerRadius * , cornerRadius * , cornerRadius * , , );//右下角
#region 下边
if (location == AnchorTipsLocation.TOP)
{
path.AddLine(rect.Right - cornerRadius * , rect.Bottom, rect.Left + rect.Width / + , rect.Bottom);
path.AddLine(rect.Left + rect.Width / + , rect.Bottom, rect.Left + rect.Width / , rect.Bottom + );
path.AddLine(rect.Left + rect.Width / , rect.Bottom + , rect.Left + rect.Width / - , rect.Bottom);
path.AddLine(rect.Left + rect.Width / - , rect.Bottom, rect.X + cornerRadius * , rect.Bottom);
}
else
{
path.AddLine(rect.Right - cornerRadius * , rect.Bottom, rect.X + cornerRadius * , rect.Bottom);
}
#endregion
path.AddArc(rect.X, rect.Bottom - cornerRadius * , cornerRadius * , cornerRadius * , , );//左下角
#region 左边
if (location == AnchorTipsLocation.RIGHT)
{
path.AddLine(rect.Left, rect.Y + cornerRadius * , rect.Left, rect.Y + rect.Height / - );//左
path.AddLine(rect.Left, rect.Y + rect.Height / - , rect.Left - , rect.Y + rect.Height / );//左
path.AddLine(rect.Left - , rect.Y + rect.Height / , rect.Left, rect.Y + rect.Height / + );//左
path.AddLine(rect.Left, rect.Y + rect.Height / + , rect.Left, rect.Y + rect.Height - cornerRadius * );//左
}
else
{
path.AddLine(rect.X, rect.Bottom - cornerRadius * , rect.X, rect.Y + cornerRadius * );//左
}
#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(, ));
gBit.Dispose();
#endregion SetBits(bit);
if (autoCloseTime > )
{
Timer t = new Timer();
t.Interval = autoCloseTime;
t.Tick += (a, b) =>
{
this.Close();
};
t.Enabled = true;
}
}
#endregion
再来2个静态函数以供调用
#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 = ,
int autoCloseTime = )
{
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 = ,
int autoCloseTime = )
{
FrmAnchorTips frm = new FrmAnchorTips(rectControl, strMsg, location, background, foreColor, fontSize, autoCloseTime);
frm.TopMost = true;
frm.Show(parentForm);
return frm;
}
#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 = ,
int autoCloseTime = )
{
InitializeComponent();
Graphics g = this.CreateGraphics();
Font _font = new Font("微软雅黑", fontSize);
Color _background = background == null ? Color.FromArgb(, , ) : 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 + , (int)sizeText.Height + );
if (formSize.Width < )
formSize.Width = ;
if (formSize.Height < )
formSize.Height = ;
if (location == AnchorTipsLocation.LEFT || location == AnchorTipsLocation.RIGHT)
{
formSize.Width += ;
}
else
{
formSize.Height += ;
} #region 获取窗体path English:Get the form path
GraphicsPath path = new GraphicsPath();
Rectangle rect;
switch (location)
{
case AnchorTipsLocation.TOP:
rect = new Rectangle(, , formSize.Width - , formSize.Height - - );
this.Location = new Point(rectControl.X + (rectControl.Width - rect.Width) / , rectControl.Y - rect.Height - );
break;
case AnchorTipsLocation.RIGHT:
rect = new Rectangle(, , formSize.Width - - , formSize.Height - );
this.Location = new Point(rectControl.Right, rectControl.Y + (rectControl.Height - rect.Height) / );
break;
case AnchorTipsLocation.BOTTOM:
rect = new Rectangle(, , formSize.Width - , formSize.Height - - );
this.Location = new Point(rectControl.X + (rectControl.Width - rect.Width) / , rectControl.Bottom);
break;
default:
rect = new Rectangle(, , formSize.Width - - , formSize.Height - );
this.Location = new Point(rectControl.X - rect.Width - , rectControl.Y + (rectControl.Height - rect.Height) / );
break;
}
int cornerRadius = ; path.AddArc(rect.X, rect.Y, cornerRadius * , cornerRadius * , , );//左上角
#region 上边
if (location == AnchorTipsLocation.BOTTOM)
{
path.AddLine(rect.X + cornerRadius, rect.Y, rect.Left + rect.Width / - , rect.Y);//上
path.AddLine(rect.Left + rect.Width / - , rect.Y, rect.Left + rect.Width / , rect.Y - );//上
path.AddLine(rect.Left + rect.Width / , rect.Y - , rect.Left + rect.Width / + , rect.Y);//上
path.AddLine(rect.Left + rect.Width / + , rect.Y, rect.Right - cornerRadius * , rect.Y);//上
}
else
{
path.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * , rect.Y);//上
}
#endregion
path.AddArc(rect.X + rect.Width - cornerRadius * , rect.Y, cornerRadius * , cornerRadius * , , );//右上角
#region 右边
if (location == AnchorTipsLocation.LEFT)
{
path.AddLine(rect.Right, rect.Y + cornerRadius * , rect.Right, rect.Y + rect.Height / - );//右
path.AddLine(rect.Right, rect.Y + rect.Height / - , rect.Right + , rect.Y + rect.Height / );//右
path.AddLine(rect.Right + , rect.Y + rect.Height / , rect.Right, rect.Y + rect.Height / + );//右
path.AddLine(rect.Right, rect.Y + rect.Height / + , rect.Right, rect.Y + rect.Height - cornerRadius * );//右
}
else
{
path.AddLine(rect.Right, rect.Y + cornerRadius * , rect.Right, rect.Y + rect.Height - cornerRadius * );//右
}
#endregion
path.AddArc(rect.X + rect.Width - cornerRadius * , rect.Y + rect.Height - cornerRadius * , cornerRadius * , cornerRadius * , , );//右下角
#region 下边
if (location == AnchorTipsLocation.TOP)
{
path.AddLine(rect.Right - cornerRadius * , rect.Bottom, rect.Left + rect.Width / + , rect.Bottom);
path.AddLine(rect.Left + rect.Width / + , rect.Bottom, rect.Left + rect.Width / , rect.Bottom + );
path.AddLine(rect.Left + rect.Width / , rect.Bottom + , rect.Left + rect.Width / - , rect.Bottom);
path.AddLine(rect.Left + rect.Width / - , rect.Bottom, rect.X + cornerRadius * , rect.Bottom);
}
else
{
path.AddLine(rect.Right - cornerRadius * , rect.Bottom, rect.X + cornerRadius * , rect.Bottom);
}
#endregion
path.AddArc(rect.X, rect.Bottom - cornerRadius * , cornerRadius * , cornerRadius * , , );//左下角
#region 左边
if (location == AnchorTipsLocation.RIGHT)
{
path.AddLine(rect.Left, rect.Y + cornerRadius * , rect.Left, rect.Y + rect.Height / - );//左
path.AddLine(rect.Left, rect.Y + rect.Height / - , rect.Left - , rect.Y + rect.Height / );//左
path.AddLine(rect.Left - , rect.Y + rect.Height / , rect.Left, rect.Y + rect.Height / + );//左
path.AddLine(rect.Left, rect.Y + rect.Height / + , rect.Left, rect.Y + rect.Height - cornerRadius * );//左
}
else
{
path.AddLine(rect.X, rect.Bottom - cornerRadius * , rect.X, rect.Y + cornerRadius * );//左
}
#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(, ));
gBit.Dispose();
#endregion SetBits(bit);
if (autoCloseTime > )
{
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 = ,
int autoCloseTime = )
{
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 = ,
int autoCloseTime = )
{
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(, ); hBitmap = bitmap.GetHbitmap(Color.FromArgb());
oldBits = Win32.SelectObject(memDc, hBitmap); blendFunc.BlendOp = Win32.AC_SRC_OVER;
blendFunc.SourceConstantAlpha = ;
blendFunc.AlphaFormat = Win32.AC_SRC_ALPHA;
blendFunc.BlendFlags = ; Win32.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, , 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 = )]
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 = ;
public const Int32 ULW_ALPHA = ;
public const byte AC_SRC_ALPHA = ; [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(, );
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "FrmAnchorTips";
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.Text = "FrmAnchorTips";
this.ResumeLayout(false); } #endregion
}
}
最后的话
如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧
(五十一)c#Winform自定义控件-文字提示-HZHControls的更多相关文章
- (二十一)c#Winform自定义控件-气泡提示
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (三十五)c#Winform自定义控件-下拉框
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
- (二十五)c#Winform自定义控件-有确定取消的窗体(一)
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (四十五)c#Winform自定义控件-水波图表
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
- (八十一)c#Winform自定义控件-时间轴-HZHControls
官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...
- (六十五)c#Winform自定义控件-思维导图/组织架构图(工业)
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
- (八十)c#Winform自定义控件-分割线标签-HZHControls
官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...
- (五)c#Winform自定义控件-复选框
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (十五)c#Winform自定义控件-键盘(二)
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
随机推荐
- ssm整合——Mybatis配置(1)
mybatis搭建-基于注解 1. 环境准备 1.1 新建maven的webapp项目 1.2 新建必要的目录和文件 1.3 文件配置 pom.xml junit默认创建是4.11,手动改成4.12 ...
- Java中接口和抽象类的区别?
抽象类 抽象类必须用 abstract 修饰,子类必须实现抽象类中的抽象方法,如果有未实现的,那么子类也必须用 abstract 修饰.抽象类默认的权限修饰符为 public,可以定义为 public ...
- js对象属性的查询(点运算符和方括号运算符的区别)
js中可以通过点(.)和方括号([ ])运算符来获取属性的值.运算符的左侧应该是一个表达式,它返回一个对象.对于点(.)来说,右侧必须是一个以属性名称命名的简单标识符.对于方括号 ([ ])来说方括号 ...
- Web基础了解版04-XML-Tomcat-Http
XML 什么是XML - Tomcat - Http XML:eXtensible Markup Language (可扩展标记语言). XML 是一种标记语言,很类似 HTML. XML 的设计宗旨 ...
- 关于爬取babycenter.com A-Z为顺序的所有英文名及其详细属性
这一次爬取的内容已经在标题里提到了,下面是详细要求及其图示: 1.首先以A-Z的顺序获取所有英文名,最后爬取该英文名的详细信息. 2.CSV的header以3中的单词为准,请别拼错.如果没有对应的数 ...
- Redis 底层数据结构介绍
Redis 底层数据结构 版本:2.9 支持的数据类型: 字符串 散列 列表 集合 有序集合 字符串 Redis 利用原生的 c 字符串进行了一次封装.封装的字符串叫做简单动态字符串:SDS(simp ...
- JS 获取元素、修改元素/css样式/标签属性、简单事件、数据类型
基本使用 写在Script 标签里 引入外部js文件:<script src=" "></script> console.log(" " ...
- linux部署.net Core项目
首篇笔记,多多关照.方便回忆和给新手指导,大神绕道 首先在Linux系统部署.net Core项目首先准备一个Linux系统的服务器,百度云,阿里云都行. 1.net core 部署在Linux系统上 ...
- IT兄弟连 HTML5教程 CSS3属性特效 渐变2 线性渐变实例
3 线性渐变实例 一.颜色从顶部向底部渐变 制作从顶部到底部直线渐变有三种方法,第一种是起点参数不设置,因为起点参数的默认值为“top”:第二种方法起点参数设置为“top”:第三种起点参数使用“-90 ...
- IT兄弟连 HTML5教程 CSS3属性特效 弹性盒模型
CSS3引入了新的盒模型——弹性盒模型,该模型决定一个盒子在其他盒子中的分布方式以及如何处理可用的空间.使用该模型,可以很轻松的创建自适应浏览器窗口的流动布局或自适应字体大小的弹性布局.弹性盒模型看起 ...