C#實(shí)現(xiàn)懸浮窗口的方法詳解
一 懸浮窗口
特點(diǎn):
① 窗口一般較小,有時(shí)為不規(guī)則背景;
② 置頂顯示;
③ 窗口支持拖動;
④ 一般用于程序狀態(tài)顯示,比如顯示下載進(jìn)度;
⑤ 一般支持右鍵菜單、拖拽操作等;
二 創(chuàng)建懸浮窗口
1 實(shí)現(xiàn)細(xì)節(jié)
① 無邊框FormBorderStyle:Noe;
② 置頂顯示TopMost:true;
③ 顯示位置StartPosition:Manual自由指定;
2 細(xì)節(jié)
對應(yīng)Form來說先Show,后設(shè)置大小和位置
floatBox=new myFloatBox(); floatBox.Owner=this; floatBox.Show(); floatBox.Bounds=new Rectangle(0,0,100,100);
三 圓形背景
代碼實(shí)現(xiàn):
① 添加一個(gè)正方形的圖片資源;
② 繪制圓形圖片;
③ 將外圍白色區(qū)域設(shè)為透明;
④ 繪制一個(gè)蒙版,確保中間區(qū)域沒有白色像素點(diǎn);
子窗體
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;//在任務(wù)欄不顯示圖標(biāo) 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; //繪制一個(gè)圓形的圖片 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)); //設(shè)置剪輯區(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); } //覆蓋一層蒙版,確保純白色像素點(diǎn)不會出現(xiàn),因?yàn)榧儼咨峭该魃? Brush brush = new SolidBrush(Color.FromArgb(10, 255, 255, 255)); g.FillRectangle(brush, rect); brush.Dispose(); g.Clip.Dispose(); g.Clip = oldClip;//恢復(fù) 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); //設(shè)置懸浮窗口的背景圖片 floatingWindow.Image = Properties.Resources.XiaoWu; } protected override void OnFormClosing(FormClosingEventArgs e) { base.OnFormClosing(e); floatingWindow.Dispose(); } } }
到此這篇關(guān)于C#實(shí)現(xiàn)懸浮窗口的方法詳解的文章就介紹到這了,更多相關(guān)C#懸浮窗口內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#打包應(yīng)用程序,與.NETFramework介紹
C#打包應(yīng)用程序,與.NETFramework介紹,需要的朋友可以參考一下2013-05-05C# 啟用事務(wù)提交多條帶參數(shù)的SQL語句實(shí)例代碼
這篇文章主要介紹了C# 啟用事務(wù)提交多條帶參數(shù)的SQL語句實(shí)例代碼,需要的朋友可以參考下2018-02-02WinForm使用正則表達(dá)式提取內(nèi)容的方法示例
這篇文章主要介紹了WinForm使用正則表達(dá)式提取內(nèi)容的方法,結(jié)合實(shí)例形式分析了WinForm基于正則匹配獲取指定內(nèi)容的相關(guān)操作技巧,需要的朋友可以參考下2017-05-05