How to create a brush using CreateBrushIndirect #98

Drop a TPaintBox control and a TButton on a form and add the following code to the button's OnClick event handler.

Run the application and see a blue hatched ellipse drawn in the paint box.

procedure TForm1.Button1Click(Sender: TObject);
var
  Region: HRGN;
  LogBrush: TLogBrush;
  NewBrush: hBrush;
begin
  with LogBrush do
  begin
    lbStyle := BS_HATCHED;
    lbColor := clBlue;
    lbHatch := HS_CROSS
  end;
  NewBrush := CreateBrushIndirect(LogBrush);
  Region := CreateEllipticRgnIndirect(PaintBox1.ClientRect);
  FillRgn(PaintBox1.Canvas.Handle, Region, NewBrush);
  DeleteObject(NewBrush);
  DeleteObject(Region)
end;

Tip modified and expanded by Peter Johnson.

Original resource: The Delphi Pool
Author: Earl F. Glynn
Added: 2009/09/14
Last updated: 2009/09/14