简体   繁体   中英

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 . 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%. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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