2013-12-20 8 views
5

Wie kann ich AlphaBlend Wert (eine Form) in einer Firemonkey-Desktop-Anwendung ändern? Nun, es ist in VCL Anwendung zur Verfügung, aber ich konnte es nicht finden in Firemonkey.Alphablend in Firemonkey

Screenshot:

enter image description here

+1

Check 'Transparency' (kann useTransparancy sein) und verwenden' Fill.Color' mit Alpha-Kanal Wert – teran

+0

Versuchen Sie, die Transparenz eines Formulars zu ändern? Können Sie ein Bild des gewünschten Effekts liefern? –

+0

Sie suchen eine plattformübergreifende Lösung? oder nur für Windows? – RRUZ

Antwort

7

Um Ihre Form Hintergrund machen semitransparent sollten Sie Form Transparency Eigenschaft true und verwenden Fill.Color mit Alpha-Wert wie $AAFFFFFF (mit Fill.Kind = bkSolid) gesetzt. in diesem Fall Form Grenze wird unsichtbar (zumindest in Delphi XE2 )

, wenn Sie alle Komponenten in Form halbtransparent machen müssen dann TLayout auf Formular platzieren mit Align = alContents und legen Sie seine Opacity Eigenschaft auf gewünschten Wert einstellen.

enter image description here

, wenn Sie halbtransparente Fenster mit Alpha-Blending müssen, wie es in VCL war können Sie die gleichen Methoden verwenden (für Windows-Plattform) als getWindowLong/SetWindowLong. Set transparency zurück zu false und verwenden Sie Code wie folgt in Form OnCreate Ereignishandler:

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; 

natürlich alle Komponenten werden zu trasparent.

+0

Dank. Aber das größte Problem sind die Formulargrenzen. Sie werden unsichtbar! Wie kann ich das beheben? – Sky

+0

@Sky Ich habe ein kleines Update über Grenzen hinzugefügt; – teran

+0

ok, @RRUZ hat bereits den Code von VCL gepostet, ich bin ein bisschen spät :) – teran

6

Da Sie nur nach einer Windows-Implementierung suchen, müssen Sie das Format WS_EX_LAYERED zu Ihrem Formular hinzufügen und dann mit der Methode SetLayeredWindowAttributes den Alpha-Wert basierend auf einem Wert oder einer Farbe festlegen.

prüfen diese Implementierung, eine Interposer-Klasse.

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

+0

Haben Sie versucht, den ColorKey selbst zu benutzen? Während die Verwendung von LWA_ALPHA für mich gut funktioniert, kann ich den anderen Teil nicht zum Laufen bringen. Das Übergeben des ColorKey (und das Definieren von LWA_COLORKEY) scheint keine Wirkung zu haben. – DNR

Verwandte Themen