Results 1 to 8 of 8

Thread: [RESOLVED] Gradient in a Round Shape

  1. #1

    Thread Starter
    Lively Member MaxRaceSoftware's Avatar
    Join Date
    Feb 2007
    Posts
    83

    Resolved [RESOLVED] Gradient in a Round Shape

    How could i create a multi-color smooth looking color gradient
    in a round shape ... maybe even like in the
    Round Shape Control ? , or changing the
    shape of a PictureBox Control to Round in which a Color Gradient
    would appear ?

  2. #2
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Gradient in a Round Shape

    You need to create a region, basically clipping off parts of the form.

    I don't have any gradient code handy right now but there are lots at www.pscode.com/vb if you search for gradient.

    But try starting a new VB project and running this code...hopefully my memory serves me correctly...

    Code:
    Option Explicit
    
    Private Declare Function CreateRoundRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long, ByVal X3 As Long, ByVal Y3 As Long) As Long
    Private Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long
    
    Private Sub Form_Load()
        Me.BorderStyle = 0
        Me.Caption = vbNullString
        Me.ScaleMode = vbPixels
        Me.AutoRedraw = True 'For gradient.
        
        'Draw gradient here...
    
        'Create rounded region.
        '15, 15 is radius of rounded corners.
        'Higher the value, the more round they are.
        Dim lonRgn As Long
        lonRgn = CreateRoundRectRgn(0, 0, Me.ScaleWidth, Me.ScaleHeight, 15, 15)
        SetWindowRgn Me.hWnd, lonRgn, True
    End Sub
    In this example I used a form, but you can do it to a PictureBox also.

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Gradient in a Round Shape

    Shape control? Not really possible as far as I know, you don't have control over how that is painted or when it is painted.

    Picturebox you have control over but requires some API usage. Getting a smooth gradient is relatively simple, but getting a smooth circular boundary is not -- the best it may look like is a circular shape control. If that is all you are expecting, then...

    If you plan on having like hundreds of these, then other options can be employed which include creating these in a paint program and saving them as a transparent GIF, then using the Image control. Other options could be available too, but we would need more details.

    1. Look at this example pertaining to the GradientFill API. You will probably want to use it vs creating your own gradient routines -- your choice of course

    2. Your picturebox should be AutoRedraw=True, no borders. Then call this command in your Form_Load function and add the 2 declarations to your form. The code below shapes the picturebox.

    Edited: I may have misunderstood, if circular shape is needed, use the APIs below. If rectangle with rounded corners is needed, use the ones DigiRev gave you.
    Code:
    ' declarations section
    Private Declare Function CreateEllipticRgn Lib "gdi32.dll" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
    Private Declare Function SetWindowRgn Lib "user32.dll" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long
    
    Private Sub Form_Load()
        Dim hRgn As Long
        Picture1.ScaleMode = vbPixels
        hRgn = CreateEllipticRgn(0, 0, Picture1.ScaleWidth, Picture1.ScaleHeight)
        SetWindowRgn Picture1.hWnd, hRgn, True
    End Sub
    Last edited by LaVolpe; Apr 5th, 2008 at 11:43 AM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4

    Thread Starter
    Lively Member MaxRaceSoftware's Avatar
    Join Date
    Feb 2007
    Posts
    83

    Re: Gradient in a Round Shape

    Thanks a bunch , DigiRev and LaVolpe ....that was quick and easy !

    i can figure the rest of the Gradient code
    to display in the Round or Elliptical PictureBox shapes

    Code:
    Option Explicit
    
    Private Declare Function CreateRoundRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long, ByVal X3 As Long, ByVal Y3 As Long) As Long
    Private Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long
    Private Declare Function CreateEllipticRgn Lib "gdi32.dll" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
    
    Private Sub Form_Load()
        
        Picture1.BorderStyle = 0
        Picture1.ScaleMode = vbPixels
        Picture1.AutoRedraw = True 'For gradient.
        
        'Draw gradient here...
    
        'Create rounded region.
        '15, 15 is radius of rounded corners.
        'Higher the value, the more round they are.
        'Dim lonRgn As Long
        'lonRgn = CreateRoundRectRgn(0, 0, Picture1.ScaleWidth, Picture1.ScaleHeight, 50, 50)
        'SetWindowRgn Picture1.hWnd, lonRgn, True
    
    
        Dim hRgn As Long
        hRgn = CreateEllipticRgn(200, 0, Picture1.ScaleWidth, Picture1.ScaleHeight)
        SetWindowRgn Picture1.hWnd, hRgn, True
    
    
    
    End Sub

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Gradient in a Round Shape

    Cool. If you want to give your shapes a solid color border that can be done too. Just let us know.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6

    Thread Starter
    Lively Member MaxRaceSoftware's Avatar
    Join Date
    Feb 2007
    Posts
    83

    Re: Gradient in a Round Shape

    Cool. If you want to give your shapes a solid color border that can be done too. Just let us know.



    OK, great , that would also be very helpful...
    how would i go about doing that also ?

    One more question or potential problem or conflict ??

    i'm developing an Application that requires GDI-Plus
    any problems with GDI-Plus -vs- regular GDI ??
    ( Win98 on up. ) including Vista

  7. #7
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Gradient in a Round Shape

    Simple, change vbRed to border color you want (do not use system colors, i.e., don't use vbButonFace). But order is important here
    1. Do your gradient
    2. Create your hRgn before doing the border, and don't assign it via SetWindowRgn yet
    3. Draw the border
    4. Assign region via SetWindowRgn
    Code:
    ' 3 more apis
    Private Declare Function FrameRgn Lib "gdi32.dll" (ByVal hdc As Long, ByVal hRgn As Long, ByVal hBrush As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
    Private Declare Function CreateSolidBrush Lib "gdi32.dll" (ByVal crColor As Long) As Long
    Private Declare Function DeleteObject Lib "gdi32.dll" (ByVal hObject As Long) As Long
    
    ' outline code, add to your project where needed
    Dim hBrush As Long
        hBrush = CreateSolidBrush(vbRed)
        FrameRgn Picture1.hdc, hRgn, hBrush, 1, 1
        DeleteObject hBrush
    Edited: FYI, the reason why order is important is that if your gradient is done after border, the gradient will probably overpaint the border. The region if referenced after using SetWindowRgn may be invalid (depends on O/S) so nothing will be done. If for some reason a border needs to be painted after SetWindowRgn is called, recreate the region, frame the region then destroy the region with DeleteObject hRgn. No need to call SetWindowRgn again.
    Last edited by LaVolpe; Apr 5th, 2008 at 02:57 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  8. #8
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Gradient in a Round Shape

    Quote Originally Posted by MaxRaceSoftware
    One more question or potential problem or conflict ??
    i'm developing an Application that requires GDI-Plus
    any problems with GDI-Plus -vs- regular GDI ??
    ( Win98 on up. ) including Vista
    Generally -- no conflicts. However, handles and objects used/created by one very rarely can be used by the other.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

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