| 導購 | 订阅 | 在线投稿
分享
 
 
 

用DELPHI實現透明以及半透明窗體

來源:互聯網網民  2006-06-07 04:32:35  評論

在QQ中,我可以調節面板的透明度。如果我們自己編程,應該如何實現呢?

(一)半透明窗體

在windows2000以及後續産品中下增加了一些API,可以輕易的實現半透明的窗體,源程序如下:

interface

uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

const//定義常量

WS_EX_LAYERED = $80000;

AC_SRC_OVER = $0;

AC_SRC_ALPHA = $1;

AC_SRC_NO_PREMULT_ALPHA = $1;

AC_SRC_NO_ALPHA = $2;

AC_DST_NO_PREMULT_ALPHA = $10;

AC_DST_NO_ALPHA = $20;

LWA_COLORKEY = $1;

LWA_ALPHA = $2;

ULW_COLORKEY = $1

ULW_ALPHA = $2

ULW_OPAQUE = $4

type

TForm1 = class(TForm)

procedure FormCreate(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

function SetLayeredWindowAttributes(hwnd:HWND; crKey:Longint; bAlpha:byte; dwFlags:longint ):longint; stdcall; external user32;//函數聲明

var

Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);

var l:longint;

begin

l:=getWindowLong(Handle, GWL_EXSTYLE);

l := l Or WS_EX_LAYERED;

SetWindowLong (handle, GWL_EXSTYLE, l);

SetLayeredWindowAttributes (handle, 0, 180, LWA_ALPHA);

//第二個參數是指定透明顔色

//第二個參數爲0則使用第四個參數設置alpha值,從0到255

end;

end.

(二) 透明窗體的實現

將一個FORM變成透明的實質性手段就是攔截CMEraseBkgnd消息。

unit Utransform; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls;

type TForm1 = class(TForm)

private { Private declarations }

public { Public declarations }

PROCEDURE CMEraseBkgnd(var Message:TWMEraseBkgnd);Message WM_ERASEBKGND;

end;

var Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.CMEraseBkgnd(var Message: TWMEraseBkgnd);

begin

brush.style:=bsClear;

end;

procedure TForm1.FormCreate(Sender: TObject);

begin

Form1.Brush.Style := bsClear;//如果只攔截消息,沒有這裏的設置,窗內的標題欄一樣會顯示 Form1.BorderStyle := bsNone

end;

procedure TForm1.BitBtn1Click(Sender: TObject);

begin

close;

end;

 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
在QQ中,我可以調節面板的透明度。如果我們自己編程,應該如何實現呢? (一)半透明窗體 在windows2000以及後續産品中下增加了一些API,可以輕易的實現半透明的窗體,源程序如下: interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs; const//定義常量 WS_EX_LAYERED = $80000; AC_SRC_OVER = $0; AC_SRC_ALPHA = $1; AC_SRC_NO_PREMULT_ALPHA = $1; AC_SRC_NO_ALPHA = $2; AC_DST_NO_PREMULT_ALPHA = $10; AC_DST_NO_ALPHA = $20; LWA_COLORKEY = $1; LWA_ALPHA = $2; ULW_COLORKEY = $1 ULW_ALPHA = $2 ULW_OPAQUE = $4 type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; function SetLayeredWindowAttributes(hwnd:HWND; crKey:Longint; bAlpha:byte; dwFlags:longint ):longint; stdcall; external user32;//函數聲明 var Form1: TForm1; implementation {$R *.DFM} procedure TForm1.FormCreate(Sender: TObject); var l:longint; begin l:=getWindowLong(Handle, GWL_EXSTYLE); l := l Or WS_EX_LAYERED; SetWindowLong (handle, GWL_EXSTYLE, l); SetLayeredWindowAttributes (handle, 0, 180, LWA_ALPHA); //第二個參數是指定透明顔色 //第二個參數爲0則使用第四個參數設置alpha值,從0到255 end; end. (二) 透明窗體的實現 將一個FORM變成透明的實質性手段就是攔截CMEraseBkgnd消息。 unit Utransform; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls; type TForm1 = class(TForm) private { Private declarations } public { Public declarations } PROCEDURE CMEraseBkgnd(var Message:TWMEraseBkgnd);Message WM_ERASEBKGND; end; var Form1: TForm1; implementation {$R *.DFM} procedure TForm1.CMEraseBkgnd(var Message: TWMEraseBkgnd); begin brush.style:=bsClear; end; procedure TForm1.FormCreate(Sender: TObject); begin Form1.Brush.Style := bsClear;//如果只攔截消息,沒有這裏的設置,窗內的標題欄一樣會顯示 Form1.BorderStyle := bsNone end; procedure TForm1.BitBtn1Click(Sender: TObject); begin close; end;
󰈣󰈤
王朝萬家燈火計劃
期待原創作者加盟
 
 
 
>>返回首頁<<
 
 
 
 
 
 熱帖排行
 
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有