Board index » delphi » How to dither a window color?

How to dither a window color?

Hello all,

Anybody knows how to dither the color of a window (Form / panel) or a
non-windowed control (TShape, TPaintbox) like in the TTabSet Win3.1
control's background?

Thanx in advance

Vijay

 

Re:How to dither a window color?


Use the Control.Canvas.Brush property.  The Brush has a bitmap property that
will do the trick.

Quote
Vijay Srinivas wrote in message <6nq94h$e...@forums.borland.com>...
>Hello all,

>Anybody knows how to dither the color of a window (Form / panel) or a
>non-windowed control (TShape, TPaintbox) like in the TTabSet Win3.1
>control's background?

>Thanx in advance

>Vijay

Re:How to dither a window color?


Hello,

You can use the folowing code that writes a dithered color if no exact color
found.

implementation

var
  DrawBitmap: TBitmap;
  UserCount: Integer;

procedure UsesBitmap;
begin
  if UserCount = 0 then
    DrawBitmap := TBitmap.Create;
  Inc(UserCount);
end;

procedure ReleaseBitmap;
begin
  Dec(UserCount);
  if UserCount = 0 then DrawBitmap.Free;
end;

procedure WriteText(ACanvas: TCanvas; ARect: TRect; DX, DY: Integer;
  const Text: string; Alignment: TAlignment);
const
  AlignFlags : array [TAlignment] of Integer =
    ( DT_LEFT or DT_WORDBREAK or DT_EXPANDTABS or DT_NOPREFIX,
      DT_RIGHT or DT_WORDBREAK or DT_EXPANDTABS or DT_NOPREFIX,
      DT_CENTER or DT_WORDBREAK or DT_EXPANDTABS or DT_NOPREFIX );
var
  B, R: TRect;
  I, Left: Integer;
begin
  I := ColorToRGB(ACanvas.Brush.Color);
  if GetNearestColor(ACanvas.Handle, I) = I then
  begin                       { Use ExtTextOut for solid colors }
    case Alignment of
      taLeftJustify:
        Left := ARect.Left + DX;
      taRightJustify:
        Left := ARect.Right - ACanvas.TextWidth(Text) - 3;
    else { taCenter }
      Left := ARect.Left + (ARect.Right - ARect.Left) shr 1
        - (ACanvas.TextWidth(Text) shr 1);
    end;
    ExtTextOut(ACanvas.Handle, Left, ARect.Top + DY, ETO_OPAQUE or
      ETO_CLIPPED, @ARect, PChar(Text), Length(Text), nil);
  end
  else begin                  { Use FillRect and Drawtext for dithered
colors }
    DrawBitmap.Canvas.Lock;
    try
      with DrawBitmap, ARect do { Use offscreen bitmap to eliminate flicker
and }
      begin                     { brush origin tics in painting /
olling.    }
        Width := Max(Width, Right - Left);
        Height := Max(Height, Bottom - Top);
        R := Rect(DX, DY, Right - Left - 1, Bottom - Top - 1);
        B := Rect(0, 0, Right - Left, Bottom - Top);
      end;
      with DrawBitmap.Canvas do
      begin
        Font := ACanvas.Font;
        Font.Color := ACanvas.Font.Color;
        Brush := ACanvas.Brush;
        Brush.Style := bsSolid;
        FillRect(B);
        SetBkMode(Handle, TRANSPARENT);
        DrawText(Handle, PChar(Text), Length(Text), R,
AlignFlags[Alignment]);
      end;
      ACanvas.CopyRect(ARect, DrawBitmap.Canvas, B);
    finally
      DrawBitmap.Canvas.Unlock;
    end;
  end;
end;

initialization

UsesBitmap;

Vijay Srinivas escribi en mensaje <6nq94h$e...@forums.borland.com>...

Quote
>Hello all,

>Anybody knows how to dither the color of a window (Form / panel) or a
>non-windowed control (TShape, TPaintbox) like in the TTabSet Win3.1
>control's background?

>Thanx in advance

>Vijay

Other Threads