//声明: SetLayeredWindowAttributes( Hwnd: THandle; {窗口句柄} crKey: COLORREF; {透明色} bAlpha: Byte; {Alpha 值} dwFlags: DWORD {LWA_COLORKEY(=1)表示使用透明色; LWA_ALPHA(=2)表示使用 Alpha 值} ): Boolean; {是否成功设置}
//举例(控制外部程序的透明度, 用计算器举了个例子): unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Button2: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} {设定计算器的 Alpha 透明} procedure TForm1.Button1Click(Sender: TObject); var h: HWND; FormStyle: Integer; begin h := FindWindow('SciCalc', nil); FormStyle := GetWindowLong(h, GWL_EXSTYLE); SetWindowLong(h, GWL_EXSTYLE, FormStyle or WS_EX_LAYERED); SetLayeredWindowAttributes(h, 0, 128, LWA_ALPHA); end; {设定计算器中的白色透明} procedure TForm1.Button2Click(Sender: TObject); var h: HWND; FormStyle: Integer; begin h := FindWindow('SciCalc', nil); FormStyle := GetWindowLong(h, GWL_EXSTYLE); SetWindowLong(h, GWL_EXSTYLE, FormStyle or WS_EX_LAYERED); SetLayeredWindowAttributes(h, clWhite, 255, LWA_COLORKEY); end; end.
WinAPI: SetLayeredWindowAttributes - 设置窗口的透明
这是来宾 Dolby 在 http://www.cnblogs.com/del/archive/2008/03/08/1081295.html#1096814 询问的问题.
//声明:
SetLayeredWindowAttributes(
Hwnd: THandle; {窗口句柄}
crKey: COLORREF; {透明色}
bAlpha: Byte; {Alpha 值}
dwFlags: DWORD {LWA_COLORKEY(=1)表示使用透明色; LWA_ALPHA(=2)表示使用 Alpha 值}
): Boolean; {是否成功设置}
//举例(控制外部程序的透明度, 用计算器举了个例子):
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{设定计算器的 Alpha 透明}
procedure TForm1.Button1Click(Sender: TObject);
var
h: HWND;
FormStyle: Integer;
begin
h := FindWindow('SciCalc', nil);
FormStyle := GetWindowLong(h, GWL_EXSTYLE);
SetWindowLong(h, GWL_EXSTYLE, FormStyle or WS_EX_LAYERED);
SetLayeredWindowAttributes(h, 0, 128, LWA_ALPHA);
end;
{设定计算器中的白色透明}
procedure TForm1.Button2Click(Sender: TObject);
var
h: HWND;
FormStyle: Integer;
begin
h := FindWindow('SciCalc', nil);
FormStyle := GetWindowLong(h, GWL_EXSTYLE);
SetWindowLong(h, GWL_EXSTYLE, FormStyle or WS_EX_LAYERED);
SetLayeredWindowAttributes(h, clWhite, 255, LWA_COLORKEY);
end;
end.
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Samples.Spin, Vcl.StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
SpinEdit1: TSpinEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
h: HWND;
FormStyle: Integer;
begin
h := FindWindow(PChar(Edit1.Text), nil);
FormStyle := GetWindowLong(h, GWL_EXSTYLE);
SetWindowLong(h, GWL_EXSTYLE, FormStyle or WS_EX_LAYERED);
SetLayeredWindowAttributes(h, 0, SpinEdit1.Value, LWA_ALPHA);
end;
end.
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 177
ClientWidth = 352
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Edit1: TEdit
Left = 8
Top = 8
Width = 121
Height = 21
TabOrder = 0
Text = 'Edit1'
end
object Button1: TButton
Left = 176
Top = 8
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 1
OnClick = Button1Click
end
object SpinEdit1: TSpinEdit
Left = 8
Top = 48
Width = 121
Height = 22
MaxValue = 0
MinValue = 0
TabOrder = 2
Value = 0
end
end