如何在C++启动代码中将Flutter桌面程序设为无标题栏全屏
Hey there! As someone who’s tweaked Flutter’s Windows entry code before, let’s walk through exactly how to get your app launching in that clean, borderless fullscreen mode you want—perfect for game-like experiences. Since you’re new to C++, I’ll keep this step-by-step and explain every change clearly.
Core Idea
Flutter’s Windows app is built on top of Win32 APIs, so we can directly tweak the window’s properties using native Win32 calls to remove the titlebar and stretch it across the entire screen. Here’s how to modify your existing code:
Modified Code with Annotations
#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); // --- MODIFICATION START --- // Get the full screen resolution of the primary display int screen_width = GetSystemMetrics(SM_CXSCREEN); int screen_height = GetSystemMetrics(SM_CYSCREEN); // Set origin to top-left corner and size to full screen Win32Window::Point origin(0, 0); Win32Window::Size size(screen_width, screen_height); // --- MODIFICATION END --- if (!window.CreateAndShow(L"mlzs_labmanager", origin, size)) { return EXIT_FAILURE; } // --- MODIFICATION START --- // Get the window handle and tweak its style to remove titlebar/borders HWND hwnd = window.GetHandle(); // Assumes Win32Window has a GetHandle() method (standard in Flutter's template) if (hwnd != nullptr) { // Set window style to WS_POPUP (borderless, no titlebar) SetWindowLongPtr(hwnd, GWL_STYLE, WS_POPUP); // Force the window to update its position/size and apply the new style SetWindowPos(hwnd, HWND_TOP, 0, 0, screen_width, screen_height, SWP_FRAMECHANGED); } // --- MODIFICATION END --- window.SetQuitOnClose(true); run_loop.Run(); ::CoUninitialize(); return EXIT_SUCCESS; }
What Each Change Does
Grab Full Screen Dimensions:
GetSystemMetrics(SM_CXSCREEN)andGetSystemMetrics(SM_CYSCREEN)fetch the width and height of your primary monitor, so the window fits perfectly no matter your screen resolution.
Set Window Position & Size:
- We move the window’s origin to
(0,0)(the top-left corner of the screen) and set its size to match the screen dimensions.
- We move the window’s origin to
Remove Titlebar & Borders:
SetWindowLongPtr(hwnd, GWL_STYLE, WS_POPUP)changes the window’s style to a popup window—this removes all default borders, titlebars, and window controls (like minimize/maximize buttons).SetWindowPosforces the window to update with the new style and size, ensuring it fills the entire screen immediately.
Quick Note for New C++ Developers
If your Win32Window class doesn’t have a GetHandle() method (unlikely in Flutter’s default template), check the win32_window.h file—you’ll just need to add a public method that returns the HWND member variable (usually named window_handle_).
内容的提问来源于stack exchange,提问作者Kumar Saptam

