quarta-feira, 6 de junho de 2012

Form com área transparente no Lazarus/Windows

Recentemente precisei criar um form com área interna transparente no Lazarus. Pesquisando no Google, encontrei uma dica para Delphi que foi facilmente adaptado para meu caso.


unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, Spin, Windows;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
SpinEdit1: TSpinEdit;
SpinEdit2: TSpinEdit;
SpinEdit3: TSpinEdit;
SpinEdit4: TSpinEdit;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormResize(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
procedure NewWindowRgn;
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
// Area tranparente
// Adaptado a partir de "Get transparent form - Forms - Tips & Tricks - Greatis Delphi Pages"
// http://www.greatis.com/delphicb/tips/lib/forms-transpform.html
procedure TForm1.NewWindowRgn;
var
FormRgn, NewRgn: THandle;
begin
FormRgn := CreateRectRgn(0, 0, Screen.Width, Screen.Height);
NewRgn:= CreateRectRgn(
SpinEdit1.Value,
SpinEdit2.Value,
SpinEdit3.Value,
SpinEdit4.Value);
CombineRgn(FormRgn, FormRgn, NewRgn, RGN_DIFF);
SetWindowRgn(Handle, FormRgn, True);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
HorzScrollBar.Visible:=False;
VertScrollBar.Visible:=False;
NewWindowRgn;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
NewWindowRgn;
end;
procedure TForm1.FormResize(Sender: TObject);
begin
NewWindowRgn;
end;
end.
view raw gistfile1.pas hosted with ❤ by GitHub

Infelizmente esta dica funciona apenas no Windows (não esquecer de incluir a unidade Windows na cláusula Uses).

Adaptado a partir de Get transparent form - Forms - Tips & Tricks - Greatis Delphi Pages.

[]'s,

Ericson Benjamim.