C#实现悬浮窗口的方法详解
作者:钢铁男儿 时间:2022-06-15 12:29:29
一 悬浮窗口
特点:
① 窗口一般较小,有时为不规则背景;
② 置顶显示;
③ 窗口支持拖动;
④ 一般用于程序状态显示,比如显示下载进度;
⑤ 一般支持右键菜单、拖拽操作等;
二 创建悬浮窗口
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();
}
}
}
来源:https://blog.csdn.net/weixin_42291376/article/details/128174879
标签:C#,悬浮,窗口
0
投稿
猜你喜欢
springboot之Jpa通用接口及公共方法使用示例
2023-02-17 16:18:52
浅谈SpringCache与redis集成实现缓存解决方案
2022-10-12 01:11:17
详解Mybatis的二级缓存配置
2023-03-20 10:48:37
Springboot+AOP实现返回数据提示语国际化的示例代码
2021-08-18 19:49:12
详谈jvm--Java中init和clinit的区别
2022-01-10 10:35:22
OPENCV+JAVA实现人脸识别
2022-03-15 18:31:39
Java 中普通代码块,构造代码块,静态代码块区别及代码示例
2022-07-03 03:54:21
Java关于MyBatis缓存详解
2021-11-01 00:40:20
C# SynchronizationContext以及Send和Post使用解读
2023-10-16 04:27:28
详解Spring Bean 之间的特殊关系
2022-10-25 21:12:17
Java实现线程同步方法及原理详解
2021-07-29 21:28:13
Java集合TreeSet用法详解
2023-11-10 22:53:34
java编写ftp下载工具
2022-03-15 08:15:13
C#泛型类创建与使用的方法
2023-02-28 21:26:36
Mybatis 动态SQL的几种实现方法
2023-11-10 12:15:15
java的url方式、本地方式获取json文件内容
2023-08-22 18:30:23
详解C#中使用对象或集合的初始值设定项初始化的操作
2021-12-25 20:04:52
C#基于DBContext(EF)实现通用增删改查的REST方法实例
2021-06-23 20:56:07
用JAVA实现杨辉三角实例
2023-08-28 16:45:23
使用 Java8 实现观察者模式的方法(下)
2021-08-03 04:21:37