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

Flutter Windows应用禁止截图实现方案咨询

Flutter Windows应用禁止截图的实现方案

核心结论

Flutter框架本身没有提供跨平台的防截图API,因为这属于系统级的窗口属性控制,必须通过Windows原生代码实现。目前没有成熟的通用插件,建议基于Windows原生API自行封装。

Windows原生实现方案

Windows 10 1809及以上版本提供了SetWindowDisplayAffinity API,可以直接标记窗口禁止被截图或录屏,这是最官方的解决方案。

1. 静态全局禁止(窗口启动即生效)

修改Flutter Windows项目的windows/runner/main.cpp文件,在窗口创建完成后调用API:

#include <windows.h>

// 封装防截图开关函数
void SetWindowCaptureBlock(HWND hwnd, bool block) {
    if (IsWindows101809OrHigher()) {
        DWORD affinity = block ? WDA_EXCLUDEFROMCAPTURE : WDA_NONE;
        SetWindowDisplayAffinity(hwnd, affinity);
    }
}

// 检查Windows版本是否满足要求(10.0.17763及以上)
bool IsWindows101809OrHigher() {
    OSVERSIONINFOEX osvi;
    ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
    osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
    osvi.dwMajorVersion = 10;
    osvi.dwMinorVersion = 0;
    osvi.dwBuildNumber = 17763;

    DWORDLONG conditionMask = 0;
    VER_SET_CONDITION(conditionMask, VER_MAJORVERSION, VER_GREATER_EQUAL);
    VER_SET_CONDITION(conditionMask, VER_MINORVERSION, VER_GREATER_EQUAL);
    VER_SET_CONDITION(conditionMask, VER_BUILDNUMBER, VER_GREATER_EQUAL);

    return VerifyVersionInfo(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_BUILDNUMBER, conditionMask);
}

然后在窗口创建逻辑中调用(比如FlutterWindow::CreateAndShow方法内):

bool FlutterWindow::CreateAndShow(const std::wstring& title,
                                  const Point& origin,
                                  const Size& size) {
  // ... 保留原有窗口创建代码 ...

  // 窗口初始化完成后开启防截图
  SetWindowCaptureBlock(GetHandle(), true);

  return true;
}

2. 动态控制(Flutter端调用)

如果需要在Flutter界面中动态开启/关闭防截图,可以通过MethodChannel实现Flutter与原生的交互:

原生端注册MethodChannel

在main.cpp中添加通道注册逻辑:

#include <flutter/method_channel.h>
#include <flutter/plugin_registrar_windows.h>
#include <flutter/standard_method_codec.h>

void RegisterCaptureBlockChannel(flutter::PluginRegistrarWindows* registrar) {
  auto channel = std::make_unique<flutter::MethodChannel<flutter::EncodableValue>>(
      registrar->messenger(), "your_app/capture_block",
      &flutter::StandardMethodCodec::GetInstance());

  channel->SetMethodCallHandler([registrar](const auto& call, auto result) {
    if (call.method_name() == "setCaptureBlock") {
      bool block = std::get<bool>(*call.arguments());
      HWND hwnd = registrar->GetView()->GetNativeWindow();
      SetWindowCaptureBlock(hwnd, block);
      result->Success();
    } else {
      result->NotImplemented();
    }
  });
}

然后在wWinMain函数中注册该通道:

int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,
                      _In_ wchar_t* command_line, _In_ int show_command) {
  // ... 原有代码 ...

  flutter::PluginRegistrarWindows* registrar = registrar_ref.get();
  RegisterCaptureBlockChannel(registrar);

  // ... 原有代码 ...
}
Flutter端调用方法
import 'package:flutter/services.dart';

class CaptureController {
  static const _channel = MethodChannel('your_app/capture_block');

  static Future<void> setCaptureBlock(bool block) async {
    await _channel.invokeMethod('setCaptureBlock', block);
  }
}

// 使用示例:在需要的地方调用
// CaptureController.setCaptureBlock(true);
// CaptureController.setCaptureBlock(false);

注意事项

  • 该API仅支持Windows 10 1809及以上版本,低版本系统无法生效,建议在代码中做版本判断并给出提示。
  • 此方法可以阻止系统自带的截图工具(Win+PrintScreen、Snip & Sketch等)和大部分录屏软件,但无法完全阻止所有第三方截图工具(比如通过底层Hook实现的工具)。
  • 若需要更严格的防截图控制,可结合窗口透明化、像素混淆等额外手段,但会增加复杂度。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 09:01:35