Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to make the opacity of my form in .net lower but I don't want any contol to be affected. How can I lessen the opacity of the form without affecting any controls?
Posted
Comments
Andy Lanng 30-Jun-15 6:25am    
sorry for incorrect solution. I should have read your tags #^_^#

I don't know THE SOLUTION at the Moment.

You have to customize a Form and in this Form the Background-Painting must be handled seperatly (Overriding the OnPaint-method , handling the Opacity by yourself and not by the Form itself)
 
Share this answer
 
In WinForms, you can't do it easily - Opacity is a feature of the form, not the controls it contains.
You could do something that looks right, with two forms, one on top of the other. The top form contains the controls, and has a transparent background, is borderless, and so forth. The lower form has the opacity. Messy though.
 
Share this answer
 
Comments
Ralf Meier 30-Jun-15 6:40am    
@OG:
For me (and my opinion), your Solution is only a work-around (in Germany, we would say "eine Krücke").
Nevertheless, I could imagine that it will work - but is that good .Net-pactice ?
OriginalGriff 30-Jun-15 6:49am    
No - but neither is what the OP wants to do either! :laugh:
Ralf Meier 2-Jul-15 4:27am    
@OG:
I decided myself for either ... ;)
What do you think about my Solution (below) ?
Mark Cyrus Yncierto 30-Jun-15 6:57am    
I made this solution too, but like what you've said it became messy. And when I alt+tab I can see the form with less opacity. Thank for your concern
OriginalGriff 30-Jun-15 7:06am    
What are you actually trying to achieve? Is it worth the amount of time you are going to have to spend on this?
Because for me this isn't solved - here is the codee to my posted solution (2) :

VB
Imports System.ComponentModel

Public Class OpacForm
    Inherits System.Windows.Forms.Form

    Public Sub New()
        Me.SetStyle(ControlStyles.Opaque, True)
        Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, False)
        Me.SetStyle(ControlStyles.ResizeRedraw, True)

        MyBase.FormBorderStyle = Windows.Forms.FormBorderStyle.None
    End Sub

    <designerserializationvisibility(designerserializationvisibility.hidden)>
    <browsable(false),> _
    Shadows Property FormBorderStyle As FormBorderStyle



    <typeconverterattribute(gettype(opacityconverter))>
    Shadows Property Opacity As Double
        Get
            Return my_Opacity / 255
        End Get
        Set(ByVal value As Double)
            my_Opacity = value * 255
            Me.Refresh()
        End Set
    End Property

    Private my_Opacity As Integer = 255



    Protected Overrides Sub OnPaint(ByVal pe As System.Windows.Forms.PaintEventArgs)
        'MyBase.OnPaint(pe)

        Dim gr As System.Drawing.Graphics = pe.Graphics

        Dim myBrush As New SolidBrush(Color.FromArgb(my_Opacity, BackColor))

        gr.FillRectangle(myBrush, 0, 0, Width, Height)

    End Sub

End Class


This Form-Class must be inherited by the Form in which you want to use this Feature.
If you want to have any kind of Background it must also be created inside the OnPaint-method.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900