Results 1 to 5 of 5

Thread: [RESOLVED] VS2012 - Add Depth to Form Background

  1. #1

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

    Resolved [RESOLVED] VS2012 - Add Depth to Form Background

    I have an MDI application with controls situated on the outer edges of the Parent. Only one MdiChild is opened at a time and is set to DockStyle.Fill in the center of the Parent.

    I understand how gradients work when painting backgrounds, but the resulting display always appears to be flat and without character. I want to add depth to the Child forms so that their controls appear to be actual objects. I hope that is a clear description of my intent.

    How can I paint the background of a Child form to add depth without taking longer to paint than a standard gradient would?

    Thanks in advance for any comments or suggestions.
    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
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: VS2012 - Add Depth to Form Background

    Hi Circuits,

    Here's an example to illustrate a few possibilities in GDI+. Is it the kind of thing you have in mind? If not, an example or a sketch from you would help. I admit I've gone to town a bit especially with those radial lines.

    Name:  sunny.jpg
Views: 208
Size:  39.9 KB

    Here's the code I used to generate it:
    Code:
    Public Class Form1
    
    	Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    		Me.DoubleBuffered = True
    	End Sub
    
    	Private Sub Form1_Resize(sender As Object, e As System.EventArgs) Handles Me.Resize
    		Me.Invalidate()
    	End Sub
    
    	Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    		Dim gradientFocus As New PointF(100, 80)
    	
    		Using gp As New Drawing2D.GraphicsPath
    
    			'Draw radial gradient
    			Dim r As Rectangle = Me.ClientRectangle
    			r.Inflate(r.Width \ 2, r.Height \ 2)
    			gp.AddEllipse(r)
    			gp.Flatten(Nothing, 0.25)
    			Dim clrs As New List(Of Color)
    			For Each pp In gp.PathPoints
    				clrs.Add(Color.FromArgb(20, 0, 50))
    			Next
    			Using pgb As New Drawing2D.PathGradientBrush(gp)
    				pgb.CenterPoint = gradientFocus
    				pgb.CenterColor = Color.FromArgb(50, 150, 200)
    				pgb.SurroundColors = clrs.ToArray
    				e.Graphics.FillRectangle(pgb, Me.ClientRectangle)
    			End Using
    
    			'Draw radial lines
    			e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
    			For i As Integer = 0 To gp.PointCount - 2
    				Using pn As New Pen(Color.FromArgb(150, Color.Gold), 1)
    					e.Graphics.DrawLine(pn, gradientFocus, gp.PathPoints(i))
    				End Using
    			Next
    
    		End Using
    
    		'Draw drop shadows for all controls: 
    		For Each ctrl As Control In Me.Controls
    			Dim r As Rectangle = ctrl.Bounds
    			r.Offset(7, 10)
    			Using sbr As New SolidBrush(Color.FromArgb(80, Color.Black))
    				e.Graphics.FillRectangle(sbr, r)
    			End Using
    		Next
    
    	End Sub
    
    End Class
    More complicated code doesn't necessarily translate directly into slower paint times. Drawing those radial lines takes a little time but double buffering the form helps; try drag-resizing the form to see. The drop shadows are easy to code and quick to paint.

    BB

  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: VS2012 - Add Depth to Form Background

    Ooh, pretty! Those shadows are really effective and so simple! Why didn't I think of that? Just grab that for the code snippets then!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  4. #4

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

    Re: VS2012 - Add Depth to Form Background

    That's exactly what I was looking for boops! Radials and drop shadows give me the depth I wanted. Thank you!
    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
    Addicted Member MetalInquisitor's Avatar
    Join Date
    Sep 2012
    Posts
    143

    Thumbs up Re: VS2012 - Add Depth to Form Background

    Quote Originally Posted by boops boops View Post
    Hi Circuits,

    Here's an example to illustrate a few possibilities in GDI+. Is it the kind of thing you have in mind? If not, an example or a sketch from you would help. I admit I've gone to town a bit especially with those radial lines.

    Name:  sunny.jpg
Views: 208
Size:  39.9 KB

    Here's the code I used to generate it:
    Code:
    Public Class Form1
    
    	Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    		Me.DoubleBuffered = True
    	End Sub
    
    	Private Sub Form1_Resize(sender As Object, e As System.EventArgs) Handles Me.Resize
    		Me.Invalidate()
    	End Sub
    
    	Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    		Dim gradientFocus As New PointF(100, 80)
    	
    		Using gp As New Drawing2D.GraphicsPath
    
    			'Draw radial gradient
    			Dim r As Rectangle = Me.ClientRectangle
    			r.Inflate(r.Width \ 2, r.Height \ 2)
    			gp.AddEllipse(r)
    			gp.Flatten(Nothing, 0.25)
    			Dim clrs As New List(Of Color)
    			For Each pp In gp.PathPoints
    				clrs.Add(Color.FromArgb(20, 0, 50))
    			Next
    			Using pgb As New Drawing2D.PathGradientBrush(gp)
    				pgb.CenterPoint = gradientFocus
    				pgb.CenterColor = Color.FromArgb(50, 150, 200)
    				pgb.SurroundColors = clrs.ToArray
    				e.Graphics.FillRectangle(pgb, Me.ClientRectangle)
    			End Using
    
    			'Draw radial lines
    			e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
    			For i As Integer = 0 To gp.PointCount - 2
    				Using pn As New Pen(Color.FromArgb(150, Color.Gold), 1)
    					e.Graphics.DrawLine(pn, gradientFocus, gp.PathPoints(i))
    				End Using
    			Next
    
    		End Using
    
    		'Draw drop shadows for all controls: 
    		For Each ctrl As Control In Me.Controls
    			Dim r As Rectangle = ctrl.Bounds
    			r.Offset(7, 10)
    			Using sbr As New SolidBrush(Color.FromArgb(80, Color.Black))
    				e.Graphics.FillRectangle(sbr, r)
    			End Using
    		Next
    
    	End Sub
    
    End Class
    More complicated code doesn't necessarily translate directly into slower paint times. Drawing those radial lines takes a little time but double buffering the form helps; try drag-resizing the form to see. The drop shadows are easy to code and quick to paint.

    BB
    Well, this is really cool! I'll be spending a couple of hours having fun tweaking your code.

    Kudos boops!

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