Advertisement
tomjerry741

gradient background to the .NET windows forms or controls

Mar 31st, 2012
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. The following few lines of code adds cool gradient background to the .NET windows forms or controls.
  2. ------------------------------------------
  3. up the form add code
  4. ---------------------------------------
  5. 1. Imports System.Drawing.Drawing2D
  6. ------------------------------------------
  7. then
  8. 2. Add the following in New() function
  9. ------------------------------------------
  10. Public Sub New()
  11.  
  12. ' This call is required by the Windows Form Designer.
  13. InitializeComponent()
  14.  
  15. ' Add any initialization after the InitializeComponent() call.
  16.  
  17.  
  18. Me.SetStyle(System.Windows.Forms.ControlStyles.DoubleBuffer, True)
  19. Me.SetStyle(System.Windows.Forms.ControlStyles.AllPaintingInWmPaint, True)
  20. Me.SetStyle(System.Windows.Forms.ControlStyles.ResizeRedraw, True)
  21. Me.SetStyle(System.Windows.Forms.ControlStyles.UserPaint, True)
  22. End Sub
  23. 3. Add the following in Paint() function of Form
  24.  
  25. Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
  26. 'Fill Panel with gradient colors Red and Blue
  27. Dim rect As New System.Drawing.Rectangle(0, 0, e.ClipRectangle.Width, e.ClipRectangle.Height)
  28.  
  29. Dim g As Graphics = e.Graphics
  30.  
  31. Dim gradientBrush As New LinearGradientBrush(New Point(0, 0), New Point(Width, 0), System.Drawing.Color.Red, Color.Blue)
  32. g.FillRectangle(gradientBrush, rect)
  33.  
  34. End Sub
  35. -------------------------------------------------------
  36. what alse!!!
  37. -------------------------------------------------------
  38.  
  39. 1. We can do the same thing for all Form Controls by adding the above three lines in the Paint Event of the controls (E.x Panel )
  40.  
  41. The following Code snippet explains how to paint the panel with gradient background.
  42. -------------------------------------------------------
  43. -------------------------------------------------------
  44. add one panel or any item alse
  45. double click
  46. then , write this code
  47. -------------------------------------------------------
  48. wate alse
  49. for Ex : add panel or butoon then add this code below
  50. -------------------------------------------------------
  51.  
  52. Private Sub Panel1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
  53.  
  54. 'Fill Panel with gradient colors white and green
  55. Dim rect As New System.Drawing.Rectangle(0, 0, e.ClipRectangle.Width, e.ClipRectangle.Height)
  56.  
  57. Dim g As Graphics = e.Graphics
  58.  
  59. Dim gradientBrush As New LinearGradientBrush(New Point(0, 0), New Point(Width, 0), System.Drawing.Color.White, Color.Green)
  60. g.FillRectangle(gradientBrush, rect)
  61.  
  62. End Sub
  63. -------------------------------------------------------
  64. --------------------------------------------------------------------------------------------------------------
  65. -------------------------------------------------------
  66. thanx
  67. tom jerry
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement