Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Multimedia (https://www.delphipraxis.net/16-multimedia/)
-   -   Delphi Windows.GradientFill (https://www.delphipraxis.net/79610-windows-gradientfill.html)

Neutral General 25. Okt 2006 12:08


Windows.GradientFill
 
Hi,

Habe gerade mal die Windows Unit durchgekramt aus Langeweile.. Bin auf ne Funktion namens GradientFill gestoßen. Macht die echt das was ich denke was sie macht? Naja egal. Ich will sie gerne mal ausprobieren. Aber ich weiß bei manchen Parametern nicht weiter:

Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
var test: _TRIVERTEX;
begin
  test.x := 20;
  test.y := 50;
  test.Red := 100;
  test.Green := 50;
  test.Blue := 30;
  test.Alpha := 125;
  Windows.GradientFill(Canvas.Handle,ehm,{1},{2},{3},GRADIENT_FILL_TRIANGLE);
end;
1. NumVertex: Cardinal
2. Mesh: Pointer
3. NumMesh: Cardinal

Was bedeuten diese Parameter und was muss ich übergeben? Vorallem bei dem Pointer hab ich keine Ahnung was ich da Pointern soll... Ich hab mal irgendnen Schwachsinn übergeben da oder was nicht so Schwachsinniges aber es passiert einfach nie was. Es gibt auch keine AV...

Gruß
Neutral General

mkinzler 25. Okt 2006 12:16

Re: Windows.GradientFill
 
Die Hilfe sagt mir:
Zitat:

Zitat von Delphi Hilfe
GradientFill
The GradientFill function fills rectangle and triangle structures.

BOOL GradientFill(
HDC hdc, // handle to DC
PTRIVERTEX pVertex, // array of vertices
ULONG dwNumVertex, // number of vertices
PVOID pMesh, // array of gradients
ULONG dwNumMesh, // size of gradient array
ULONG dwMode // gradient fill mode
);
Parameters
hdc
[in] Handle to the destination device context.
pVertex
[in] Pointer to an array of TRIVERTEX structures that each define a triangle vertex.
dwNumVertex
[in] The number of vertices in pVertex.
pMesh
[in] Array of GRADIENT_TRIANGLE structures in triangle mode, or an array of GRADIENT_RECT structures in rectangle mode.
dwNumMesh
[in] The number of elements (triangles or rectangles) in pMesh.
dwMode
[in] Specifies gradient fill mode. This parameter can be one of the following values. Value Meaning
GRADIENT_FILL_RECT_H In this mode, two endpoints describe a rectangle. The rectangle is defined to have a constant color (specified by the TRIVERTEX structure) for the left and right edges. GDI interpolates the color from the left to right edge and fills the interior.
GRADIENT_FILL_RECT_V In this mode, two endpoints describe a rectangle. The rectangle is defined to have a constant color (specified by the TRIVERTEX structure) for the top and bottom edges. GDI interpolates the color from the top to bottom edge and fills the interior.
GRADIENT_FILL_TRIANGLE In this mode, an array of TRIVERTEX structures is passed to GDI along with a list of array indexes that describe separate triangles. GDI performs linear interpolation between triangle vertices and fills the interior. Drawing is done directly in 24- and 32-bpp modes. Dithering is performed in 16-, 8-, 4-, and 1-bpp mode.


Return Values
If the function succeeds, the return value is TRUE.

If the function fails, the return value is FALSE.

Windows NT/2000/XP: To get extended error information, call GetLastError.

Remarks
To add smooth shading to a triangle, call the GradientFill function with the three triangle endpoints. GDI will linearly interpolate and fill the triangle.

To add smooth shading to a rectangle, call GradientFill with the upper-left and lower-right coordinates of the rectangle. There are two shading modes used when drawing a rectangle. In horizontal mode, the rectangle is shaded from left-to-right. In vertical mode, the rectangle is shaded from top-to-bottom.

The GradientFill function uses a mesh method to specify the endpoints of the object to draw. All vertices are passed to GradientFill in the pVertex array. The pMesh parameter specifies how these vertices are connected to form an object. When filling a rectangle, pMesh points to an array of GRADIENT_RECT structures. Each GRADIENT_RECT structure specifies the index of two vertices in the pVertex array. These two vertices form the upper-left and lower-right boundary of one rectangle.

In the case of filling a triangle, pMesh points to an array of GRADIENT_TRIANGLE structures. Each GRADIENT_TRIANGLE structure specifies the index of three vertices in the pVertex array. These three vertices form one triangle.

To simplify hardware acceleration, this routine is not required to be pixel-perfect in the triangle interior.

Note that GradientFill does not use the Alpha member of the TRIVERTEX structure. To use GradientFill with transparency, call GradientFill and then call AlphaBlend with the desired values for the alpha channel of each vertex.

For more information, see Smooth Shading, Drawing a Shaded Triangle, and Drawing a Shaded Rectangle.

Requirements
Windows NT/2000/XP: Included in Windows 2000 or later.
Windows 95/98/Me: Included in Windows 98 or later.
Header: Declared in Wingdi.h; include Windows.h.
Library: Included as a resource in Msimg32.dll.

See Also
Bitmaps Overview, Bitmap Functions, EMRGRADIENTFILL, GRADIENT_RECT, GRADIENT_TRIANGLE, TRIVERTEX


--------------------------------------------------------------------------------

© 2002 Microsoft Corporation. All rights reserved.

Requirements
Windows NT/2000/XP: Included in Windows 2000 or later.
Windows 95/98/Me: Included in Windows 98 or later.
Header: Declared in Wingdi.h; include Windows.h.
Library: Included as a resource in Msimg32.dll.
See Also
Bitmaps Overview, Bitmap Functions, EMRGRADIENTFILL, GRADIENT_RECT, GRADIENT_TRIANGLE, TRIVERTEX


--------------------------------------------------------------------------------

© 2002 Microsoft Corporation. All rights reserved.


Neutral General 25. Okt 2006 12:29

Re: Windows.GradientFill
 
Ok danke... Naja meine Hilfe hat mir komischerweise nichts dazu gesagt .... :roll:

STOPP!

Zitat:

[in] Pointer to an array of TRIVERTEX structures that each define a triangle vertex.
dwNumVertex
Bei mir wird nur ein einzelnes TRIVERTEX erwartet und akzeptiert o.O

Flocke 25. Okt 2006 16:01

Re: Windows.GradientFill
 
Ist halt (wie so viele Dinge) von Borland nicht korrekt deklariert worden. Nimm ein Array von TTriVertex Elementen und übergib das erste als Var-Parameter.

turboPASCAL 25. Nov 2008 14:05

Re: Windows.GradientFill
 
Ich habe mir grad eine Funktion gebastelt:

Delphi-Quellcode:
var
  _GradientFill: function(DC: HDC; pTriVertex: Pointer; dwNumVertex: DWORD;
  pMesh: Pointer; dwNumMesh, dwMode: DWORD): DWORD; stdcall; { external in "msimg32.dll" }

type
  TFillMode = GRADIENT_FILL_RECT_H..GRADIENT_FILL_RECT_V;

function wGradientFill(DC: HDC; fillRect: TRect; startColor, endColor: COLORREF; FillMode: TFillMode): Boolean;
type
  _TTRIVERTEX = packed record
    X, Y: DWORD;
    Red, Green, Blue, Alpha: Word;
  end;
var
  tv: array [0..1] of _TTRIVERTEX;
  gr: GRADIENT_RECT;
  hLib: THandle;
begin
  // -------------------------------------------------------------------------
  // sollte global aufgerufen werden zB. in FormCreate
  // an sonsten wird das laden und freigeben unnötig oft aufgerufen
  hLib := LoadLibrary('MSIMG32.DLL');
  if hLib <> 0 then
    @_GradientFill := GetProcAddress(hLib, 'GradientFill');
  // -------------------------------------------------------------------------

  if @_GradientFill <> nil then
  begin
    // fillmode direction
    gr.UpperLeft := 0;
    gr.LowerRight := 1;

    // eckpunkte festlegen
    tv[0].x    := fillRect.Left;
    tv[0].y    := fillRect.Top;
    tv[1].x    := fillRect.Right;
    tv[1].y    := fillRect.Bottom;

    // -------------------------------------------------------------------------
    // -> $0000 or BYTE(startColor shr 8) shl 8;
    // ein wenig trciki, GradientFill erwartet als farbwert ein word
    // wobei das farbbyte an erster stelle stehen muss.
    // ich verzichte hierbei gleich auf GetRValue() usw. von Windows. ;-)

    tv[0].Red  := $0000 or BYTE(startColor) shl 8;
    tv[0].Green := $0000 or BYTE(startColor shr 8) shl 8;
    tv[0].Blue := $0000 or BYTE(startColor shr 16) shl 8;
    tv[0].Alpha := $0000 or BYTE(startColor shr 24) shl 8;

    tv[1].Red  := $0000 or BYTE(endColor) shl 8;
    tv[1].Green := $0000 or BYTE(endColor shr 8) shl 8;
    tv[1].Blue := $0000 or BYTE(endColor shr 16) shl 8;
    tv[1].Alpha := $0000 or BYTE(endColor shr 24) shl 8;

    Result := _GradientFill(DC, @tv, 2, @gr, 1, FillMode) <> 0;
  end else
    Result := FALSE;

  // -------------------------------------------------------------------------
  // sollte global aufgerufen werden zB. in FormDestroy
  if hLib <> 0 then FreeLibrary(hLib);
  // -------------------------------------------------------------------------
end;
Aufruf zB.:

Delphi-Quellcode:
begin
  //...
 
  r := FormX.Clientrect;

  if @wGradientFill <> nil
    then wGradientFill(FormX.Canvas.Handle, r, clRed, clGreen, GRADIENT_FILL_RECT_H)
    else FormX.Canvas.FillRect(r);

Ich wusste doch das wir das in DP schon mal als Thema hatten. :gruebel:

turboPASCAL 15. Sep 2009 20:00

Re: Windows.GradientFill
 
Hier mal noch ein Beispiel mit drei Farben:

Delphi-Quellcode:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TForm1 = class(TForm)
    procedure FormPaint(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function GradientFill(DC: HDC; pTriVertex: Pointer; dwNumVertex: DWORD;
  pMesh: Pointer; dwNumMesh, dwMode: DWORD): BOOL; stdcall; external 'msimg32.dll' name 'GradientFill';

type
  TFillMode = (fmHorizontal = GRADIENT_FILL_RECT_H, fmVertiktal = GRADIENT_FILL_RECT_V);

function wGradientFill(DC: HDC; fillRect: TRect; Col: array of COLORREF; FillMode: TFillMode): Bool;
type
  _TTRIVERTEX = packed record
    X, Y: DWORD;
    Red, Green, Blue, Alpha: Word;
  end;
var
  tv: array [0..3] of _TTRIVERTEX;
  gr: array [0..1] of GRADIENT_RECT;
begin
  if length(col) = 3 then
  begin
    // fillmode direction
    gr[0].UpperLeft := 0;
    gr[0].LowerRight := 1;
    gr[1].UpperLeft := 2;
    gr[1].LowerRight := 3;

    // eckpunkte festlegen
    tv[0].x    := fillRect.Left;
    tv[0].y    := fillRect.Top;
    tv[1].x    := fillRect.Right;
    tv[1].y    := fillRect.Bottom div 2;

    tv[2].x    := fillRect.Left;
    tv[2].y    := fillRect.Bottom div 2;
    tv[3].x    := fillRect.Right;
    tv[3].y    := fillRect.Bottom;

    // -------------------------------------------------------------------------
    // -> $0000 or BYTE(startColor shr 8) shl 8;
    // ein wenig trckiki, GradientFill erwartet als farbwert ein word
    // wobei das farbbyte an erster stelle stehen muss.
    // ich verzichte hierbei gleich mal auf GetRValue() usw. von Windows. ;-)

    tv[0].Red  := BYTE(col[0]) shl 8;
    tv[0].Green := BYTE(col[0] shr 8) shl 8;
    tv[0].Blue := BYTE(col[0] shr 16) shl 8;
    tv[0].Alpha := BYTE(col[0] shr 24) shl 8;

    tv[1].Red  := BYTE(col[1]) shl 8;
    tv[1].Green := BYTE(col[1] shr 8) shl 8;
    tv[1].Blue := BYTE(col[1] shr 16) shl 8;
    tv[1].Alpha := BYTE(col[1] shr 24) shl 8;

    tv[2].Red  := BYTE(col[1]) shl 8;
    tv[2].Green := BYTE(col[1] shr 8) shl 8;
    tv[2].Blue := BYTE(col[1] shr 16) shl 8;
    tv[2].Alpha := BYTE(col[1] shr 24) shl 8;

    tv[3].Red  := BYTE(col[2]) shl 8;
    tv[3].Green := BYTE(col[2] shr 8) shl 8;
    tv[3].Blue := BYTE(col[2] shr 16) shl 8;
    tv[3].Alpha := BYTE(col[2] shr 24) shl 8;

    Result := GradientFill(DC, @tv, length(tv), @gr, length(gr), DWORD(FillMode));
  end else
  begin
    Application.MessageBox('Anzahl der Farben stimmt nicht.', 'wGradientFill:', MB_OK);
    Result := False;
  end;
end;

procedure TForm1.FormPaint(Sender: TObject);
begin
  wGradientFill(Canvas.Handle, ClientRect, [clNavy, clLime, clYellow], fmVertiktal);
end;

end.


Alle Zeitangaben in WEZ +1. Es ist jetzt 08:48 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz