Here's the code:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
type
TForm1 = class(TForm)
procedure FormPaint(Sender: TObject);
private
procedure Test(P:HPen; Y:integer);
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Test(P:HPen; Y:integer);
var
R:TRect;
begin
R.Left:=300;
R.Right:=350;
R.Top:=Y+10;
R.Bottom:=Y+50;
Canvas.Pen.Handle:=P;
Canvas.MoveTo(10,Y+10);
Canvas.LineTo(100,Y+10);
Canvas.LineTo(120,Y+30);
Canvas.RoundRect(200, Y+10, 250, Y+50,50,50);
Canvas.FillRect(R);
end;
procedure TForm1.FormPaint(Sender: TObject);
var PH1, PH2: HPEN;
Style : cardinal;
Brush : TLogBrush;
W:integer;
begin
Style := PS_GEOMETRIC or PS_SOLID;
//Style := Style or PS_ENDCAP_SQUARE or PS_JOIN_MITER;
Style := Style or PS_ENDCAP_ROUND or PS_JOIN_ROUND;
Brush.lbStyle := BS_SOLID;
Brush.lbColor := RGB(255,0,0);
Brush.lbHatch := 0;
PH1:= ExtCreatePen(Style, 10, Brush, 0, nil);
Brush.lbColor := RGB(255, 128, 0);
PH2:= ExtCreatePen(Style, 5, Brush, 0, nil);
//Canvas.Brush.Color:= RGB(255,0,255);
Test(PH1, 0);
Test(PH2, 100);
//Brush.lbColor := RGB(255,0,0);
//PH1:= ExtCreatePen(Style, 10, Brush, 0, nil);
Test(PH1, 200);
end;
end.
You shouldn't really need to be fiddling with the internal handles of
ANY object. Usually ends up with a headache.
When assigning a value to the pen handle, the TPen class frees the
resource the current handle is pointing to.
Therefore: By assigning PH1, you free the original resource. After
that you assign PH2, you free the PH1's resource.
Have a look the TPen.SetHandle in Graphics.pas, and follow the trail
of funtions TResourceManager.ChangeResource()
I recommend you rather use the TPen class, and assigning its
properties using Canvas.Pen.Assign()
pen1 := TPen.Create; //remember to free this later
pen1.Width := 5;
pen1.Color := clBlue;
pen1.Style := etc etc
pen2 := TPen.Create; //remember to free this later
pen2.Width := 15;
pen2.Color := clLime;
pen2.Style := etc etc
Canvas.Pen.Assign(pen1);
//Do some drawing
Canvas.Pen.Assign(pen2);
//Do some drawing
Canvas.Pen.Assign(pen1);
//Do some drawing
Does this help?