Graphics

Transparent text on desktop

Posted in Graphics

Use GetDesktopWindow and GetWindowDC functions to find attributes of desktop for further using. And set background mode by using GetBKMode function.

procedure TForm1.Button1Click(Sender: TObject);
var
  Desktop: THandle;
  MyCanvas: TCanvas;
  DesktopDC: HDC;
begin
  Desktop:=GetDesktopWindow;
  DesktopDC:=GetWindowDC(Desktop);

  MyCanvas:=TCanvas.Create;
  MyCanvas.Handle:=DesktopDC;

  SetBkMode(MyCanvas.Handle, TRANSPARENT);
  MyCanvas.TextOut(10,10, 'Hello');
end;

Set transparent color of image

Posted in Graphics

Image component has a Transparent property. Set this property to True and your background color will be the same as color of your form.

procedure TForm1.FormCreate(Sender: TObject);
begin
  Image1.Transparent:=True;
  Image1.Picture.LoadFromFile('picture.bmp');
end;

Set color of text and backgroud

Posted in Graphics

One of the ways to set text color and background color is using SetTextColor and SetBkColor function.

procedure TForm1.Button1Click(Sender: TObject);
begin
  SetTextColor(Form1.Canvas.Handle, clRed);
  SetBkColor(Form1.Canvas.Handle, clGray);
  SetBkMode(Form1.Canvas.Handle, OPAQUE);
  Form1.Canvas.TextOut(10, 10, 'Test Test Test Test Test Test');
end;

Zoom an icon

Posted in Graphics

You can't use StretchDraw function for this, because this function will return icon with original size. But you may solve this problem by using DrawIconEx function.

procedure TForm1.Button1Click(Sender: TObject);
begin
  DrawIconEx(
    Canvas.Handle,
    5,
    5,
    Application.Icon.Handle,
    50,
    50,
    1,
    0,
    DI_NORMAL);
end;

Illustrate brush styles

Posted in Graphics

Use Canvas method, which has a Brush property. This example shows all styles of Brush on mouse click.

procedure TForm1.FormClick(Sender: TObject);
begin
  with Canvas do
  begin
    Brush.Style:=bsSolid;
    Brush.Color:=clWhite;
    Rectangle(0,0,ClientWidth,ClientHeight);
    case Click1 of
      1: Brush.Style:=bsHorizontal;
      2: Brush.Style:=bsVertical;
      3: Brush.Style:=bsFDiagonal;
      4: Brush.Style:=bsBDiagonal;
      5: Brush.Style:=bsCross;
      6: Brush.Style:=bsDiagCross;
      7: Brush.Style:=bsSolid;
      8: Brush.Style:=bsClear;
    end;
    Brush.Color:=clRed;
    Rectangle(0,0,ClientWidth,ClientHeight);
  end;
  Inc(Click1);
  if Click1>8 then Click1:=1;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Click1:=1;
end;