VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



The following code draws a gradient on the form, from one color to another. It uses WIN32 API in or

by Nir Sofer (3 Submissions)
Category: Graphics
Compatability: Visual Basic 4.0 (32-bit)
Difficulty: Unknown Difficulty
Originally Published: Mon 25th February 2002
Date Added: Mon 8th February 2021
Rating: (1 Votes)

The following code draws a gradient on the form, from one color to another. It uses WIN32 API in order to get the best performances. The

API Declarations


Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Private Declare Function GetClientRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function FillRect Lib "user32" (ByVal hdc As Long, lpRect As RECT, ByVal hBrush As Long) As Long
Private Declare Function SetRect Lib "user32" (lpRect As RECT, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long


Rate The following code draws a gradient on the form, from one color to another. It uses WIN32 API in or



    If X2 = 0 Then SafeDiv = 0 Else SafeDiv = X1 / X2
End Function

Private Sub PaintGradient(frm As Form, Red1 As Integer, Green1 As Integer, Blue1 As Integer, Red2 As Integer, Green2 As Integer, Blue2 As Integer)
    Dim WinRect     As RECT
    Dim ColorRect   As RECT
    Dim Y           As Long
    Dim hBrush      As Long
    Dim hPrevBrush  As Long
    Dim DivValue    As Double
    Dim CurrRed     As Integer
    Dim CurrGreen   As Integer
    Dim CurrBlue    As Integer
    
    GetClientRect frm.hwnd, WinRect
    For Y = WinRect.Top To WinRect.Bottom
        DivValue = SafeDiv((WinRect.Bottom - WinRect.Top), (Y - WinRect.Top))
        CurrRed = Red1 + SafeDiv((Red2 - Red1), DivValue)
        CurrGreen = Green1 + SafeDiv((Green2 - Green1), DivValue)
        CurrBlue = Blue1 + SafeDiv((Blue2 - Blue1), DivValue)
        SetRect ColorRect, WinRect.Left, Y, WinRect.Right, Y + 1
        hBrush = CreateSolidBrush(RGB(CurrRed, CurrGreen, CurrBlue))
        hPrevBrush = SelectObject(frm.hdc, hBrush)
        FillRect frm.hdc, ColorRect, hBrush
        SelectObject frm.hdc, hPrevBrush
        DeleteObject hBrush
    Next
End Sub

Private Sub Form_Paint()
    PaintGradient Me, 255, 128, 0, 128, 0, 255
End Sub


Download this snippet    Add to My Saved Code

The following code draws a gradient on the form, from one color to another. It uses WIN32 API in or Comments

No comments have been posted about The following code draws a gradient on the form, from one color to another. It uses WIN32 API in or. Why not be the first to post a comment about The following code draws a gradient on the form, from one color to another. It uses WIN32 API in or.

Post your comment

Subject:
Message:
0/1000 characters