2004. július 20., kedd

Create a round form


Problem/Question/Abstract:

How can I make a transparent or solid round form, without caption and border?

Answer:

This is a complete example of how to make a round form. Do not forget to create a TButton to close the window
  
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls,
  Forms, Dialogs, ExtCtrls, Buttons, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen}
    procedure CreateParams(var Params: TCreateParams); override;
  public
    { Public-Deklarationen}
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

{ TForm1 }

procedure TForm1.CreateParams(var Params: TCreateParams);
begin
  inherited CreateParams(Params);

  {Remove caption and border}
  Params.Style := Params.Style or ws_popup xor ws_dlgframe;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  FormRgn: hRgn;
begin
  {clear form}
  Form1.Brush.Style := bsSolid; //bsclear;
  {make form round}
  GetWindowRgn(Form1.Handle, FormRgn);

  { delete the old object }
  DeleteObject(FormRgn);
  { make the form rectangular }
  Form1.Height := 500;
  Form1.Width := Form1.Height;
  { create the round form }
  FormRgn := CreateRoundRectRgn(1, 1, Form1.Width - 1,
    Form1.height - 1, Form1.width, Form1.height);

  { set the new round window }
  SetWindowRgn(Form1.Handle, FormRgn, TRUE);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Form1.close;
end;

end.

Nincsenek megjegyzések:

Megjegyzés küldése