unit TransparentWnd;
interfaceuses
Windows, Messages, Classes, Controls, Forms;
type
_Percentage = 0..100;
TTransparentWnd = class(TComponent)
private{ Private declarations }protected{ Protected declarations }
_percent: _Percentage;
_auto: boolean;
User32: HMODULE;
public{ Public declarations }constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
//These work on a Handle//It doesn't change the Percent Property Value!procedure SetTransparentHWND(hwnd: THandle; percent : _Percentage);
//These work on the Owner (a TWinControl decendant is the Minumum)//They don't change the Percent Property Value!procedure SetTransparent; overload;
procedure SetTransparent(percent : _Percentage); overload;
procedure SetOpaqueHWND(hwnd : THandle);
procedure SetOpaque;
published{ Published declarations }//This works on the Owner (a TWinControl decendant is the Minumum)property Percent: _Percentage read _percent write _percent default 0;
property AutoOpaque: boolean read _auto write _auto default false;
end;
procedureregister;
implementationconst LWA_ALPHA = $2;
const GWL_EXSTYLE = (-20);
const WS_EX_LAYERED = $80000;
const WS_EX_TRANSPARENT = $20;
var
SetLayeredWindowAttributes: function (hwnd: LongInt; crKey: byte;
bAlpha: byte; dwFlags: LongInt): LongInt; stdcall;
constructor TTransparentWnd.Create(AOwner: TComponent);
begininherited;
User32 := LoadLibrary('USER32.DLL');
if User32 <> 0 then
@SetLayeredWindowAttributes := GetProcAddress(User32, 'SetLayeredWindowAttributes')
else
SetLayeredWindowAttributes := nil;
end;
destructor TTransparentWnd.Destroy;
beginif User32 <> 0 then
FreeLibrary(User32);
inherited;
end;
procedure TTransparentWnd.SetOpaqueHWND(hwnd: THandle);
var
old: THandle;
beginif IsWindow(hwnd) thenbegin
old := GetWindowLongA(hwnd,GWL_EXSTYLE);
SetWindowLongA(hwnd, GWL_EXSTYLE, old and ((not 0)-WS_EX_LAYERED));
end;
end;
procedure TTransparentWnd.SetOpaque;
begin
Self.SetOpaqueHWND((Self.Owner as TWinControl).Handle);
end;
procedure TTransparentWnd.SetTransparent;
begin
Self.SetTransparentHWND((Self.Owner as TWinControl).Handle, Self._percent);
end;
procedure TTransparentWnd.SetTransparentHWND(hwnd: THandle; percent : _Percentage);
var
old: THandle;
beginif (User32 <> 0) and (Assigned(SetLayeredWindowAttributes)) and (IsWindow(hwnd)) thenif (_auto=true) and (percent=0) then
SetOpaqueHWND(hwnd)
elsebegin
percent := 100 - percent;
old := GetWindowLongA(hwnd, GWL_EXSTYLE);
SetWindowLongA(hwnd, GWL_EXSTYLE, old or WS_EX_LAYERED);
SetLayeredWindowAttributes(hwnd, 0, (255 * percent) div 100, LWA_ALPHA);
end;
end;
procedure TTransparentWnd.SetTransparent(percent: _Percentage);
begin
Self.SetTransparentHWND((Self.Owner as TForm).Handle, percent);
end;
procedureregister;
begin
RegisterComponents('Win32', [TTransparentWnd]);
end;
end.
Это компонент, для Дельфи, инкапсулирующий нужные функции
Статья Полупрозрачная форма в Win2000 раздела Программа и Интерфейс Интерфейс может быть полезна для разработчиков на Delphi и FreePascal.
Комментарии и вопросы
Материалы статей собраны из открытых источников, владелец сайта не претендует на авторство. Там где авторство установить не удалось, материал подаётся без имени автора. В случае если Вы считаете, что Ваши права нарушены, пожалуйста, свяжитесь с владельцем сайта.