FireMonkey中的AlphaBlend

时间:2013-12-20 08:14:22

标签: delphi firemonkey alphablending delphi-xe5

如何更改FireMonkey桌面应用程序中的AlphaBlend值(表单)?嗯它可以在VCL应用程序中使用,但我在FireMonkey中找不到它。

截图:

enter image description here

2 个答案:

答案 0 :(得分:9)

要使表单背景为半透明,您应将表单Transparency属性设置为true,并使用Fill.Color,其值为$AAFFFFFF(带Fill.Kind = bkSolid)。 在这种情况下,表格边框变得不可见(至少在Delphi XE2 中)

如果您需要以半透明的形式制作所有组件,请将TLayout放在包含Align = alContents的表单上,并将其Opacity属性设置为所需的值。

enter image description here

如果你需要像在VCL中那样使用alpha混合的半透明窗口,你可以使用与getWindowLong/SetWindowLong相同的方法(对于Windows平台)。将transparency设置回false并在表单OnCreate事件处理程序中使用此类代码:

implementation
uses fmx.platform.win, winapi.windows;
{$R *.fmx}

procedure TMainForm.FormCreate(Sender: TObject);
var h : HWND;
    aStyle : integer;
    alphaValue : byte;
begin
    h := WindowHandleToPlatform(self.Handle).Wnd;
    AStyle := GetWindowLong(h, GWL_EXSTYLE);
    SetWindowLong(h, GWL_EXSTYLE, AStyle or WS_EX_LAYERED);


    AlphaValue := 125;
    SetLayeredWindowAttributes(h, 0, alphaValue, LWA_ALPHA);
end;

当然所有组件也变得很透明。

答案 1 :(得分:6)

由于您只查看Windows实现,因此必须将WS_EX_LAYERED样式添加到表单中,然后使用SetLayeredWindowAttributes方法设置基于值或颜色的Alpha值。

使用插入器类检查此实现。

type
  TForm   = class(FMX.Forms.TForm)
  private
    FAlphaBlend: Boolean;
    FAlphaBlendValue: Byte;
    FTransparentColor: Boolean;
    FTransparentColorValue: TColor;
    procedure SetAlphaBlend(const Value: Boolean);
    procedure SetAlphaBlendValue(const Value: Byte);
    procedure SetLayeredAttribs;
    procedure SetTransparentColor(const Value: Boolean);
    procedure SetTransparentColorValue(const Value: TColor);
  protected
    property AlphaBlend: Boolean read FAlphaBlend write SetAlphaBlend  default False;
    property AlphaBlendValue: Byte read FAlphaBlendValue write SetAlphaBlendValue default 255;
    property TransparentColor: Boolean read FTransparentColor write SetTransparentColor default False;
    property TransparentColorValue: TColor read FTransparentColorValue write SetTransparentColorValue;
  end;

  TForm1 = class(TForm)
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;


var
  Form1: TForm1;

implementation

{$R *.fmx}

uses
 FMX.Platform.Win,
 Winapi.Windows,
 Vcl.Graphics;

type
  TSetLayeredWindowAttributes = function (Hwnd: THandle; crKey: COLORREF;
    bAlpha: Byte; dwFlags: DWORD): Boolean; stdcall;

var
  SetLayeredWindowAttributes: TSetLayeredWindowAttributes = nil;

procedure TForm1.FormCreate(Sender: TObject);
begin
   @SetLayeredWindowAttributes := GetProcAddress(GetModuleHandle(User32), 'SetLayeredWindowAttributes');
   AlphaBlend:=True;
   AlphaBlendValue:=200;
end;

{ TForm }

procedure TForm.SetAlphaBlend(const Value: Boolean);
begin
  if FAlphaBlend <> Value then
  begin
    FAlphaBlend := Value;
    SetLayeredAttribs;
  end;
end;

procedure TForm.SetAlphaBlendValue(const Value: Byte);
begin
  if FAlphaBlendValue <> Value then
  begin
    FAlphaBlendValue := Value;
    SetLayeredAttribs;
  end;
end;

procedure TForm.SetTransparentColor(const Value: Boolean);
begin
  if FTransparentColor <> Value then
  begin
    FTransparentColor := Value;
    SetLayeredAttribs;
  end;
end;

procedure TForm.SetTransparentColorValue(const Value: TColor);
begin
  if FTransparentColorValue <> Value then
  begin
    FTransparentColorValue := Value;
    SetLayeredAttribs;
  end;
end;

procedure TForm.SetLayeredAttribs;
const
  cUseAlpha: array [Boolean] of Integer = (0, LWA_ALPHA);
  cUseColorKey: array [Boolean] of Integer = (0, LWA_COLORKEY);
var
  AStyle: Integer;
begin
  if not (csDesigning in ComponentState) and
    (Assigned(SetLayeredWindowAttributes))  then
  begin

    AStyle := GetWindowLong( WindowHandleToPlatform(Handle).Wnd, GWL_EXSTYLE);
    if FAlphaBlend or FTransparentColor then
    begin
      if (AStyle and WS_EX_LAYERED) = 0 then
        SetWindowLong(WindowHandleToPlatform(Handle).Wnd, GWL_EXSTYLE, AStyle or WS_EX_LAYERED);
      SetLayeredWindowAttributes(WindowHandleToPlatform(Handle).Wnd, ColorToRGB(FTransparentColorValue), FAlphaBlendValue,
        cUseAlpha[FAlphaBlend] or cUseColorKey[FTransparentColor]);
    end
    else
    begin
      SetWindowLong(WindowHandleToPlatform(Handle).Wnd, GWL_EXSTYLE, AStyle and not WS_EX_LAYERED);
      RedrawWindow(WindowHandleToPlatform(Handle).Wnd, nil, 0, RDW_ERASE or RDW_INVALIDATE or RDW_FRAME or RDW_ALLCHILDREN);
    end;
  end;
end;

enter image description here