如何修改C++启动代码将Flutter Windows程序设为无标题栏全屏?
实现Flutter Windows端无标题栏全屏模式(游戏风格)
别担心,我帮你一步步修改这段C启动代码,完全不用你懂C,直接替换对应的部分就行,实现你想要的无标题栏全屏效果:
修改后的完整代码
#include <flutter/dart_project.h> #include <flutter/flutter_view_controller.h> #include <windows.h> #include "flutter_window.h" #include "run_loop.h" #include "utils.h" 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. if (!::AttachConsole(ATTACH_PARENT_PROCESS) && ::IsDebuggerPresent()) { CreateAndAttachConsole(); } // Initialize COM, so that it is available for use in the library and/or // plugins. ::CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED); RunLoop run_loop; flutter::DartProject project(L"data"); std::vector<std::string> command_line_arguments = GetCommandLineArguments(); project.set_dart_entrypoint_arguments(std::move(command_line_arguments)); FlutterWindow window(&run_loop, project); // 获取当前屏幕的完整尺寸 int screen_width = GetSystemMetrics(SM_CXSCREEN); int screen_height = GetSystemMetrics(SM_CYSCREEN); // 设置窗口从屏幕左上角开始,大小铺满整个屏幕 Win32Window::Point origin(0, 0); Win32Window::Size size(screen_width, screen_height); // 创建无标题栏的全屏窗口:使用弹出式窗口样式,去掉所有边框和标题栏 if (!window.CreateAndShow(L"mlzs_labmanager", origin, size, WS_POPUP | WS_VISIBLE)) { return EXIT_FAILURE; } window.SetQuitOnClose(true); run_loop.Run(); ::CoUninitialize(); return EXIT_SUCCESS; }
关键改动说明
我给你加了3处核心修改,每一处都帮你注释清楚了:
- 获取屏幕真实尺寸:用
GetSystemMetrics这个Windows系统API自动获取屏幕的宽高,不用硬写固定分辨率,适配所有屏幕。 - 调整窗口位置和大小:把窗口起点设为(0,0),大小设为屏幕的完整尺寸,确保铺满整个屏幕。
- 修改窗口样式:给
CreateAndShow加了WS_POPUP | WS_VISIBLE参数:WS_POPUP:让窗口变成完全无框的弹出式窗口,没有标题栏、最大化/最小化按钮和边框,完全是游戏那种全屏效果。WS_VISIBLE:确保窗口创建后直接显示出来,不用额外操作。
万一遇到兼容问题?
如果替换后编译报错,大概率是你的Win32Window类的CreateAndShow方法不支持自定义窗口样式参数。这时候你需要找到项目里的win32_window.cpp文件,找到里面的Create方法,把默认的窗口样式从WS_OVERLAPPEDWINDOW改成WS_POPUP | WS_VISIBLE就行,比如:
// 找到类似这样的代码行 const DWORD window_style = WS_OVERLAPPEDWINDOW; // 改成 const DWORD window_style = WS_POPUP | WS_VISIBLE;
改完之后重新编译Windows端的程序,就能看到你想要的无标题栏全屏效果啦!
内容的提问来源于stack exchange,提问作者Kumar Saptam
相关产品推荐
相关产品推荐

