Solution for GradientFill errors

Posted by webmaster Guido on July 31, 2007

In Reply to GradientFill example gives errors posted by Dan Forrester P15960 on July 30, 2007

: When I try to compile your source example code for GradientFill with Delphi 7 Personal, I get the errors "Constant expression violates subrange bounds" and "Types of actual and formal var parameters must be identical".

You're right. Amazing... this bug existed since Delphi 3 up to Delphi 7!
I didn't notice it before, because I only tried out GradientFill with Turbo Delphi, and afterwards a quick look in the older Delphi Help documents showed that it also existed in Delphi 7 as well as in Delphi 4.

We found out that the TRIVERTEX structure and the GradientFill function were declared wrongly in all Delphi versions before Delphi 2005. Since Delphi 2005 this bug has been fixed.

So, if you're using Delphi version 7 or older, add the following correct declarations to your source code, at the top of the IMPLEMENTATION section of the form's unit:

implementation
  
{$R  *.DFM}

{ Corrects the wrong declararions of TRIVERTEX ---------------------
  and GradientFill in Delphi unit WINDOWS.PAS
}
type
  TRIVERTEX = packed record
    X, Y: DWORD;
    Red, Green, Blue, Alpha: Word;
  end;

function GradientFill(DC: hDC; pVertex: Pointer; dwNumVertex: DWORD;
  pMesh: Pointer; dwNumMesh, dwMode: DWORD): DWord; stdcall;
		external 'msimg32.dll';
//------------------------------------------------------------------

Tip: if you're using gradient fills with several forms or several projects, better put the corrected code in a separate unit and add this unit to the USES clause of every form unit.

Related articles

       

Follow Ups