C#悬浮窗口 图像背景

特点:
① 窗口一般较小,有时为不规则背景;
② 置顶显示;
③ 窗口支持拖动;
④ 一般用于程序状态显示,比如显示下载进度;
⑤ 一般支持右键菜单、拖拽操作等;

特点:
① 窗口一般较小,有时为不规则背景;
② 置顶显示;
③ 窗口支持拖动;
④ 一般用于程序状态显示,比如显示下载进度;
⑤ 一般支持右键菜单,拖拽操作等;

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);

                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();
        }
    }
}

Original: https://blog.csdn.net/weixin_42291376/article/details/128174879
Author: 钢铁男儿
Title: C#悬浮窗口 图像背景

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/724058/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球