You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++Builder FireMonkey透明窗体问题:透明区域显示黑色

C++Builder FireMonkey 透明窗体解决思路

1. 分平台适配核心逻辑

FireMonkey的Transparency属性在不同平台上生效逻辑差异很大,从你添加的Android清单配置来看,先聚焦Android平台的解决步骤:

2. 修正Android主题配置

你使用的Theme.Translucent.NoTitleBar是旧版Android主题,API 21+的系统需要改用兼容的自定义主题:

  • 在C++Builder的清单文件中替换为自定义主题引用:
    SetActivityAttribute(YourFormClass, android:theme, "@style/CustomTransparentTheme");
    
  • 在项目的res/values/styles.xml中定义透明主题:
    <style name="CustomTransparentTheme" parent="android:Theme.Material.Light.NoActionBar">
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowActionBar">false</item>
    </style>
    

3. FireMonkey窗体关键设置

  • 除了Transparency=true,还要将窗体BackgroundColor设为全透明:
    YourForm->BackgroundColor = TAlphaColors::Null;
    
  • 确保窗体FormStyle设为fsNormal,避免全屏、对话框等样式冲突。

4. Android原生层强制清除背景

FireMonkey在Android上可能会默认填充黑色背景,需要通过JNI调用原生API清除:

#include <Androidapi.JNI.GraphicsContentViewText.hpp>
#include <Androidapi.Helpers.hpp>

void __fastcall TYourForm::FormCreate(TObject *Sender)
{
    JNIEnv* env = TAndroidHelper::JNIEnv;
    jobject activity = TAndroidHelper::Activity;
    jobject window = activity->GetWindow();
    jclass windowClass = env->GetObjectClass(window);
    jmethodID setBgMethod = env->GetMethodID(windowClass, "setBackgroundDrawable", "(Landroid/graphics/drawable/Drawable;)V");
    env->CallVoidMethod(window, setBgMethod, NULL);
    env->DeleteLocalRef(windowClass);
}

5. Windows平台补充(若需兼容)

如果同时需要Windows平台生效:

  • 开启窗体AlphaBlend=true,AlphaBlendValue=255
  • 处理WM_ERASEBKGND消息避免黑色背景:
    void __fastcall TYourForm::WndProc(TMessage &Message)
    {
        if (Message.Msg == WM_ERASEBKGND)
        {
            Message.Result = 1;
            return;
        }
        TForm::WndProc(Message);
    }
    

内容的提问来源于stack exchange,提问作者neg1414

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 08:56:08