Results 1 to 8 of 8

Thread: Draw Gradients [CODE INSIDE]

  1. #1

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253

    Lightbulb Draw Gradients [CODE INSIDE]

    Simple to use, short in code and doesnt take much memory when loading. Set your form to AutoRedraw True, then add this code in the Declarations:

    VB Code:
    1. Private Declare Function LineTo Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
    2. Private Declare Function MoveToEx Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, lpPoint As POINTAPI) As Long
    3.                   Private Type POINTAPI
    4.                          x As Long
    5.                          y As Long
    6.                   End Type

    Then add this code:

    VB Code:
    1. Public Function gradient(obj As Object, col1, col2)
    2. Dim p As POINTAPI
    3.  
    4.                   oldsc = obj.ScaleMode
    5.                   obj.ScaleMode = 3
    6.                   r = (col1 And &HFF&)
    7.                   g = (col1 And &HFF00&) / &H100&
    8.                   b = (col1 And &HFF0000) / &H10000
    9.  
    10.                   r2 = (col2 And &HFF&)
    11.                   g2 = (col2 And &HFF00&) / &H100&
    12.                   b2 = (col2 And &HFF0000) / &H10000
    13.                   sw = obj.ScaleWidth
    14.                   dr = (r2 - r) / sw
    15.                   dg = (g2 - g) / sw
    16.                   db = (b2 - b) / sw
    17.                   obj.Cls
    18.                   For i = 1 To sw
    19.                   r = r + dr
    20.                   g = g + dg
    21.                   b = b + db
    22.                   y = obj.ScaleHeight
    23.                   obj.ForeColor = RGB(CInt(r), CInt(g), CInt(b))
    24.                   MoveToEx Form1.hdc, i, 0, p
    25.                   LineTo obj.hdc, i, y
    26.  
    27.                   Next i
    28.  
    29.                   obj.ScaleMode = oldsc
    30.                   End Function

    And finally add this:

    VB Code:
    1. Private Sub Form_Resize()
    2. gradient Form1, Color1, Color2
    3. End Sub

    Enjoy;)

  2. #2

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    You can also make your own custom colored gradients, make a command button, and add a Dialog1 and Dialog2 control then.

    Private Sub Command1_Click()
    Dialog1.ShowColor
    Dialog2.ShowColor
    gradient Me, Dialog1.Color, Dialog2.Color
    End Sub

    Im away from vb so this may be wrong. But the idea is there!

  3. #3
    New Member
    Join Date
    Nov 2003
    Location
    London, UK
    Posts
    9


    There is actually an API function that draws gradient rectangles; I found this out when making the bsGradientLabel control.

    http://www.mentalis.org/apilist/GradientFillRect.shtml
    Drew (aka Headdy)

  4. #4

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    Yeah, that site has tons of API codes

  5. #5

  6. #6

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    Cheers marty, i forgot you could do that.

  7. #7

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    Hee hee

    The code doesnt work anymore, so im just going to try and fix it up now...unless my messed up computer has messed up VB

    EDIT

    This is awful to use however with Option Explicit, you'll have to dim a ton of variables Is there no such thing as Option Implicit so you can make that paticular routine, well.....Implicit?
    Last edited by Madboy; Mar 30th, 2004 at 11:29 AM.

  8. #8
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    Originally posted by Madboy
    you'll have to dim a ton of variables
    You should do that though...


    Has someone helped you? Then you can Rate their helpful post.

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