Re: Gradient with Opacity?
No thoughts? :confused: 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
Re: Gradient with Opacity?
Hi Circuits2,
Have you tried sending the graphics object to the sub? Something like this:
VB Code:
Imports System.Drawing
Imports System.Drawing.Drawing2D
Public Class Form1
Private Sub uiMainForm_Paint(ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles Me.Paint
MyGradient(e.Graphics)
End Sub
Private Sub MyGradient(ByVal gr As Graphics)
' Exit on size 0
If Me.ClientRectangle.Width = 0 Or _
Me.ClientRectangle.Height = 0 Then Exit Sub
' Declare variables
Dim zsRectangle As New Rectangle
zsRectangle = Me.ClientRectangle
Dim zsbrush As New LinearGradientBrush(zsRectangle, _
Color.FromKnownColor(KnownColor.Control), _
Color.FromKnownColor(KnownColor.ControlDark), _
LinearGradientMode.Horizontal)
zsbrush.SetBlendTriangularShape(0.5)
' Use graphics to paint the gradient
gr.FillRectangle(zsbrush, zsRectangle)
' Clean
zsbrush.Dispose()
zsbrush = Nothing
End Sub
End Class
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?
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.
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?
Re: Gradient with Opacity?
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
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.
1 Attachment(s)
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%
Re: Gradient with Opacity?
Does it work the same with MDI Children?
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?
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.
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
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.
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??
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.