6

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 https://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.

awhite92
  • 85
  • 5
  • Hmmm, double buffering usually solves my flickering problems. Is it possible that after InitializeComponent DoubleBuffered is somehow getting set back to false? – kmarks2 Jun 25 '12 at 20:12
  • Does any of the inestimable @HansPassant suggestions in his answer http://stackoverflow.com/questions/2612487/how-to-fix-the-flickering-in-user-controls help? It fixed a flickering issue with my form and background image. Silly question, but does just making the background of your main form transparent via Web Colors achieve something similar? – dash Jun 25 '12 at 20:14
  • @kmarks2 just tried adding it after the init() and its not the double buffering that's the problem, i think its that the form disappears for a split second. if there is a way i can get the screen from behind the form without having to "hide" it, it would stop the flickering. – awhite92 Jun 25 '12 at 20:15
  • @dash his solution wouldn't apply to this problem, i tried it anyways and no difference. and how would i go about doing that? my screenshot is the form on my desktop. i think ill update it to better explain whats needed. – awhite92 Jun 25 '12 at 20:23
  • @awhite92 There's no way to do a screen capture of what's behind something else directly. It just isn't possible because you can't pick and choose which z layers you want to capture, you can only modify what's in which z-layer (Hide) which flickers. Perhaps a more specific description of what _exactly_ you want to do would help. – kmarks2 Jun 25 '12 at 20:27
  • @kmarks2 i understand, so i guess this isn't exactly going to work for what i had originally planned, i guess it could still be used for a dock or a something of that sort. and I've tied to be as clear as possible lol but ill try again, 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. *fixed link* – awhite92 Jun 25 '12 at 20:41
  • @awhite92 Sorry I couldn't be more help. Btw the form/controls you linked to do look quite nice...reminds me of the default desktop environment of RH or CentOS except nicer. – kmarks2 Jun 25 '12 at 20:44
  • @kmarks2 thanks, maybe someone else mite be able to help me, hopefully lol. if not, i'll survive. – awhite92 Jun 25 '12 at 20:48

1 Answers1

1

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

Michael
  • 8,891
  • 3
  • 29
  • 42
  • yes thats what im trying to do lol.but it wont work correct. the reason is, because i want to draw anything on the form, like semi-transparent elements, and possibly a drop shadow. and you cant have anti-aliasing if you use the transparencyKey Method, everything ends up having part of the transparencyKey color in the semi-transparent parts. – awhite92 Jun 26 '12 at 02:00
  • and i actually use that TransparencyKey method for my other custom forms http://i.stack.imgur.com/rqvDe.png but your left with sharp corners and no shadow. (i hate the 'toolTip' shadow hack). – awhite92 Jun 26 '12 at 02:04
  • 1
    Have you tried looking at WPF. It is great for round borders, shadows, etc. – Michael Jun 26 '12 at 02:12
  • i will actually, I've been putting it off for some time now, i just had so much hope in WinForms. ill still try to get this to work, i only started on it today anyways. it looks as if WPF is my only hope. i wander what Microsoft uses for their Visual Studio 2012 RC, because it has a very custom, blue drop shadow.. – awhite92 Jun 26 '12 at 02:19