首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AlphaBlend in FireMonkey

AlphaBlend in FireMonkey
EN

Stack Overflow用户
提问于 2013-12-20 08:14:21
回答 2查看 5K关注 0票数 6

如何在AlphaBlend桌面应用程序中更改(窗体的) FireMonkey值?它在VCL应用程序中是可用的,但我在FireMonkey中找不到它。

截图:

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-12-20 09:46:42

要使您的表单背景半透明,您应该将form Transparency属性设置为true,并使用带有alpha值的Fill.Color (与Fill.Kind = bkSolid)一样。在这种情况下,表单边框变得不可见(至少在Delphi XE2中是如此)

如果您需要使所有组件在窗体半透明,然后将TLayoutAlign = alContents放在表单上,并将其Opacity属性设置为所需的值。

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

代码语言:javascript
复制
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;

当然,您的所有组件也成为了转换。

票数 9
EN

Stack Overflow用户

发布于 2013-12-20 16:10:52

因为您只查找WS_EX_LAYERED实现,所以必须将SetLayeredWindowAttributes样式添加到窗体中,然后使用SetLayeredWindowAttributes方法根据值或颜色设置alpha值。

使用interposer类检查此实现。

代码语言:javascript
复制
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;

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20699032

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档