|
-
Apr 5th, 2008, 11:20 AM
#1
Thread Starter
Lively Member
[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 ?
-
Apr 5th, 2008, 11:35 AM
#2
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.
-
Apr 5th, 2008, 11:36 AM
#3
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.
-
Apr 5th, 2008, 12:45 PM
#4
Thread Starter
Lively Member
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
-
Apr 5th, 2008, 12:59 PM
#5
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.
-
Apr 5th, 2008, 01:03 PM
#6
Thread Starter
Lively Member
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
-
Apr 5th, 2008, 01:13 PM
#7
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.
-
Apr 5th, 2008, 01:20 PM
#8
Re: Gradient in a Round Shape
 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|