日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C#實現懸浮窗口的方法詳解_C#教程

作者:鋼鐵男兒 ? 更新時間: 2023-01-23 編程語言

一 懸浮窗口

特點:

① 窗口一般較小,有時為不規則背景;

② 置頂顯示;

③ 窗口支持拖動;

④ 一般用于程序狀態顯示,比如顯示下載進度;

⑤ 一般支持右鍵菜單、拖拽操作等;

二 創建懸浮窗口

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

欄目分類
最近更新