Results 1 to 17 of 17

Thread: Gradient with Opacity?

Hybrid View

  1. #1

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Gradient with Opacity?

    In VS 2003 it is really easy to add the VbPowerPack and use BlendPanels to achieve gradients. However, BlendPanels are somewhat buggy when it comes to Docking and form Opacity. Am I correct in assuming that manually coding gradients on a form will not affect the forms ability to adjust in degrees of Opacity? This question is in regards to a project that I am working on in VS 2005.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  2. #2

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Gradient with Opacity?

    No thoughts? Anyway, I've created a sub that handles the gradient for each of the panels on a form. However, If there are other controls on the panel (Like labels) with a transparent background then they do not reflect the gradient. Can you see where I went wrong?

    Code:
    Private Sub uiMainForm_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) _
            Handles Me.Paint, BlendPanel1.Paint
    
            ' FILL BLENDPANEL1 GRADIENT
            FillLinearGradient(Me.BlendPanel1, 0, 0, Me.BlendPanel1.Width, Me.BlendPanel1.Height, _
                Color.FromKnownColor(KnownColor.Control), Color.FromKnownColor(KnownColor.ControlDark), LinearGradientMode.Vertical)
    
        End Sub
    
        Private Sub FillLinearGradient(ByRef sender As Panel, ByVal leftPos As Integer, ByVal topPos As Integer, _
            ByVal recWidth As Integer, ByVal recHeight As Integer, ByVal color1 As Color, _
            ByVal color2 As Color, ByVal direction As LinearGradientMode)
    
            Dim brushSize As New Rectangle(leftPos, topPos, recWidth, recHeight)
            Dim myGraphics As Graphics
            Dim myLinGradBrush As New LinearGradientBrush(brushSize, color1, color2, direction)
            Dim myBrush As Brush = myLinGradBrush
    
            ' SET WRAPMODE FOR LINEARGRADIENTBRUSH NOT CLAMP
            myLinGradBrush.WrapMode = WrapMode.Tile
    
            ' DRAW GRADIENT
            myGraphics = sender.CreateGraphics()
    
            ' FILL THE RECTANGLE
            myGraphics.FillRectangle(myBrush, leftPos, topPos, recWidth, recHeight)
    
        End Sub
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  3. #3
    Lively Member Shardox's Avatar
    Join Date
    Nov 2006
    Location
    Barcelona, Spain
    Posts
    123

    Smile Re: Gradient with Opacity?

    Hi Circuits2,

    Have you tried sending the graphics object to the sub? Something like this:


    VB Code:
    1. Imports System.Drawing
    2. Imports System.Drawing.Drawing2D
    3. Public Class Form1
    4.     Private Sub uiMainForm_Paint(ByVal sender As Object, _
    5.     ByVal e As System.Windows.Forms.PaintEventArgs) _
    6.     Handles Me.Paint
    7.  
    8.         MyGradient(e.Graphics)
    9.  
    10.     End Sub
    11.  
    12.     Private Sub MyGradient(ByVal gr As Graphics)
    13.  
    14.         ' Exit on size 0
    15.         If Me.ClientRectangle.Width = 0 Or _
    16.            Me.ClientRectangle.Height = 0 Then Exit Sub
    17.  
    18.         ' Declare variables
    19.         Dim zsRectangle As New Rectangle
    20.         zsRectangle = Me.ClientRectangle
    21.         Dim zsbrush As New LinearGradientBrush(zsRectangle, _
    22.          Color.FromKnownColor(KnownColor.Control), _
    23.          Color.FromKnownColor(KnownColor.ControlDark), _
    24.          LinearGradientMode.Horizontal)
    25.  
    26.         zsbrush.SetBlendTriangularShape(0.5)
    27.  
    28.         ' Use graphics to paint the gradient
    29.         gr.FillRectangle(zsbrush, zsRectangle)
    30.  
    31.         ' Clean  
    32.         zsbrush.Dispose()
    33.         zsbrush = Nothing
    34.     End Sub
    35. End Class

  4. #4

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Gradient with Opacity?

    Shardox, yours works in Horizontal mode, but not Vertical. I'm not sure why that is but I need it in Vertical. Also, why do labels show the correct background with yours and not mine?
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  5. #5

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Gradient with Opacity?

    Ok, I figured out why it doesn't work in Vertical and that is because you are using the entire ClientRectangle. I am sending it controls or objects that are relatively small, so I got around this by adding parameters to your Sub and passing the height and width of the sending object. I still do not understand why the labels perform differently though.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  6. #6

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Gradient with Opacity?

    Ok, I've got all the kinks worked out of the gradient now. But I am still not able to make a form Opaque. I have tried hard coding the opacity as well as setting the form property, but to no avail. Can anyone shed some light on this?
    Last edited by circuits2; Nov 16th, 2006 at 03:18 PM.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  7. #7

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Gradient with Opacity?

    Anyone?
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  8. #8
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: Gradient with Opacity?

    Quote Originally Posted by circuits2
    In VS 2003 it is really easy to add the VbPowerPack and use BlendPanels to achieve gradients. However, BlendPanels are somewhat buggy when it comes to Docking and form Opacity. Am I correct in assuming that manually coding gradients on a form will not affect the forms ability to adjust in degrees of Opacity? This question is in regards to a project that I am working on in VS 2005.
    Hi,

    You could use the opacity Mask to make it gradient, here's the link;

    http://msdn2.microsoft.com/en-us/library/ms743320.aspx

    Wkr,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  9. #9

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Gradient with Opacity?

    The opacity mask in that link is for images. Not really relevant for what I'm doing. All I want to achieve is fading a form in and out. I don't know why it is all of a sudden so complicated.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  10. #10
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Gradient with Opacity?

    not sure where you are getting held up circuits.. here is a screen shot of a form being painted with a horizontal linear gradient brush, that has a label on it (back color set to transparent) and opacity set (at design time) to 50%
    Attached Images Attached Images  

  11. #11

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Gradient with Opacity?

    Does it work the same with MDI Children?
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  12. #12
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Gradient with Opacity?

    you mean the gradient form is an MDI child and the MDI parent just just a normal form? or the other way around?

  13. #13

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Gradient with Opacity?

    Both contain gradients. I am able to get the MDI parent to fade, but haven't had any luck with the Children.


    Edit: Basically I am trying to create a fading transition effect from Child to Child. I.E. One Child fades out and another Child fades in.
    Last edited by circuits2; Nov 16th, 2006 at 04:07 PM.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  14. #14
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Gradient with Opacity?

    from MSDN

    The MDI child form you create in this step is a standard Windows Form. As such, it has an Opacity property, which enables you to control the transparency of the form. However, the Opacity property was designed for top-level windows. Do not use it with MDI child forms, as painting problems can occur.

    http://msdn2.microsoft.com/en-us/lib...76(VS.80).aspx

  15. #15

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Gradient with Opacity?

    That explains it! Thanks for finding that! What is a good transitional technique for MDI Children? The screen flash when closing and opening, then docking forms annoys me to no end. I tried the suspend layout technique but that still doesn't quite do it for me. I would like to implement something impressive. Thanks.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  16. #16
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Gradient with Opacity?

    maybe you could have 1 form that is docked, and is basically a container, and use the form in a form method to display whatever docked child forms you need.

    Or you could try a rollout form method.. where you use a loop/timer to shrink the form down to 0,0 in size and then roll out the new one.

    I guess it depends on how fancy you want to get. Maybe other members will have some other suggestions too??

  17. #17

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Gradient with Opacity?

    I think I might try a variation of the rollout method. If I get it to work like I want it to then I will post the code. I'm still open to suggestion if anyone has any other ideas. Thanks for clearing this up for me Klein.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width