網站首頁 編程語言 正文
一 懸浮窗口
特點:
① 窗口一般較小,有時為不規則背景;
② 置頂顯示;
③ 窗口支持拖動;
④ 一般用于程序狀態顯示,比如顯示下載進度;
⑤ 一般支持右鍵菜單、拖拽操作等;
二 創建懸浮窗口
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
相關推薦
- 2022-10-10 pycharm創建并使用虛擬環境的詳細圖文教程_python
- 2022-07-18 Redis服務器連接本地Linux所踩的坑
- 2022-06-07 victoriaMetrics庫布隆過濾器初始化及使用詳解_Golang
- 2022-02-18 解決上傳apk文件后為vnd.android.package-archive格式的問題
- 2022-07-13 Android自定義View實現簡易畫板功能_Android
- 2022-10-15 C語言利用UDP實現群聊聊天室的示例代碼_C 語言
- 2023-05-07 Go項目配置管理神器之viper的介紹與使用詳解_Golang
- 2024-03-08 學習基于ssm框架前后端分離實現注冊登錄MD5加密的心得體會
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支