0

I have a form with opacity set to 0. How would I be able to paint a filled rectangle inside that form, that is 50% transparent ?

//brush1 transparency is set at 128 (50%)
SolidBrush brush1 = new SolidBrush(Color.FromArgb(128, 100, 100, 100));

private void Form1_Paint(object sender, PaintEventArgs e)
{
    e.Grapics.FillRectangle(brush1, rectangle1);
}

If I paint using e.Graphics,nothing is drawn on the screen, since the form's opacity is 0.

I tried drawing using Graphics g = Graphics.FromHwnd(IntPtr.Zero); but it is so slow (only slow with brushes with transparency), that is absolutely ineffective.

Edit: I am doing this to draw to the screen. The form is being used a transparent canvas in order to achieve that. I tried using BackColor = Color.LightGreen; TransparencyKey = Color.LightGreen; but that just draws the rectangle LightGreen.

This is what I want to achieve:

1

dimitris93
  • 4,155
  • 11
  • 50
  • 86
  • @Sayka Can I please know how you would do this in WPF ? – dimitris93 Apr 18 '15 at 20:45
  • Oh what are you talking? In WPF every control has its own opacity margin and everything.. We can play a lot with transparencies.. – Abdul Saleem Apr 18 '15 at 20:47
  • Do you want to see an iOS like menu with blurred back i did using transparencies? – Abdul Saleem Apr 18 '15 at 20:48
  • @Sayka I guess it might help – dimitris93 Apr 18 '15 at 20:49
  • http://i.stack.imgur.com/CUthE.png – Abdul Saleem Apr 18 '15 at 20:55
  • You mean that transparent square is what you need? If so, why can't you make your form that small square sized? I'm really missing so much context to answer your question. I guess I'll have to delete my answer. – Sriram Sakthivel Apr 18 '15 at 20:55
  • And you want to place completely opaque things above it right? – Abdul Saleem Apr 18 '15 at 20:56
  • @SriramSakthivel as I said, using the code I have on my question, nothing is drawn on the screen, as you stated in your question, since opacity is set to 0. If I use `BackColor = Color.LightGreen; TransparencyKey = Color.LightGreen;` and Draw a filled rectangle wih a `128,100,100,100` brush (that is 50% transparent grey), i get [this](http://i.gyazo.com/26da64956b32079bab9bc3da9762c86f.png) – dimitris93 Apr 18 '15 at 20:57
  • You can achieve this by showing up two forms. One with Transparancy key and the other with half opacity – Abdul Saleem Apr 18 '15 at 21:01
  • @Sayka I guess this works here, but you basically suggest to have a form for every rectangle ? there has to be a better way to do this – dimitris93 Apr 18 '15 at 21:04
  • That you can wrap in a class and reuse. But first you should make it once.. You said you are ready to do whatever for the output. Let me add an answer. Wait.. – Abdul Saleem Apr 18 '15 at 21:06
  • And if it was WPF, all you have to do is to set the Alpha of the Background Brush to 0.25, add a border, set its border thickness to 1 or 1.5.. I also believed that WinForms can do anything. But b4 2 or 3 months, I migrated completely to WPF. Animations, styling, the XAML... ppa!! **MindBLOWING**. May the Winforms RIP – Abdul Saleem Apr 18 '15 at 21:31

1 Answers1

1

You can achieve this by using two forms. One with a partial opacity on the background and other with Transparency key on the foreground.

enter image description here

The transparent Window is in a Label kept on a form named Foreground_Form. And the background is a form named Form_TransparentBack

Form_TransparentBack

public partial class Form_TransparentBack : Form
{
    public Form_TransparentBack(Form _foregroundForm)
    {
        InitializeComponent();
        StartPosition = _foregroundForm.StartPosition;
        Location = _foregroundForm.Location;
        Size = _foregroundForm.Size;
        _foregroundForm.Resize += _foregroundForm_Resize;
        FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        _foregroundForm.LocationChanged += _foregroundForm_LocationChanged;
        ShowInTaskbar = false;
        BackColor = Color.WhiteSmoke;
        Opacity = 0.5;
        Timer timer = new Timer() { Interval = 10};
        timer.Tick += delegate(object sn, EventArgs ea)
        {
            (sn as Timer).Stop();
            _foregroundForm.ShowDialog();
        };
        timer.Start();
        Show();
    }

    void _foregroundForm_LocationChanged(object sender, EventArgs e)
    {
        Location = (sender as Form).Location;
    }

    void _foregroundForm_Resize(object sender, EventArgs e)
    {
        WindowState = (sender as Form).WindowState;
        Size = (sender as Form).Size;
    }
}

Foreground_Form

public partial class Foreground_Form : Form
{
    public Foreground_Form()
    {
        InitializeComponent();
        FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;  // required
        TransparencyKey = this.BackColor;  // required
        StartPosition = FormStartPosition.CenterScreen;
        this.Paint += Foreground_Form_Paint;
    }

    void Foreground_Form_Paint(object sender, PaintEventArgs e)
    {
        //this is for the Stroke
        e.Graphics.DrawRectangle(Pens.White, new Rectangle(0, 0, Width - 1, Height - 1));
    }
}

Now you can call any form with the transparent background. For the transparency, you should set the TransparencyKey to the form to be displayed.

private void button1_Click(object sender, EventArgs e)
{
    new Form_TransparentBack(new Foreground_Form());
}
Abdul Saleem
  • 10,098
  • 5
  • 45
  • 45
  • No, the whole program is one form. The purpose of this program is to simply take a screenshot. So, the program starts (nothing appears on the screen, other than the chursor changing) and the user clicks to a point A and drags to point B, releases the mouse, the "screenshot" gets saved and the program closes. I am trying to implement the "Gyazo" software if you have heard of it, its pretty popular – dimitris93 Apr 20 '15 at 01:34
  • [new question](http://stackoverflow.com/questions/29738090/change-cursor-to-cross-while-form-is-open-snippet-tool) – dimitris93 Apr 20 '15 at 01:49