昨天分享了一个环形滚动条控件,今天分享一个提示框风格的窗体。代码如下:

/// <summary>
/// 继承自Form,但将FormBorderStyle设置为None
/// </summary>
public partial class TipForm : Form
{
public TipForm()
{
InitializeComponent();
} /// <summary>
/// 鼠标按下位置,方便移动窗体
/// </summary>
private Point ptMouseDown; /// <summary>
/// 窗体下部分尖头的坐标位置
/// </summary>
private Point position;
public Point Position
{
get { return position; }
set
{
position = value;
SetPosition(position);
}
} /// <summary>
/// 设置窗口的圆角半径
/// </summary>
private int radius = ;
public int Radius
{
get { return radius; }
set { radius = value; }
} /// <summary>
/// 重写OnPaint方法以绘制窗体边框
/// </summary>
/// <param name="e"></param>
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics graph = e.Graphics;
graph.SmoothingMode = SmoothingMode.AntiAlias;
GraphicsPath path = GetTipPath(ClientRectangle);
Rectangle rect = new Rectangle(ClientRectangle.X, ClientRectangle.Y,
ClientRectangle.Width, ClientRectangle.Height);
rect.Inflate(-, -);
GraphicsPath border = GetTipPath(rect);
GraphicsPath curve = new GraphicsPath();
graph.DrawPath(new Pen(Color.Black,), border);
this.Region = new Region(path);
} /// <summary>
/// 根据窗体下部的顶点位置设置窗体位置
/// </summary>
/// <param name="pos">屏幕坐标点</param>
public void SetPosition(Point pos)
{
this.Location = new Point(pos.X - Size.Width / ,
pos.Y - Size.Height);
} /// <summary>
/// 根据当前窗体的ClientRectangle属性获取Tip风格路径
/// </summary>
/// <param name="rect"></param>
/// <returns></returns>
private GraphicsPath GetTipPath(Rectangle rect)
{
GraphicsPath path = new GraphicsPath();
int height = rect.Height - Radius;
path.StartFigure();
path.AddArc(rect.X, rect.Y, Radius, Radius, , -);
path.AddArc(rect.X, rect.Y + height - Radius, Radius, Radius,
, -);
path.AddLine(
new Point(rect.X + Radius / , rect.Y + height),
new Point(rect.X + rect.Width / - Radius / ,
rect.Y + height));
path.AddLine(
new Point(rect.X + rect.Width / - Radius / ,
rect.Y + height),
new Point(rect.X + rect.Width / , rect.Y + height + Radius));
path.AddLine(
new Point(rect.X + rect.Width / , rect.Y + height + Radius),
new Point(rect.X + rect.Width / + Radius / ,
rect.Y + height));
path.AddLine(
new Point(rect.X + rect.Width / + Radius / ,
rect.Y + height),
new Point(rect.X + rect.Width - Radius / ,
rect.Y + height));
path.AddArc(rect.X + rect.Width - radius,
rect.Y + height - Radius, Radius, Radius, , -);
path.AddArc(rect.X + rect.Width - Radius, rect.Y,
Radius, Radius, , -);
path.AddLine(new Point(rect.X + rect.Width - Radius / , rect.Y),
new Point(rect.X + Radius / , rect.Y)); path.CloseFigure();
return path;
} private void button1_Click(object sender, EventArgs e)
{
Close();
} /// <summary>
/// 鼠标移动事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TipForm_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Point pt = e.Location;
Location = new Point(Location.X + pt.X - ptMouseDown.X,
Location.Y + pt.Y - ptMouseDown.Y);
}
} /// <summary>
/// 鼠标按下事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TipForm_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ptMouseDown = e.Location;
}
} private void btnClose_Click(object sender, EventArgs e)
{
Close();
} private void TipForm_SizeChanged(object sender, EventArgs e)
{
Point pt = new Point(ClientRectangle.X + ClientRectangle.Width - Radius / - ,
ClientRectangle.Y + Radius / );
btnClose.Location = pt;
}
}

主要是通过创建一个表示窗体轮廓的路径,然后根据这个路径设置窗体的Region属性来完成的。下面是效果截图:

最新文章

  1. 轻量级表达式树解析框架Faller
  2. GitLab安装手记
  3. VM arguments
  4. 修改eclipse中settings.xml和默认资源库保存地址
  5. Linux下smokeping网络监控环境部署记录
  6. Java中的IO流系统详解(转载)
  7. Ubuntu中root用户和user用户的相互切换
  8. POJ 2531 Network Saboteur
  9. Parallel.Foreach的并发问题解决方法-比如爬虫WebClient
  10. meta的属性详解
  11. [LeetCode]题解(python):153-Find Minimum in Rotated Sorted Array
  12. 机器学习之支持向量机(二):SMO算法
  13. 【原创】大数据基础之Flink(1)简介、安装、使用
  14. 【原创】大叔经验分享(18)hive2.0以后通过beeline执行sql没有进度信息
  15. Docker操作笔记(三)数据管理
  16. Objective-C Core Animation深入理解
  17. 前端异步的一种方法库:axios
  18. Azure DevOps Server (TFS)中代码文件换行问题解决方案(Git)
  19. 使用w3m访问页面执行函数
  20. POJ 1386 Play on Words(欧拉路)

热门文章

  1. PHP 发送与接收流文件
  2. 一行代码从表中选取N行到另一个表
  3. E - 滑雪
  4. linux 下find命令 --查找文件名
  5. dpkg ---- apt-get ------ aptitude 三种方式的区别 及命令格式
  6. 多媒体视频(video)
  7. 关于HTML标签(元素)的那些事?
  8. 在Eclipse中配置Tomcat服务器
  9. HTML5实战与剖析之触摸事件(touchstart、touchmove和touchend)
  10. Qt之阴影边框(转)