一 悬浮窗口
特点:
① 窗口一般较小,有时为不规则背景;
② 置顶显示;
③ 窗口支持拖动;
④ 一般用于程序状态显示,比如显示下载进度;
⑤ 一般支持右键菜单、拖拽操作等;
二 创建悬浮窗口
特点:
① 窗口一般较小,有时为不规则背景;
② 置顶显示;
③ 窗口支持拖动;
④ 一般用于程序状态显示,比如显示下载进度;
⑤ 一般支持右键菜单,拖拽操作等;
1 实现细节:
① 无边框FormBorderStyle:Noe;
② 置顶显示TopMost:true;
③ 显示位置StartPosition:Manual自由指定;
2 细节
对应Form来说先Show,后设置大小和位置
floatBox=new myFloatBox();
floatBox.Owner=this;
floatBox.Show();
floatBox.Bounds=new Rectangle(0,0,100,100);
三 圆形背景
① 窗口一般较小,有时为不规则背景;
② 置顶显示;
③ 窗口支持拖动;
④ 一般用于程序状态显示,比如显示下载精度;
⑤ 一般支持右键菜单、拖拽操作等;
代码实现:
① 添加一个正方形的图片资源;
② 绘制圆形图片;
③ 将外围白色区域设为透明;
④ 绘制一个蒙版,确保中间区域没有白色像素点;
子窗体
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace 圆形背景
{public partial class FloatingWindow : Form{private Image image;public FloatingWindow(){this.FormBorderStyle = FormBorderStyle.None;//无边框this.StartPosition = FormStartPosition.Manual;//手工指定位置this.ShowInTaskbar = false;//在任务栏不显示图标this.TopMost = true;//置顶显示this.BackColor = Color.White;//背景色this.TransparencyKey = Color.White;//指定透明区域的颜色this.SetStyle(ControlStyles.UserPaint, true);this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);}protected override void OnPaint(PaintEventArgs e){base.OnPaint(e);Graphics g = e.Graphics;int w = this.Width, h = this.Height;Rectangle rect = new Rectangle(0, 0, w, h);rect.Inflate(-2, -2);//平滑绘制,反锯齿g.SmoothingMode = SmoothingMode.HighQuality;g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;//绘制一个圆形的图片if(true){GraphicsPath path = new GraphicsPath();int radius = rect.Width / 2;int x = w / 2 - radius;int y = h / 2 - radius;path.AddEllipse(new Rectangle(x, y, radius * 2, radius * 2));//设置剪辑区域Region oldClip = g.Clip;g.Clip = new Region(path);//绘制图像(Clip区域之外的部分不会显示)if(this.image!=null){Console.WriteLine("图像:" + image.Size);Console.WriteLine("位置" + this.Size);g.DrawImage(image, rect);}//覆盖一层蒙版,确保纯白色像素点不会出现,因为纯白色是透明色Brush brush = new SolidBrush(Color.FromArgb(10, 255, 255, 255));g.FillRectangle(brush, rect);brush.Dispose();g.Clip.Dispose();g.Clip = oldClip;//恢复Pen pen = new Pen(Color.LightBlue);g.DrawPath(pen, path);pen.Dispose();}}public Image Image{get { return this.image; }set { this.image = value;this.Invalidate();}}}
}
父窗体
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace 圆形背景
{public partial class Form1 : Form{FloatingWindow floatingWindow;public Form1(){InitializeComponent();floatingWindow=new FloatingWindow();floatingWindow.Show();floatingWindow.Bounds = new Rectangle(0, 0, 100, 100);//设置悬浮窗口的背景图片floatingWindow.Image = Properties.Resources.XiaoWu;}protected override void OnFormClosing(FormClosingEventArgs e){base.OnFormClosing(e);floatingWindow.Dispose();}}
}