如何制作半透明的窗体?

czq7 2001-07-19 02:54:59
在Win2K+D6环境下可直接制作出半透明的窗体,并且其透明度可调,那么在Win2K+D5下有戏吗?
...全文
136 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
cdchq 2001-12-03
  • 打赏
  • 举报
回复
标记
czq7 2001-07-19
  • 打赏
  • 举报
回复
楼上的方法,我也试过,在9x下报错。我同意li_zhifu的方法。不过得谢谢楼上的解答!
kuangning 2001-07-19
  • 打赏
  • 举报
回复
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;
Const
WS_EX_LAYERED = $80000;
LWA_ALPHA = 2;

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.
li_zhifu 2001-07-19
  • 打赏
  • 举报
回复

type
TSetLayeredWindowAttributes = function (hwnd:HWND; crKey:Longint; bAlpha:byte; dwFlags:longint ):longint; stdcall;

var
Form1: TForm1;
SetLayeredWindowAttributes: TSetLayeredWindowAttributes = nil;

...

procedure InitProcs;
const
sUser32 = 'User32.dll';
var
ModH: HMODULE;
begin
ModH := GetModuleHandle(sUser32);
if ModH <> 0 then
@SetLayeredWindowAttributes := GetProcAddress(ModH, 'SetLayeredWindowAttributes');
end;

procedure TForm1.FormCreate(Sender: TObject);
var AStyle:Integer;
begin
InitProcs;
Alpha:=150;//0为完全透明,255完全不透明
AStyle := GetWindowLong(Handle, GWL_EXSTYLE);
if (AStyle and $00080000) = 0 then
SetWindowLong(Handle, GWL_EXSTYLE, AStyle or $00080000);
if assigned(SetLayeredWindowAttributes) then
SetLayeredWindowAttributes(Handle,0,Alpha,$2);
end;

用这种方法可以在Win2K中显示半透明的窗体,而在Win9X中不透明,但也不会报Dll错

5,392

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