渐变填充及delphi中相关部分的修正 [原]
sundytu
12:38 PM
在windows98后的MS操作系统中,新增了很多图形处理函数,他们都位于msimg32.dll中,利用这些函数可以快速得到很多很炫目的效果,并且自己不要做什么工作,由系统去做,我们要做的只是告诉它如何画就可以了,这里介绍一下渐变填充函数的应用。
下面是这个函数的声明,其实在delphi中有这个函数的声明:GradientFill,但使用过程中我发现它的数据结构与MSDN上最新的结构定义不同,所以这里改了一下:
TriVertexSundy = record
x: DWORD;
y: DWORD;
Red: WORD;
Green: WORD;
Blue: WORD;
Alpha: WORD;
end;
function GradientFillEx(DC: HDC; var Vertex: TriVertexSundy; NumVertex: DWORD; Mesh: Pointer; NumMesh, Mode: DWORD): boolean; stdcall;
external 'msimg32.dll' name 'GradientFill';//这个函数的参数定义大家可以参照MSDN的说明,很简单的!
然后我们就可以利用这个函数做出很多炫目的效果了,这里的代码只是做了横向的矩形渐变填充,对于三角的渐变和纵向渐变填充可以通过这个例子以及MSDN的说明举一反三。
下面是源代码:
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TriVertexSundy = record
x: DWORD;
y: DWORD;
Red: WORD;
Green: WORD;
Blue: WORD;
Alpha: WORD;
end;
TForm1 = class(TForm)
Timer1: TTimer;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
function GradientFillEx(DC: HDC; var Vertex: TriVertexSundy; NumVertex: DWORD; Mesh: Pointer; NumMesh, Mode: DWORD): boolean; stdcall;
external 'msimg32.dll' name 'GradientFill';
var
Form1: TForm1;
l: integer = 0;
memDC: HDC;
memBmp, tmpbmp: HBitMap;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var r: TRect;
vert: array[0..3] of TriVertexSundy;
gTRi: array[0..1] of TGradientRect;
DC: HDC;
begin
DC := GetDC(0);
if memBmp <> 0 then
begin
SelectObject(memdc, tmpbmp);
deleteobject(memBmp);
end;
if memdc <> 0 then DeleteDC(memdc);
memDC := CreateCompatibleDC(DC);
memBmp := CreateCompatibleBitmap(DC, 200, 20);
releaseDC(0, DC);
tmpbmp := SelectObject(memDC, membmp);
vert[0].x := 0;
vert[0].y := 0;
vert[0].Red := $FFFF;
vert[0].Green := $FFFF;
vert[0].Blue := $FFFF;
vert[0].Alpha := 0;
vert[1].x := 100;
vert[1].y := 20;
vert[1].Red := 0;
vert[1].Green := 0;
vert[1].Blue := $FFFF;
vert[1].Alpha := 0;
vert[2].x := 100;
vert[2].y := 0;
vert[2].Red := 0;
vert[2].Green := 0;
vert[2].Blue := $FFFF;
vert[2].Alpha := 0;
vert[3].x := 200;
vert[3].y := 20;
vert[3].Red := $FFFF;
vert[3].Green := $FFFF;
vert[3].Blue := $FFFF;
vert[3].Alpha := 0;
gTRi[0].UpperLeft := 0;
gTRi[0].LowerRight := 1;
gTRi[1].UpperLeft := 2;
gTRi[1].LowerRight := 3;
GradientFillEx(memDC, vert[0], 4, @gTRI, 2, GRADIENT_FILL_Rect_H);
Timer1.Enabled := not Timer1.Enabled;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if memBmp <> 0 then
begin
SelectObject(memdc, tmpbmp);
deleteobject(memBmp);
end;
if memdc <> 0 then DeleteDC(memdc);
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var DC: HDC;
begin
DC := getDC(Handle);
bitblt(DC, 0, 0, l, 20, memdc, 200 - l, 0, SRCCOPY);
bitblt(DC, l, 0, 200 - l, 20, memdc, 0, 0, SRCCOPY);
l := (l + 10) mod 200;
ReleaseDC(Handle, DC);
end;
end.