網(wǎng)站首頁 編程語言 正文
一 懸浮窗口
特點:
① 窗口一般較小,有時為不規(guī)則背景;
② 置頂顯示;
③ 窗口支持拖動;
④ 一般用于程序狀態(tài)顯示,比如顯示下載進度;
⑤ 一般支持右鍵菜單、拖拽操作等;
二 創(chuàng)建懸浮窗口
1 實現(xiàn)細節(jié)
① 無邊框FormBorderStyle:Noe;
② 置頂顯示TopMost:true;
③ 顯示位置StartPosition:Manual自由指定;
2 細節(jié)
對應Form來說先Show,后設置大小和位置
floatBox=new myFloatBox();
floatBox.Owner=this;
floatBox.Show();
floatBox.Bounds=new Rectangle(0,0,100,100);
三 圓形背景
代碼實現(xiàn):
① 添加一個正方形的圖片資源;
② 繪制圓形圖片;
③ 將外圍白色區(qū)域設為透明;
④ 繪制一個蒙版,確保中間區(qū)域沒有白色像素點;
子窗體
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;//指定透明區(qū)域的顏色
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));
//設置剪輯區(qū)域
Region oldClip = g.Clip;
g.Clip = new Region(path);
//繪制圖像(Clip區(qū)域之外的部分不會顯示)
if(this.image!=null)
{
Console.WriteLine("圖像:" + image.Size);
Console.WriteLine("位置" + this.Size);
g.DrawImage(image, rect);
}
//覆蓋一層蒙版,確保純白色像素點不會出現(xiàn),因為純白色是透明色
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
相關(guān)推薦
- 2022-01-16 1.把字符串轉(zhuǎn)化為時間戳,再將時間戳轉(zhuǎn)化為Date對象 /** *@parame time = 2
- 2022-10-09 .NET使用Moq進行單元測試_實用技巧
- 2022-10-04 C語言實現(xiàn)倒置字符串的兩種方法分享_C 語言
- 2022-06-08 ASP.NET?Core中間件_基礎應用
- 2023-01-15 利用Python實現(xiàn)讀取Word文檔里的Excel附件_python
- 2023-10-15 前端console.log打印內(nèi)容與后端請求返回數(shù)據(jù)不一致
- 2022-06-22 C++深入探究類與對象之友元與運算符重載_C 語言
- 2022-09-12 Python中.py程序在CMD控制臺以指定虛擬環(huán)境運行_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支