function SetLayeredWindowAttributes(Handle: HWND;
COLORKEY: COLORREF; Alpha: BYTE; Flags: DWord): Boolean; stdcall; external 'USER32.DLL';
Const
WS_EX_LAYERED = $80000;
LWA_ALPHA = 2;
我們調(diào)用GetWindowLong函數(shù)獲取當(dāng)前窗口的擴(kuò)展屬性,并調(diào)用SetWindowLong函數(shù)將新的WS_EX_LAYERED窗口擴(kuò)展屬性添加進(jìn)去。
PRocedure TForm1.FormCreate(Sender: TObject);
begin
SetWindowLong(Handle,GWL_EXSTYLE,GetWindowLong(Handle,GWL_EXSTYLE) or WS_EX_LAYERED);
end;
現(xiàn)在我們的窗口已經(jīng)可以調(diào)用SetLayeredWindowAttributes函數(shù),通過設(shè)置該函數(shù)的Alpha參數(shù),我們就可以看到窗口的效果的變化。
procedure TForm1.Button1Click(Sender: TObject);
begin
SetLayeredWindowAttributes(Form1.Handle, 0, 180, LWA_ALPHA);
end;
以下的VCL控件代碼封裝了SetLayeredWindowAttributes函數(shù),編程時(shí)動(dòng)態(tài)改變AlphaValue值,您就可以看到窗口的透明效果了。控件屏蔽了設(shè)計(jì)期的顯示效果,如果讀者愿意可以改為設(shè)計(jì)期效果可見,不過那樣的話,一不小心,您可能就會(huì)找不著你要設(shè)計(jì)的窗體了 8-)
unit TranForm; {DragonPC 2001.2.21 }
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms ;
type
TTranForm = class(TComponent)
private
FAlphaValue : integer ;
FParentFormHandle : HWND ;
procedure SetFAlphaValue(Alpha:integer) ;
protected
procedure UpdateDisplay ;
public
constructor Create(AOwner: TComponent); override;
published
property AlphaValue : integer read FAlphaValue write SetFAlphaValue ;
end;
procedure Register;
function SetLayeredWindowAttributes(Handle: HWND; COLORKEY: COLORREF;
Alpha: BYTE; Flags: DWORD): Boolean; stdcall; external 'USER32.DLL';
implementation
procedure Register;
begin
RegisterComponents('Standard', [TTranForm]);
end;
procedure TTranForm.SetFAlphaValue(Alpha: integer);
begin
if (Alpha >= 0) and (Alpha < 256) then begin
FAlphaValue := Alpha ;
UpdateDisplay() ;
end;
end;
procedure TTranForm.UpdateDisplay;
begin
if (csDesigning in ComponentState) then Exit ;
SetLayeredWindowAttributes(FParentFormHandle, 0, FAlphaValue, 2);
end;
constructor TTranForm.Create(AOwner: TComponent);
begin
inherited;
if (csDesigning in ComponentState) then Exit;
FAlphaValue := 255 ;
FParentFormHandle := TForm(AOwner).Handle ;
SetWindowLong(FParentFormHandle,
GWL_EXSTYLE,
GetWindowLong(FParentFormHandle, GWL_EXSTYLE) or WS_EX_LAYERED);
end;
end.
謝謝您的莫大耐心得以看完此文,此文純粹本著賺取china-pub"假幣"目的所寫,萬一各位看官有任何的疑問,歡迎信至dragonpc@21cn.com,謝謝。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注