Important alert: (current site time 7/16/2013 3:50:39 AM EDT)
 

winzip icon

_SUPER FAST! Gradient Fills (Any Angle) (Updated)

Email
Submitted on: 2/19/2000
By: Rocky Clark (Kath-Rock Software) 
Level: Intermediate
User Rating: By 69 Users
Compatibility: VB 5.0, VB 6.0
Views: 50276
 
     (Updated - Even Faster Now) This class will fill any Form or PictureBox with a Gradient fill starting from Color1 and blending to Color2. Sure, I know you've seen that plenty of times, but this code allows you to select any angle 0º to 359.99999º and it does a perfect blend at all angles. Use any colors, even system colors. Create multiple special effects by setting PictureBoxes next to each other and filling them in different directions. Includes full source code and sample program. PLEASE VOTE FOR ME!

 
winzip iconDownload code

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
  3. Scan the source code with Minnow's Project Scanner

If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com

 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.


Other 12 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

2/19/2000 4:58:50 PMRocky Clark

PLEASE LEAVE A COMMENT AND A VOTE. This will keep the code coming.
(If this comment was disrespectful, please report it.)

 
2/19/2000 7:35:58 PMKarl

This IS the best code I've seen in a long time. Thanks for posting it. It is very fast and the colors are perfect. NICE WORK!
(If this comment was disrespectful, please report it.)

 
2/20/2000 6:18:16 AMGilb

I have to say it, this code is fantastic! I can see where you could make a few optimisations (I mean, the code you use to separate the colour values from the RGB long works, however it could be a LOT smaller).

Well anyway, great stuff and keep it coming!
(If this comment was disrespectful, please report it.)

 
2/21/2000 5:04:16 AMmohammed

it,s what i want
(If this comment was disrespectful, please report it.)

 
2/24/2000 2:42:12 AMGerhard

it's one of the best codes i've ever seen.
(If this comment was disrespectful, please report it.)

 
2/26/2000 6:02:47 AMRocky Clark

2/26/2000 - Optimized the code for even greater speed. Can now fill a 1280 x 1024 screen in less than 50 milliseconds (1/20th of a second).
PLEASE VOTE!
(If this comment was disrespectful, please report it.)

 
2/27/2000 1:11:25 PMKM

This is exceptionally nice code. Well commented, nicely formatted and easy to follow. I had started my own code to do the same thing, but I've replaced it with yours. I give this code an excellent rating.
(If this comment was disrespectful, please report it.)

 
2/28/2000 5:25:18 AMJohn P

Great code!! I'm gonna go look at your other stuff.
(If this comment was disrespectful, please report it.)

 
3/7/2000 6:49:13 AMRandy Mcdowell

This will work perfect for my latest installation program. The beta testers said the plain blue wash gradient was boring, so this outta blow them away!
(If this comment was disrespectful, please report it.)

 
4/24/2000 7:58:56 AMAdam W

Looks great!!!
(If this comment was disrespectful, please report it.)

 
4/24/2000 8:00:05 AMAdam W.

Just as a suggestion, I have read that the TimeGetTime API is more accurate than GetTickCount for precise timing operations.
(If this comment was disrespectful, please report it.)

 
5/8/2000 2:46:24 PMPicklezz

This is one of the best code for gradient things I ever seen
(If this comment was disrespectful, please report it.)

 
5/16/2000 12:34:37 PMBard

Very fine code. Is there a way to specify a certain rectangle of the object to draw the gradient in?
(If this comment was disrespectful, please report it.)

 
5/16/2000 7:08:46 PMRocky Clark (Kath-Rock Software)

Bard,

Yes, you can fill a hidden PictureBox and then use the PaintPicture method to transfer it into any other PictureBox at any coordinates.

Rocky
(If this comment was disrespectful, please report it.)

 
5/17/2000 10:42:49 AMBard

I wanted to use it for something progressbar like. So I only needed a new lower right point. I simply added an optional parameter in the draw method specifying the percents of the box to fill which standard value is 100. A new right end is calculated after the dimensions of the box are ftched with GetWindowRect.
Rect.right=parameter*Rect.right/100
If the parameter is omitted or set to 100 it fills to the vey right, otherwise not.
(If this comment was disrespectful, please report it.)

 
5/25/2000 5:01:17 PMsindizzy

