flutter开发桌面应用实现窗体透明

flutter开发桌面应用实现窗体透明

尽意
2024-09-21 / 0 评论 / 48 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2024年09月21日,已超过123天没有更新,若内容或图片失效,请留言反馈。
1.使用bitsdojo_window插件:

使用命令添加

dart pub add bitsdojo_window
2.在 windows/runner/main.cpp 中进行窗口透明设置:
#include <flutter/dart_project.h>
#include <flutter/flutter_view_controller.h>
#include <windows.h>

#include "flutter_window.h"
#include "utils.h"

// 添加的代码
#include<bitsdojo_window_windows/bitsdojo_window_plugin.h>
auto bdw = bitsdojo_window_configure(BDW_CUSTOM_FRAME | BDW_HIDE_ON_STARTUP);

void EnableTransparency(HWND hwnd) {
    SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_LAYERED);
    COLORREF colorKey = RGB(255, 255, 255);
    BYTE alpha = 128; // 设置透明度(0-256)
    SetLayeredWindowAttributes(hwnd, colorKey, alpha, LWA_ALPHA);
}
// 到这里结束

int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,
                      _In_ wchar_t *command_line, _In_ int show_command) {
  // Attach to console when present (e.g., 'flutter run') or create a
  // new console when running with a debugger.
//...省略中间的代码
  // 添加的代码
EnableTransparency(window.GetHandle());
// 到这里结束
window.SetQuitOnClose(true);

  ::MSG msg;
  while (::GetMessage(&msg, nullptr, 0, 0)) {
    ::TranslateMessage(&msg);
    ::DispatchMessage(&msg);
  }

  ::CoUninitialize();
  return EXIT_SUCCESS;
}
3.设置页面背景为透明:
Scaffold(
        backgroundColor: Colors.transparent,
        body: Container()
        )
4.实现的效果:

m1biwdk7.png

1

评论 (0)

取消