FireMonkey中的AlphaBlend

Sky*_*Sky 5 delphi alphablending firemonkey delphi-xe5

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

截图:

在此输入图像描述

ter*_*ran 9

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

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

在此输入图像描述

如果您需要半透明窗口与alpha混合,就像在VCL中一样,您可以使用相同的方法(对于Windows平台)getWindowLong/SetWindowLong.设置transparencyfalse和形式使用这样的代码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;
Run Code Online (Sandbox Code Playgroud)

当然,你所有的组件也变得很透明.


RRU*_*RUZ 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;
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述