Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 671
awhite92 Avatar asked Jun 25 '12 19:06

awhite92


People also ask

How to avoid flickering in windows forms c#?

use control. DoubleBuffered property. set it to true. DoubleBuffered allows Control redraws its surface using secondary buffer to avoid flickering.

Which property of a control is used to reduce flickering when it is drawn?

Buffered graphics can reduce or eliminate flicker that is caused by progressive redrawing of parts of a displayed surface.

How do I turn on double buffering?

To enable double buffering, simply call the setDoubleBuffered() method (inherited from JComponent) to set the double-buffered property to true for any components that should use double-buffered drawing.


1 Answers

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

like image 159
Michael Avatar answered Oct 02 '22 22:10

Michael