Finally learned how to use the CLS in any project to draw gradients on any picturebox or form. My main question is this, Most of my VB programs are based on many frames. I like the look and i have many seperate option buttons that belong in different frames. But if I paint the main form with a gradient it wont show thru the frames. Is there a way to make the frames color transparent so that the gradient shows thru, yet the frame borders are still visible. thanx.
(If this comment was disrespectful, please report it.)

 
5/28/2000 11:52:05 PMJohn Leabeater

Great stuff! But I implemented this in a project for which I need to transfer the gradient to another form. I'm stuck. How is this accomplished?
(If this comment was disrespectful, please report it.)

 
6/2/2000 2:18:31 PMBard

Hello it's me again.
(If this comment was disrespectful, please report it.)

 
6/2/2000 2:20:39 PMBard

Hello it's me again.
Do you know a way how to implement conic or elliptic gradients such like in Corel Draw?
(If this comment was disrespectful, please report it.)

 
6/2/2000 10:38:43 PMRocky Clark

Hey Bard,
(If this comment was disrespectful, please report it.)

 
6/2/2000 10:40:24 PMRocky Clark (Kath-Rock Software)

Hey Bard,

I have just the thing you're looking for. It's called Ellipse3D and I just put it up on this sight. I'll also email it to you.

Keep on Coding!
Rocky
(If this comment was disrespectful, please report it.)

 
6/5/2000 11:17:22 PMAndri

I like your great idea, it' cool
(If this comment was disrespectful, please report it.)

 
6/6/2000 6:11:32 AMRocky Clark (Kath-Rock Software)

I would like to thank everyone who's comments are listed above. You are the ones who keep the code coming.

Thank You!
Rocky
(If this comment was disrespectful, please report it.)

 
6/9/2000 2:04:32 PMTim Talma

VERY VERY Nice code. Your one of the few people who actually free up memory when doing grapical work (which we all know can be a memory hog). Well comment and clearly thought out. I plaed with this a little and added the ability to pick where the gradient went to on the edge of the box (I think this is a type of conical gradient mentioned above). Anyway keep up the great work.
(If this comment was disrespectful, please report it.)

 
6/17/2000 7:24:37 PMOscar Hernandez

Very good Job..congratulations.
(If this comment was disrespectful, please report it.)

 
7/10/2000 9:28:40 PMcozmik x

whoa.. that code rocks, thanks for sharing it with us!
(If this comment was disrespectful, please report it.)

 
8/5/2000 11:28:33 PMA

Very nice! Beats the snot out of the code I was using! ;)
(If this comment was disrespectful, please report it.)

 
8/14/2000 4:17:02 PMSan Franko

this is very very good, i haven't seen u get one bad comment yet!!

this is fast too, this is brilliant!!
(If this comment was disrespectful, please report it.)

 
10/11/2000 9:32:10 AMLockedincupboard

Do u mind if I nick this?
Cheers
(If this comment was disrespectful, please report it.)

 
10/25/2000 6:42:25 AMHaaaaallpp

haaaaayy...that's good. i have been working on grads myself these days (circle grads, multiple circle grads, boxed grads till now). yo, how bout a gradient class that makes grads from 1 color to another till the middle, then back to the first color? like in ppt. AT ANY ANGLE? i was able to do this only hor + ver but not diagonally or any other angle. but lets c, i'm still experimenting...
(If this comment was disrespectful, please report it.)

 
11/5/2000 10:26:31 AMSourceFalcon

The code works very well, but if you modify it to use doubles instead of singles and remove the unnecessary cdbl commands (considering you're already working with doubles after the change) you'll actually get greater accuracy and about a 33% increase in speed.

[Speed increase measured on a p-3 700 running the demo full screen 1280x1024]

Average time using original class: 75 ms
Average time using modified class: 50 ms
(If this comment was disrespectful, please report it.)

 
11/30/2000 5:14:04 PMTom

I wish all free code was written and commented as well! Thanks for the extremely useful function.
(If this comment was disrespectful, please report it.)

 
3/2/2001 2:17:05 PMImmanuel Ulmer

Amazing. Some time ago I thought how to mix the colors and adjust the gradient, in order to produce a atractive install shield and here I found it. Just beautifull. Thank you.
(If this comment was disrespectful, please report it.)

 
3/2/2001 2:19:58 PMImmanuel Ulmer

Just beautifull. it's what I need to make nice forms. Thank you verry much.
(If this comment was disrespectful, please report it.)

 
7/11/2001 11:44:35 AMKevin

why cant someboydy make a gradient filler for older versions
(If this comment was disrespectful, please report it.)

 
8/1/2001 11:13:44 PMKevin

AHHHH
help me
when i try to set anything in the class
or try to use a function in the class it tells me "Object Required"
ps i have vb4
(If this comment was disrespectful, please report it.)

 
9/8/2001 9:52:10 AMCoite

Well better late than never. Vote of Excellent. This really is most excellent. Contest Winner (I should think so) and before I voted 110 Votes. I just now came across this, and almost two years later.. it's still fantastic!
Thanks for sharing this.
(If this comment was disrespectful, please report it.)

 
9/24/2001 4:00:41 PMMikael Nordfelth

Wonderful!
(If this comment was disrespectful, please report it.)

 
10/11/2001 2:40:53 PMJeroen Paul

Ha, you're the first who has my real name on a post, what a honour (NOT). Great code!!! It's perfect.
(If this comment was disrespectful, please report it.)

 
11/13/2001 3:51:15 PMSergey Olekhnovich

Excellent job! Very useful code!
(If this comment was disrespectful, please report it.)

 
2/5/2002 10:32:44 PMpj

no offense... But I can not get this to work right. THe demo works great and looks awesome, but I need it to work on a form background rather then a pic. Any suggestions?
(If this comment was disrespectful, please report it.)

 
2/6/2002 6:18:19 AMRocky Clark (Kath-Rock Software)

pj,

Look at the DrawGradient() procedure on the main form in the demo. Here's the code.

With mGradient
.Angle = mfAngle
.Color1 = mlColor1
.Color2 = mlColor2
.Draw picDraw
End With
picDraw.Refresh

Now just replace the two instances of "picDraw" with the name of your form, like this.

With mGradient
.Angle = mfAngle
.Color1 = mlColor1
.Color2 = mlColor2
.Draw frmSomeForm
End With
frmSomeForm.Refresh

Make sure your form has AutoRedraw = True.

That's all there is to it!

Rocky
(If this comment was disrespectful, please report it.)

 
3/17/2002 11:19:17 PMyoni eza

this is cool men
(If this comment was disrespectful, please report it.)

 
6/1/2002 11:05:42 AMKeith Hall

Wow! I love this code. I never thought it possible to draw gradients so fast. Thanks man.
(If this comment was disrespectful, please report it.)

 
6/11/2002 4:13:53 AMWaseem Shahzad Bhatti

This is a brilliant code.
(If this comment was disrespectful, please report it.)

 
7/13/2002 5:16:08 PMNathanael B

Wow, this is great! 5 planets.
(If this comment was disrespectful, please report it.)

 
10/18/2002 1:25:12 PM[\DReY\]

Hi Rocky.
I'm use your code in my MP3 Player please look my project andevaluate-me. You can view my code searching DReY MP3.
Thanks for your time. [\DReY\]
(If this comment was disrespectful, please report it.)

 
1/14/2003 12:37:56 AMRoger Gilchrist

Very nice. Small problem, fix:
Edit vbp file
Class=clsGradient; ..\..\Develop\Programming Libraries\Gradient.cls
TO
Class=clsGradient;Gradient.cls
(If this comment was disrespectful, please report it.)

 
8/8/2003 1:55:10 AM

This piece of code is great yaar.... Awesome n cool.. and makes for great backgorund containers of objects!
(If this comment was disrespectful, please report it.)

 
10/27/2003 10:29:02 AM

Like everyone else has said...

Ronnie likes!
(If this comment was disrespectful, please report it.)

 
1/19/2004 3:41:47 PM

i only gave this a 4 because it is very complicated to extract and use in normal ways. but the code itself is very cool.
(If this comment was disrespectful, please report it.)

 
5/8/2005 2:33:01 PMLawrence Wagerfield

Hi, the code is great, but it doesnt work on MDI forms :-(
(If this comment was disrespectful, please report it.)

 
5/15/2005 7:44:43 PM

This is really good code. I'm finding it rather useful.
(If this comment was disrespectful, please report it.)

 
5/15/2005 8:50:45 PM

Any thoughts on how to have the gradient effect saved to a file? I've tried using the SavePicture() function, but the methods isn't being sent to the picDraw control's picture property, and I'm out of ideas. Any solution or idea how to continue? (and I wonder why my user name isn't appearing in the posts) Thanks in advanced!
(If this comment was disrespectful, please report it.)

 
5/16/2005 6:09:57 AMRocky Clark (Kath-Rock Software)

Use SavePicture() and pass the Image property of picDraw.
(If this comment was disrespectful, please report it.)

 
12/7/2005 4:56:07 PMWilliam

Saved me lots of work, thanks for sharing. Good app, well done! 5
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.