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

awh*_*e92 6 c# custom-controls winforms

这对我来说一直是个问题,我一直试图制作一个透明度好的自定义绘制表单.

这是我现在可以得到的尽可能接近,我在半小时前就开始了..

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

它将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));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

结果:

如果有更好的方法来刺伤这个整体问题,我很高兴喜欢听到它,尽管我已经在网上浏览了很长时间.

Mic*_*ael 1

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