简体   繁体   English

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

[英]How can i reduce flickering on updates to my True Transparency WinForm? or is there a better way to do this?

this has been an ongoing problem with me, ive been trying to make a custom drawn form with nice transparency. 这对我来说一直是个问题,我一直试图制作一个透明度好的自定义绘制表单。

this is as close as i can get right now, i started it just a half hour ago.. 这是我现在可以得到的尽可能接近,我在半小时前就开始了..

Edit: i make custom form designs and controls from scratch, like my latest, Sunilla http://i.stack.imgur.com/rqvDe.png . 编辑:我从头开始制作自定义表单设计和控件,就像我最新的Sunilla http://i.stack.imgur.com/rqvDe.png and i was wanting to have a good drop shadow on it, or make another design that looks kinda like windows areo. 我想要在它上面留下一个好的阴影,或者制作另一种看起来有点像窗户的设计。

it sets the form.opacity to 0% then it grabs an image of the screen ( anything on the screen, current running programs, desktop, etc) directly behind the form every 500 milliseconds as of now, and sets it as the background and brings the transparency back to 100%. 它将form.opacity设置为0%然后它每隔500毫秒直接抓取一个屏幕图像(屏幕上的任何内容,当前正在运行的程序,桌面等),并将其设置为背景并带来透明度回到100%。 so i can draw anything on it and it looks perfect! 所以我可以在上面画任何东西,它看起来很完美! but the only problem i get is when it does the work, it flickers. 但我得到的唯一问题是当它完成工作时,它会闪烁。 and yes i tried it with DoubleBuffering set to true, no difference. 是的,我尝试将DoubleBuffering设置为true,没有区别。

hears the main code: 听到主要代码:

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

note: the ExtDrawing2D is a separate class that does a lot of the heavy work with drawing almost perfect round corners. 注意: ExtDrawing2D是一个单独的类,通过绘制几乎完美的圆角来完成大量繁重的工作。 AND is not the problem, i developed it half a year ago and in all my projects never had problems with it. 并不是问题,我半年前开发了它,并且在我的所有项目中从未遇到过问题。

result: 结果:

if there is a better way of stabbing at this overall problem, id gladly love to hear it, altho i've been looking around the web for a long time. 如果有更好的方法来刺伤这个整体问题,我很高兴喜欢听到它,尽管我已经在网上浏览了很长时间。

I tried your code because I couldn't figure out what you were trying to achieve, and yes it flickers a lot. 我尝试了你的代码,因为我无法弄清楚你想要实现的目标,是的,它闪烁了很多。 But this is because you are setting the opacity to 0 and back to 1 every half a second. 但这是因为您将不透明度设置为0并且每半秒将其设置为1。 If looks like you are trying to simulate a transparent window. 如果看起来你正在尝试模拟透明窗口。 Why not just use a transparent window? 为什么不使用透明窗口? ie. 即。 FormBorderStyle=None and set BackColor and TransparencyKey to the same color (eg. Red). FormBorderStyle = None并将BackColor和TransparencyKey设置为相同的颜色(例如,红色)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM