Board index » delphi » help: changing pen/brush in paint proc

help: changing pen/brush in paint proc

I am having problems drawing different colored things on my component's
canvas. Basically I draw some squares defined by an array of points
(FPoints) which is turned into a TRect by PointRect.

I have one point defined as the selected one and I want that one to be
displayed differently. However changing the pen and brush does not seem
to have an effect as all squares (and joining lines) are drawn in the
same color.

I am using Delphi 5.

Below is the paint procedure.

--

 type

 TPolyLineSel = class(TGraphicControl)
 {...}
 protected
   procedure Paint; override;

procedure TPolyLineSel.Paint;
 var
  i: integer;
  tempBrush: TBrush;
  tempPen: TPen;
 begin
  FWorkingOnCanvas := true;

  if (csDesigning in ComponentState) then
    Canvas.Rectangle(Rect(0, 0, Width, Height));

  if assigned(OnPaintHandle) then
    for i := 0 to Points do
      OnPaintHandle(Self, i, FPoints[i].x, FPoints[i].y)
  else
  begin
    with Canvas do
    begin
      tempBrush := Brush;
      tempPen := Pen;

      { draw joining lines }
      if FJoinLines then
      begin
        Pen := FLine;
        if Points >= 0 then
          MoveTo(FPoints[0].x, FPoints[0].y);
        for i := 1 to Points do
          LineTo(FPoints[i].x, FPoints[i].y);
        { draw closing line }
        if (Points >= 0) and FClosePolygon then
          LineTo(FPoints[0].x, FPoints[0].y);
      end;

      { draw box handles }
      for i := 0 to Points do
      begin
        if i = FSelectedPoint then
        begin { selected box }
          Pen := FSelectOutline;
          Brush := FSelectFill;
        end
        else
        begin { normal box }
          Pen := FBoxOutline;
          Brush := FBoxFill;
        end;
        Rectangle(PointRect(FPoints[i], FHandleOffset));
      end;

      Brush :=tempBrush;
      Pen := tempPen;
    end;
  end;
  FWorkingOnCanvas := false;
 end;

Sent via Deja.com http://www.deja.com/
Before you buy.

 

Re:help: changing pen/brush in paint proc


<digitalprosper...@my-deja.com> skrev i melding
news:8f5l0l$757$1@nnrp1.deja.com...

Quote
> I have one point defined as the selected one and I want that one to be
> displayed differently. However changing the pen and brush does not seem
> to have an effect as all squares (and joining lines) are drawn in the
> same color.
[...]
>     with Canvas do
>     begin
>       tempBrush := Brush;
>       tempPen := Pen;

>       { draw joining lines }
>       if FJoinLines then
>       begin
>         Pen := FLine;

Hi !

If you examine TCanvas.Pen property, you'll se that it's "assymetric", so
that:

      tempBrush := Brush;

actually copies a *reference* to the TPen object into tempBrush, while

      Brush :=tempBrush;

...actually copies all the *data* of the tempBrush into the Brush object of
the Canvas. So, what you really do with your last line, is nothing....simply
assigns the brush object to itself. tempbrush holds a reference to the
Canvas.Brush object, which is the very same throughout your paint method.

I believe what you should do, is:

  TempBrush:=TBrush.Create;
  try
    TempBrush.Assign(Canvas.Brush);
// Do all painting
    Canvas.Brush:=TempBrush;
  finally
    TempBrush.Free;
  end;

--
Bjoerge Saether
Consultant / Developer
Asker, Norway
bsaether.removet...@online.no (remove the obvious)

Re:help: changing pen/brush in paint proc


Thanks. I had tried using Assign but that was worse as the pen and
brush were reset. I have tracked the problem down to the fact that I
had OnChange assigned for the pen and brush. Now it seems to work
better.

In article <CotR4.2312$bO4.40...@news1.online.no>,
  "Bj?rge S?ther" <REMOVE_bsaether@THIS_online.no> wrote:

Quote
> <digitalprosper...@my-deja.com> skrev i melding
> news:8f5l0l$757$1@nnrp1.deja.com...
> > I have one point defined as the selected one and I want that one to
be
> > displayed differently. However changing the pen and brush does not
seem
> > to have an effect as all squares (and joining lines) are drawn in
the
> > same color.

Sent via Deja.com http://www.deja.com/
Before you buy.

Other Threads