Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. How do I get a splash screen to transist from one color to another?

How do I get a splash screen to transist from one color to another?

Scheduled Pinned Locked Moved Visual Basic
learningquestioncsharpcom
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tyrone_whitey
    wrote on last edited by
    #1

    The form is 510 by 360. One half of it has to go from black to green. The other Half from blue to black. I think you're supposed to create a loop of some sort to change each line using the pen. But How?? Remember I am only a beginner so, i know only the basics of VB.net. E-mail me at tate_thomasster@gmail.com and i can give you the source code to see if you can point out my mistakes. If you are familar with the Thomson Course Technology book, Visual Basic.Net: An object-oriented approach, then it is Chapter 6, Exercise 2. This is my first time working with Vb.Net. Ty

    J 1 Reply Last reply
    0
    • T Tyrone_whitey

      The form is 510 by 360. One half of it has to go from black to green. The other Half from blue to black. I think you're supposed to create a loop of some sort to change each line using the pen. But How?? Remember I am only a beginner so, i know only the basics of VB.net. E-mail me at tate_thomasster@gmail.com and i can give you the source code to see if you can point out my mistakes. If you are familar with the Thomson Course Technology book, Visual Basic.Net: An object-oriented approach, then it is Chapter 6, Exercise 2. This is my first time working with Vb.Net. Ty

      J Offline
      J Offline
      Joshua Quick
      wrote on last edited by
      #2

      Okay. From the e-mail you sent me, you want to split your form into 2 triangles. In the top left corner, you want a triangle with a color gradient from black to green. In the bottom right corner, you want a triangle with a color gradient blue to black. What you need is a GradientBrush to draw this for you. You'll also need to catch your form's Paint event so that you can draw these triangle onto your form. The drawing is done on the Paint method's passed in Graphics object. The code should look something like this.

      Private Sub MyForm_Paint(ByVal sender As Object, _
      ByVal e As System.Windows.Forms.PaintEventArgs) _
      Handles MyBase.Paint
      Dim brGradient As System.Drawing.Drawing2D.LinearGradientBrush
      Dim points() As PointF
      '
      ' Validate.
      If (e Is Nothing) Then Return
      If (e.Graphics Is Nothing) Then Return
      '
      ' Get form's client size.
      Dim clientRectangle As New System.Drawing.Rectangle(0, 0, _
      Me.ClientSize.Width, Me.ClientSize.Height)
      '
      ' Draw the green gradient triangle.
      brGradient = New System.Drawing.Drawing2D.LinearGradientBrush(clientRectangle, _
      Color.Black, Color.LightGreen, _
      System.Drawing.Drawing2D.LinearGradientMode.Horizontal)
      points = New PointF() {New PointF(0, 0), _
      New PointF(clientRectangle.Width, 0), _
      New PointF(0, clientRectangle.Height)}
      e.Graphics.FillPolygon(brGradient, points)
      brGradient.Dispose()
      '
      ' Draw the blue gradient triangle.
      brGradient = New System.Drawing.Drawing2D.LinearGradientBrush(clientRectangle, _
      Color.Blue, Color.Black, _
      System.Drawing.Drawing2D.LinearGradientMode.Horizontal)
      points = New PointF() {New PointF(0, clientRectangle.Height), _
      New PointF(clientRectangle.Width, 0), _
      New PointF(clientRectangle.Width, clientRectangle.Height)}
      e.Graphics.FillPolygon(brGradient, points)
      brGradient.Dispose()
      End Sub

      T 1 Reply Last reply
      0
      • J Joshua Quick

        Okay. From the e-mail you sent me, you want to split your form into 2 triangles. In the top left corner, you want a triangle with a color gradient from black to green. In the bottom right corner, you want a triangle with a color gradient blue to black. What you need is a GradientBrush to draw this for you. You'll also need to catch your form's Paint event so that you can draw these triangle onto your form. The drawing is done on the Paint method's passed in Graphics object. The code should look something like this.

        Private Sub MyForm_Paint(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.PaintEventArgs) _
        Handles MyBase.Paint
        Dim brGradient As System.Drawing.Drawing2D.LinearGradientBrush
        Dim points() As PointF
        '
        ' Validate.
        If (e Is Nothing) Then Return
        If (e.Graphics Is Nothing) Then Return
        '
        ' Get form's client size.
        Dim clientRectangle As New System.Drawing.Rectangle(0, 0, _
        Me.ClientSize.Width, Me.ClientSize.Height)
        '
        ' Draw the green gradient triangle.
        brGradient = New System.Drawing.Drawing2D.LinearGradientBrush(clientRectangle, _
        Color.Black, Color.LightGreen, _
        System.Drawing.Drawing2D.LinearGradientMode.Horizontal)
        points = New PointF() {New PointF(0, 0), _
        New PointF(clientRectangle.Width, 0), _
        New PointF(0, clientRectangle.Height)}
        e.Graphics.FillPolygon(brGradient, points)
        brGradient.Dispose()
        '
        ' Draw the blue gradient triangle.
        brGradient = New System.Drawing.Drawing2D.LinearGradientBrush(clientRectangle, _
        Color.Blue, Color.Black, _
        System.Drawing.Drawing2D.LinearGradientMode.Horizontal)
        points = New PointF() {New PointF(0, clientRectangle.Height), _
        New PointF(clientRectangle.Width, 0), _
        New PointF(clientRectangle.Width, clientRectangle.Height)}
        e.Graphics.FillPolygon(brGradient, points)
        brGradient.Dispose()
        End Sub

        T Offline
        T Offline
        Tyrone_whitey
        wrote on last edited by
        #3

        Okay so I sort of understand. But the way I was trying to do it was create a line that draws the color in a loop repeatedly until it changes to the other color. Like creating three different varaibles and incremeting the green variable from 0 to 255. Is that a good way of doing it?? Here's some of the code(I haven't quite yet created the loop)... Private Sub frmSplash_Paint(ByVal sender As Object, ByVal e As _ System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint Dim graCurrent As Graphics = e.Graphics Dim recCurrent As Rectangle, colCurrent As Color Dim sbCurrent As SolidBrush, penCurrent As Pen Dim intHeight As Integer, intWidth As Integer recCurrent = New Rectangle(0, 0, Me.Width, Me.Height) sbCurrent = New SolidBrush(Color.Green) graCurrent.FillRectangle(sbCurrent, recCurrent) Dim CounterLoop As Integer = 1 intHeight = 0 For intwidth = 0 To Me.Width Step _ ToInt32(Me.Width / 255) penCurrent = New Pen(Color.FromArgb(0, 0, 0, 0)) Select Case CounterLoop Case 1 colCurrent = Color.FromArgb(0, 0, 0, 0) CounterLoop += 1 Case 2 colCurrent = Color.FromArgb(0, 0, 1, 0) CounterLoop += 1 Case 3 colCurrent = Color.FromArgb(0, 0, 2, 0) CounterLoop += 1 Case 4 colCurrent = Color.FromArgb(0, 0, 3, 0) CounterLoop += 1 Case 5 colCurrent = Color.FromArgb(0, 0, 4, 0) CounterLoop += 1 Case 6 colCurrent = Color.FromArgb(0, 0, 5, 0) CounterLoop += 1 Case 7 colCurrent = Color.FromArgb(0, 0, 6, 0) CounterLoop += 1 Case 8 colCurrent = Color.FromArgb(0, 0, 7, 0) CounterLoop += 1 Case 9 colCurrent = Color.FromArgb(0, 0, 8, 0) CounterLoop += 1 Case 10 colCurrent = Color.FromArgb(0, 0, 10, 0) CounterLoop += 1 End Select graCurrent.DrawLine(penCurrent, 0, ToInt32(Me.Height / 180), _ ToInt32(Me.Width / 255), 0) Next End Sub End Class Ty -- modified at 10:04

        J 2 Replies Last reply
        0
        • T Tyrone_whitey

          Okay so I sort of understand. But the way I was trying to do it was create a line that draws the color in a loop repeatedly until it changes to the other color. Like creating three different varaibles and incremeting the green variable from 0 to 255. Is that a good way of doing it?? Here's some of the code(I haven't quite yet created the loop)... Private Sub frmSplash_Paint(ByVal sender As Object, ByVal e As _ System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint Dim graCurrent As Graphics = e.Graphics Dim recCurrent As Rectangle, colCurrent As Color Dim sbCurrent As SolidBrush, penCurrent As Pen Dim intHeight As Integer, intWidth As Integer recCurrent = New Rectangle(0, 0, Me.Width, Me.Height) sbCurrent = New SolidBrush(Color.Green) graCurrent.FillRectangle(sbCurrent, recCurrent) Dim CounterLoop As Integer = 1 intHeight = 0 For intwidth = 0 To Me.Width Step _ ToInt32(Me.Width / 255) penCurrent = New Pen(Color.FromArgb(0, 0, 0, 0)) Select Case CounterLoop Case 1 colCurrent = Color.FromArgb(0, 0, 0, 0) CounterLoop += 1 Case 2 colCurrent = Color.FromArgb(0, 0, 1, 0) CounterLoop += 1 Case 3 colCurrent = Color.FromArgb(0, 0, 2, 0) CounterLoop += 1 Case 4 colCurrent = Color.FromArgb(0, 0, 3, 0) CounterLoop += 1 Case 5 colCurrent = Color.FromArgb(0, 0, 4, 0) CounterLoop += 1 Case 6 colCurrent = Color.FromArgb(0, 0, 5, 0) CounterLoop += 1 Case 7 colCurrent = Color.FromArgb(0, 0, 6, 0) CounterLoop += 1 Case 8 colCurrent = Color.FromArgb(0, 0, 7, 0) CounterLoop += 1 Case 9 colCurrent = Color.FromArgb(0, 0, 8, 0) CounterLoop += 1 Case 10 colCurrent = Color.FromArgb(0, 0, 10, 0) CounterLoop += 1 End Select graCurrent.DrawLine(penCurrent, 0, ToInt32(Me.Height / 180), _ ToInt32(Me.Width / 255), 0) Next End Sub End Class Ty -- modified at 10:04

          J Offline
          J Offline
          Joshua Quick
          wrote on last edited by
          #4

          You can draw the gradient yourself with individual lines. It just takes more effort (and might not be as efficient). GDI+ can do the work for you with a single Gradient brush, like in the code I gave you. Your code isn't going to work though. For one thing, you're assigning a new Color to "colCurrent" on every pass in the loop, but you never assigned the color to your pen. You should be creating the pen with this color at the bottom of your switch statement. penCurrent = New Pen(colCurrent) Also, you really shouldn't be using a switch statement. There are a lot of colors for you to go through, which would involve too many case statements. Instead, you should create red, green, and blue integer variables and increment the ones you need on every pass. Luckily you're transitioning from black to another color, which involves just incrementing up from (0,0,0). If your starting color was not black, then creating a gradient would be much, much harder. Also, you need to call your brush's and pen's Dispose() method when you're done using them. Otherwise you'll have GDI leaks. As a general rule, any object that you've created should be disposed if it has a Dispose() method. That means disposing the pen before overwriting it with a new one on every pass in the loop.

          1 Reply Last reply
          0
          • T Tyrone_whitey

            Okay so I sort of understand. But the way I was trying to do it was create a line that draws the color in a loop repeatedly until it changes to the other color. Like creating three different varaibles and incremeting the green variable from 0 to 255. Is that a good way of doing it?? Here's some of the code(I haven't quite yet created the loop)... Private Sub frmSplash_Paint(ByVal sender As Object, ByVal e As _ System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint Dim graCurrent As Graphics = e.Graphics Dim recCurrent As Rectangle, colCurrent As Color Dim sbCurrent As SolidBrush, penCurrent As Pen Dim intHeight As Integer, intWidth As Integer recCurrent = New Rectangle(0, 0, Me.Width, Me.Height) sbCurrent = New SolidBrush(Color.Green) graCurrent.FillRectangle(sbCurrent, recCurrent) Dim CounterLoop As Integer = 1 intHeight = 0 For intwidth = 0 To Me.Width Step _ ToInt32(Me.Width / 255) penCurrent = New Pen(Color.FromArgb(0, 0, 0, 0)) Select Case CounterLoop Case 1 colCurrent = Color.FromArgb(0, 0, 0, 0) CounterLoop += 1 Case 2 colCurrent = Color.FromArgb(0, 0, 1, 0) CounterLoop += 1 Case 3 colCurrent = Color.FromArgb(0, 0, 2, 0) CounterLoop += 1 Case 4 colCurrent = Color.FromArgb(0, 0, 3, 0) CounterLoop += 1 Case 5 colCurrent = Color.FromArgb(0, 0, 4, 0) CounterLoop += 1 Case 6 colCurrent = Color.FromArgb(0, 0, 5, 0) CounterLoop += 1 Case 7 colCurrent = Color.FromArgb(0, 0, 6, 0) CounterLoop += 1 Case 8 colCurrent = Color.FromArgb(0, 0, 7, 0) CounterLoop += 1 Case 9 colCurrent = Color.FromArgb(0, 0, 8, 0) CounterLoop += 1 Case 10 colCurrent = Color.FromArgb(0, 0, 10, 0) CounterLoop += 1 End Select graCurrent.DrawLine(penCurrent, 0, ToInt32(Me.Height / 180), _ ToInt32(Me.Width / 255), 0) Next End Sub End Class Ty -- modified at 10:04

            J Offline
            J Offline
            Joshua Quick
            wrote on last edited by
            #5

            Here's a follow up to drawing a gradient with individual lines... First you need to figure out how to calculate the gradient. The next pen color that you calculate within your loop is determined based on your x position in the form. For the first triangle, we should convert those x coordinates into green color percentages, where the left most x on the form is 0% (black) and the right most x on the form is 100% (green). Dim percentGreen As Double = x / Me.ClientSize.Width Next, we convert that into a color value which ranges between 0 and 255. Dim greenValue As Integer = percentGreen * 255 Simple math so far. Now since we're drawing a triangle, we're going to need to use trigonometry to calculate the y. We're going to iterate the x from left to right, and draw a line top to bottom, but that bottom is going to get shorter and shorter on every pass of the loop. The width of the triangle from x to the right most point will be... Dim width As Double = Me.ClientSize.Width - x We need to calculate the angle of the right most point on the triangle. This is a constant value. You only need to calculate this once. ' Tangent(Angle) = Opposite / Adjacent ' Angle = ArcTangent(Opposite / Adjacent) ' This angle is in radians! Dim angle As Double = Math.Atan(Me.ClientSize.Height / x) Now that you have the width and angle, you can calculate the height. ' Opposite is the y, which the part we want. ' Tangent(Angle) = Opposite / Adjacent ' Opposite = Tangent(Angle) / Adjacent Dim y As Double = Math.Tan(angle) / x So, all in all, it should look something like this. I'll leave the rest to you.

            ' This is the loop for the green triangle.
            For x As Integer = 0 To (Me.ClientSize.Width - 1) Step 1
            ' Calculate green value here.
            ' Create pen here using calculated color value.
            ' Calculate x and y here.
            ' Draw line here via e.Graphics.DrawLine().
            ' Dispose pen here.
            Next

            T 1 Reply Last reply
            0
            • J Joshua Quick

              Here's a follow up to drawing a gradient with individual lines... First you need to figure out how to calculate the gradient. The next pen color that you calculate within your loop is determined based on your x position in the form. For the first triangle, we should convert those x coordinates into green color percentages, where the left most x on the form is 0% (black) and the right most x on the form is 100% (green). Dim percentGreen As Double = x / Me.ClientSize.Width Next, we convert that into a color value which ranges between 0 and 255. Dim greenValue As Integer = percentGreen * 255 Simple math so far. Now since we're drawing a triangle, we're going to need to use trigonometry to calculate the y. We're going to iterate the x from left to right, and draw a line top to bottom, but that bottom is going to get shorter and shorter on every pass of the loop. The width of the triangle from x to the right most point will be... Dim width As Double = Me.ClientSize.Width - x We need to calculate the angle of the right most point on the triangle. This is a constant value. You only need to calculate this once. ' Tangent(Angle) = Opposite / Adjacent ' Angle = ArcTangent(Opposite / Adjacent) ' This angle is in radians! Dim angle As Double = Math.Atan(Me.ClientSize.Height / x) Now that you have the width and angle, you can calculate the height. ' Opposite is the y, which the part we want. ' Tangent(Angle) = Opposite / Adjacent ' Opposite = Tangent(Angle) / Adjacent Dim y As Double = Math.Tan(angle) / x So, all in all, it should look something like this. I'll leave the rest to you.

              ' This is the loop for the green triangle.
              For x As Integer = 0 To (Me.ClientSize.Width - 1) Step 1
              ' Calculate green value here.
              ' Create pen here using calculated color value.
              ' Calculate x and y here.
              ' Draw line here via e.Graphics.DrawLine().
              ' Dispose pen here.
              Next

              T Offline
              T Offline
              Tyrone_whitey
              wrote on last edited by
              #6

              Thank You for helping me. And a last request if you don't mind. Here is the final code to the splash screen. Just look over it and see if i got the general idea right. Private Sub frmSplash_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint Dim graCurrent As Graphics = e.Graphics Dim colCurrent As Color, colCurrent1 As Color Dim penCurrent As Pen, penCurrent1 As Pen Dim LineLoop As Integer, LineLoop1 As Integer 'Sets the first coordiante for the green triangle. Dim Greenx1 As Integer = 0 Dim Greenx2 As Integer = 0 Dim Greeny1 As Integer = 0 Dim Greeny2 As Integer = Me.Height 'Sets the first coordiante for the blue triangle. Dim Bluex1 As Integer = Me.Width Dim Bluex2 As Integer = 0 Dim Bluey1 As Integer = Me.Height Dim Bluey2 As Integer = 0 ' Sets the "step value" or how far up and over for each line created. Dim StepX As Integer = ToInt32(Me.Width / 255) Dim StepY As Integer = ToInt32(Me.Height / 255) ' Darwing of the Green Triangle For LineLoop = 0 To 255 colCurrent = Color.FromArgb(0, LineLoop, 0) penCurrent = New Pen(colCurrent, 4) graCurrent.DrawLine(penCurrent, Greenx1, Greeny1, Greenx2, Greeny2) Greenx1 = Greenx1 + ToInt32(StepX) Greeny1 = Greeny1 + ToInt32(StepY) Greenx1 += 1 Greeny1 += 1 Greeny2 += 1 Next LineLoop ' Drawing of the Blue Triangle For LineLoop1 = 255 To 0 Step -1 colCurrent1 = Color.FromArgb(0, 0, LineLoop1) penCurrent1 = New Pen(colCurrent1, 4) graCurrent.DrawLine(penCurrent1, Bluex1, Bluey1, Bluex2, Bluey2) Bluex1 = Bluex1 + ToInt32(StepX) Bluey1 = Bluey1 + ToInt32(StepY) Bluey1 -= 1 Bluey2 -= 1 Bluex2 += 1 Next LineLoop1 End Sub :) Ty

              J 1 Reply Last reply
              0
              • T Tyrone_whitey

                Thank You for helping me. And a last request if you don't mind. Here is the final code to the splash screen. Just look over it and see if i got the general idea right. Private Sub frmSplash_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint Dim graCurrent As Graphics = e.Graphics Dim colCurrent As Color, colCurrent1 As Color Dim penCurrent As Pen, penCurrent1 As Pen Dim LineLoop As Integer, LineLoop1 As Integer 'Sets the first coordiante for the green triangle. Dim Greenx1 As Integer = 0 Dim Greenx2 As Integer = 0 Dim Greeny1 As Integer = 0 Dim Greeny2 As Integer = Me.Height 'Sets the first coordiante for the blue triangle. Dim Bluex1 As Integer = Me.Width Dim Bluex2 As Integer = 0 Dim Bluey1 As Integer = Me.Height Dim Bluey2 As Integer = 0 ' Sets the "step value" or how far up and over for each line created. Dim StepX As Integer = ToInt32(Me.Width / 255) Dim StepY As Integer = ToInt32(Me.Height / 255) ' Darwing of the Green Triangle For LineLoop = 0 To 255 colCurrent = Color.FromArgb(0, LineLoop, 0) penCurrent = New Pen(colCurrent, 4) graCurrent.DrawLine(penCurrent, Greenx1, Greeny1, Greenx2, Greeny2) Greenx1 = Greenx1 + ToInt32(StepX) Greeny1 = Greeny1 + ToInt32(StepY) Greenx1 += 1 Greeny1 += 1 Greeny2 += 1 Next LineLoop ' Drawing of the Blue Triangle For LineLoop1 = 255 To 0 Step -1 colCurrent1 = Color.FromArgb(0, 0, LineLoop1) penCurrent1 = New Pen(colCurrent1, 4) graCurrent.DrawLine(penCurrent1, Bluex1, Bluey1, Bluex2, Bluey2) Bluex1 = Bluex1 + ToInt32(StepX) Bluey1 = Bluey1 + ToInt32(StepY) Bluey1 -= 1 Bluey2 -= 1 Bluex2 += 1 Next LineLoop1 End Sub :) Ty

                J Offline
                J Offline
                Joshua Quick
                wrote on last edited by
                #7

                It's pretty close. It doesn't quite draw corner to corner on my form. And there is a slight white gap between the 2 triangles. My form is a different size than yours. But it is really close. Good job so far. Two things... 1) I had a compile error on your ToInt32() calls. I had to change them to Convert.ToInt32(). 2) You need to Dispose your pens at every pass of your loop. Both loops really. Do it like this...

                penCurrent.Dispose
                Next LineLoop

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups