I've been checking out ways to fill a rectangle with a color gradient
(and hopefully other shapes).

The msimg32.dll, supplied with Win98 onwards.
Unfortunately two of the funcs in this, AphlaBlend() and TransparentBlt()
have memory leaks. Microsoft claimed to have fixed the leaks with this
DLL when they released WinMe, but I have read reports that's not so.
This DLL also has GradientBlend(), which, as far as I know, doesn't have
a memory leak.
It is available for download from many places on the web, and presumably
you can place it in Win95. I see one site has two versions, for 9x and 2000.

This is a MFC method, and you pass the address of a RECT structure and a
color, no need to change brushes. So, you could put it into a tight loop
drawing one rectangle inside another, to get your gradient effect.
The problem is, I have no idea how to call it from MASM.... maybe this is a
COM forum question?

I came across a comment somewhere, I think it was the codeproject.com site,
someone said that Drawtext can draw colored rectangles faster than the rectangle
functions. You call SetBkColor(), feed Drawtext() a null string, and you get a filled
rectangle. You may also have to call SetBkMode() to choose OPAQUE mode.
You put SetBkColor() and DrawText() into a tight loop drawing rectangles inside
each other, and you get your gradient.

I haven't tried any of these yet. But, real interesting hey?
I wonder if method 3 is really faster?

Barry Kauler
Posted on 2003-03-04 19:26:54 by bkauler
No, sorry, the func in msimg32.dll is called "GradientFill()", not

Barry Kauler
Posted on 2003-03-04 19:29:00 by bkauler
Method 3 doesn't work.
There doesn't seem to be any way to use DrawText() to paint just
the filled rectangle without any text, unless you use blank characters.

I wonder if it is all that inefficient to just draw a line using say LineTo()
delete pen, create a new one (1 pixel thick), etc., in a loop?...
At least, this method can be adapted to fill any kind of shapes.
This can be Method 4.

Barry Kauler
Posted on 2003-03-05 17:37:38 by bkauler
Are you by any chance the same Barry Kauler who
wrote the book about Win asm programming?
Posted on 2003-03-05 18:29:15 by The Svin

In the past I have used MoveToEx and LineTo to perform gradient fills of client areas and it seems to work OK but I doubt it is fast enough to continually loop it without bad flickering.

As far as the shapes go, I would look at irregular regions to handle the shapes rather than calculating the line length each time but I think it would still be slow.


Posted on 2003-03-06 04:26:59 by hutch--

Are you by any chance the same Barry Kauler who
wrote the book about Win asm programming?

Yes he is (check his homepage) :)
Posted on 2003-03-06 04:30:44 by bazik

Yes he is (check his homepage) :)

Why didn't he tell it himself?
Might be he is too shy?
OK we'll write some granient color app to encourage him
to talk.
Posted on 2003-03-07 12:58:17 by The Svin
give people the time to check in on new posts :)
Posted on 2003-03-07 13:01:09 by Hiroshimator
GDI is not my field - it's just the first
idea that came to my mind coded for a minute.
Could be lots other ways.
Posted on 2003-03-07 14:25:46 by The Svin
I have changed couple stupid lines
Posted on 2003-03-07 14:54:02 by The Svin
I was away for a few days.
I'm the same Barry Kauler. That book is ancient history now
(written 1996, published Jan. 1997)!

I've just downloaded the zip file -- will have a look at it
-- much thanks for the response.

Barry Kauler
Posted on 2003-03-08 21:54:24 by bkauler

I've never been able to find it to read. I know how hard it is to write a book since I was approached to do one in the past and didn't get past first base. Have you ever considered rewriting it or is there just no market interest? Or interest on your part? It might be a good online book, though.

I once had an article published in BYTE magazine in the 80s that's now reference by the ACM.(!) It was a subject I was deeply involved in at the time so writing it was fairly easy and it was only 10 published pages or so.
Posted on 2003-03-09 11:25:05 by drhowarddrfine
I wrote the first edition in 1992, all 16-bit programming for Win3.x.
In 1996 I put in about 20% 32-bit coding for the 2nd edition.
Any third edition would have needed a complete rewrite, to be
all 32-bit, plus all the new goodies in Win32 API.
Also, the publisher lost interest. ...book publisher editors are strange
I wouldn't do it anyway, as it's so much work. After you write it, the
publisher keeps sending it back for revisions and this goes on for
3-6 months.

Just coding asm is much more fun, though doesn't earn anything.
Which brings me back to color gradients...
I ended up just drawing one rectangle or ellipse inside another, changing
the pen, no fill until the innermost one. What I really want to do is get
simple 3D effects and highlighting.
Well, the code is primitive, but the results look okay... see this GIF snapshot:

There is flicker when the object gets redrawn, but I am living with that.
I could use ScrollDC() to simply move the pixels, but in my case there are
complications with using that. Maybe some other method to grab the pixels
of a rect or ellipse region off the screen, move them?

Barry Kauler
Posted on 2003-03-09 17:39:53 by bkauler

I just had a piece of genius, depending on the level of control you require, what about dynamically creating a bitmap that has the colour gradient you require and use BitBlt to place and move the bitmap. It should be fast and flicker free if you do it right.


Posted on 2003-03-10 03:58:50 by hutch--
I had the same thought.
It's better to have bitmap as kinda canvas,
change it as memory piece to whatever you need and then display it in one call.
The same "canvas bitmap" can be used for different
places. And dinamically changed to your needs.

Matching size of the canvas and particular rects can be discussed after clarifying what and what for Barry wants
the code.
Posted on 2003-03-10 04:48:24 by The Svin
I'm no expert but as far as my "experience" goes you should use a DIB (device independant bitmap), draw the gradient to that and then blit to screen.
Posted on 2003-03-10 05:45:03 by SFP

Can someone tell me how to change the color of a line?? I 've tried it whit CreatePen, GetStockObject etc.... but nothing seems to work.

exp of what I want to do.......

//initial color here is black (done with GetStockObject,...........; blablablabla)

invoke InvalidateRect,hWin,NULL,TRUE
invoke BeginPaint,hWin,ADDR paint
mov hdc,eax
invoke MoveToEx,hdc,10,10,ADDR point
invoke LineTo,hdc,10,500
invoke MoveToEx,hdc,10,500,ADDR point

// here I want to change the color of my pen wich I use to draw lines with let us say in Redinvoke
// here I draw the line

invoke MoveToEx,hdc,500,500,ADDR point

//here I want to choose another color

Thanks for your help gays..........
Posted on 2003-10-20 04:36:32 by raidu
Be sure to select the pen into the device context before you call LineTo...

invoke CreatePen,PS_SOLID,1,rgbColor

push eax
invoke SelectObject,hDC,eax
push eax
invoke MoveToEx,hDC,Anchor.x,Anchor.y,NULL
invoke LineTo,hDC,endpoint.x,endpoint.y
invoke SetPixelV,endpoint.x,endpoint.y,rgbColor ; Be sure to paint the last pixel
pop eax
invoke SelectObject,hDC,eax
pop eax
invoke DeleteObject,eax

BTW do not pass a point structure as the last parameter in MoveToEx unless you plan to use it, it degrades performance.
Posted on 2003-10-20 10:46:32 by donkey
Thanks for your help and (ahum) what I mean is GUYS and not gays of course.
Posted on 2003-10-21 16:01:48 by raidu

I was away for a few days.
I'm the same Barry Kauler. That book is ancient history now
(written 1996, published Jan. 1997)!

I've just downloaded the zip file -- will have a look at it
-- much thanks for the response.

Barry Kauler

ancient my left cheek, i bought that book 2 years ago and i still learned from it =). You need to write another book so that we can learn more from you:alright:
Posted on 2003-10-21 18:56:44 by Guy on ASM