如何减少 True Transparency WinForm 更新时的闪烁?或者有更好的方法吗?

问题描述 投票:0回答:2

这一直是我面临的一个问题,我一直在尝试制作一个具有良好透明度的自定义绘制表单。

这是我现在所能达到的最接近的结果,我半小时前就开始了..

编辑: 我从头开始制作自定义表单设计和控件,就像我最新的 Sunilla https://i.stack.imgur.com/rqvDe.png。我想在它上面有一个很好的阴影,或者做另一个看起来有点像 windows areo 的设计。

它将 form.opacity 设置为 0%,然后每隔 500 毫秒抓取表单正后方的屏幕图像(屏幕上的任何内容、当前正在运行的程序、桌面等),并将其设置为背景和使透明度恢复到 100%。所以我可以在上面画任何东西,它看起来很完美!但我遇到的唯一问题是当它工作时,它会闪烁。是的,我尝试将 DoubleBuffering 设置为 true,没有区别。

听到主要代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TTTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            Opacity = 100;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = !timer1.Enabled;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

            Opacity = 0;
            Bitmap img = new Bitmap(this.Width, this.Height);
            Graphics gr = Graphics.FromImage(img);
            gr.CopyFromScreen(Location, Point.Empty, Size);
            this.BackgroundImage = img;
            Opacity = 100;
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.AntiAlias;

            ExtDrawing2D.FillRoundRect(g, new SolidBrush(Color.FromArgb(100, 255, 255, 255)), new RectangleF(1, 1, Width - 3, Height - 3), 4f);
            g.DrawPath(new Pen(Color.FromArgb(100, 0, 0, 0)), ExtDrawing2D.GetRoundedRect(new RectangleF(0, 0, Width - 1, Height - 1), 5f));
            g.DrawPath(new Pen(Color.FromArgb(100, 255,255,255)), ExtDrawing2D.GetRoundedRect(new RectangleF(1,1, Width - 3, Height - 3), 4f));
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer1_Tick(sender, e);
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.AntiAlias;

            ExtDrawing2D.FillRoundRect(g, new SolidBrush(Color.FromArgb(150, 255,255,255)), new RectangleF(1, 1, panel1.Width - 3, panel1.Height - 3), 2f);
            g.DrawPath(new Pen(Color.FromArgb(100, 0, 0, 0)), ExtDrawing2D.GetRoundedRect(new RectangleF(0, 0, panel1.Width - 1, panel1.Height - 1), 3f));
            g.DrawPath(new Pen(Color.FromArgb(100, 255, 255, 255)), ExtDrawing2D.GetRoundedRect(new RectangleF(1, 1, panel1.Width - 3, panel1.Height - 3), 2f));
        }
    }
}

注意: ExtDrawing2D 是一个单独的类,它完成了大量绘制近乎完美的圆角的繁重工作。这不是问题,我半年前开发了它,在我的所有项目中从未遇到过问题。

结果:

如果有更好的方法来解决这个整体问题,我很高兴听到它,尽管我已经在网上搜索了很长时间。

c# winforms custom-controls
2个回答
1
投票

我尝试了你的代码,因为我无法弄清楚你想要实现什么目标,是的,它闪烁了很多。但这是因为您将不透明度设置为 0,然后每半秒恢复为 1。如果看起来您正在尝试模拟透明窗口。为什么不直接使用透明窗口呢? IE。 FormBorderStyle=None 并将 BackColor 和 TransparencyKey 设置为相同的颜色(例如红色)。


0
投票

嗨 awhite92 你好,你找到闪烁的解决方案了吗,因为我也面临着同样的问题?

© www.soinside.com 2019 - 2024. All rights reserved.