简体   繁体   English

是否有任何选项可以在全屏模式下运行 Flutter Windows 桌面应用程序

[英]Is there any option to Run Flutter Windows Desktop application in full screen mode

我试过颤振 Windows 桌面应用程序,但我无法隐藏标题栏以在全屏模式窗口中运行应用程序

Got the same problem and I'm here to share my solution.遇到同样的问题,我在这里分享我的解决方案。

I'm not a Win32 developer but I managed to make a basic fullscreen this way.我不是 Win32 开发人员,但我设法通过这种方式制作了基本的全屏。

This code works for my Flutter version 1.21.0-10.0.pre.114, I hope it will for you too.此代码适用于我的 Flutter 版本 1.21.0-10.0.pre.114,我希望它也适用于您。

My solution is heavily inspired by this one : https://stackoverflow.com/a/2416613/14093885我的解决方案深受这个启发: https ://stackoverflow.com/a/2416613/14093885

You have to edit ./windows/runner/main.cpp您必须编辑 ./windows/runner/main.cpp

Insert the following code between these statements around line 30:在第 30 行左右的这些语句之间插入以下代码:

window.SetQuitOnClose(true);

//Insert Code Here

run_loop.Run();

Code to insert :要插入的代码:

//HWND is window handler
HWND hwnd = window.GetHandle(); 

auto windowHDC = GetDC(hwnd);
int fullscreenWidth  = GetDeviceCaps(windowHDC, DESKTOPHORZRES);
int fullscreenHeight = GetDeviceCaps(windowHDC, DESKTOPVERTRES);
int colourBits       = GetDeviceCaps(windowHDC, BITSPIXEL);
int refreshRate      = GetDeviceCaps(windowHDC, VREFRESH);

DEVMODE fullscreenSettings;
bool isChangeSuccessful;

EnumDisplaySettings(NULL, 0, &fullscreenSettings);
fullscreenSettings.dmPelsWidth        = fullscreenWidth;
fullscreenSettings.dmPelsHeight       = fullscreenHeight;
fullscreenSettings.dmBitsPerPel       = colourBits;
fullscreenSettings.dmDisplayFrequency = refreshRate;
fullscreenSettings.dmFields           = DM_PELSWIDTH |
                                      DM_PELSHEIGHT |
                                      DM_BITSPERPEL |
                                      DM_DISPLAYFREQUENCY;

SetWindowLongPtr(hwnd, GWL_EXSTYLE, WS_EX_APPWINDOW | WS_EX_TOPMOST);
SetWindowLongPtr(hwnd, GWL_STYLE, WS_POPUP | WS_VISIBLE);
SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, fullscreenWidth, fullscreenHeight, SWP_SHOWWINDOW);
isChangeSuccessful = ChangeDisplaySettings(&fullscreenSettings, CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL;
ShowWindow(hwnd, SW_MAXIMIZE);

EDIT - Flutter 2:编辑 - 颤振 2:

According to @Pavel the last line ShowWindow(hwnd, SW_MAXIMIZE);根据@Pavel 最后一行ShowWindow(hwnd, SW_MAXIMIZE); is not needed in Flutter 2.在 Flutter 2 中不需要。

I guess window_manager can achieve your needs我猜window_manager可以满足你的需求

Installation安装

Add this to your package's pubspec.yaml file:将此添加到包的 pubspec.yaml 文件中:

dependencies:
  window_manager:
    git:
      url: https://github.com/leanflutter/window_manager.git
      ref: main

Usage用法


// Enter fullscreen
await WindowManager.instance.setFullScreen(true);
// Level fullscreen
await WindowManager.instance.setFullScreen(false);

There is no built-in support for full-screen mode yet, so there's no Dart API you can call to enter full screen.目前还没有对全屏模式的内置支持,所以没有可以调用的 Dart API 来进入全屏模式。 If you are familiar with Win32 programming, you could either change the Runner code directly to make the window full screen, or write a plug-in that does it.如果您熟悉 Win32 编程,您可以直接更改 Runner 代码以使窗口全屏,或者编写一个插件来做到这一点。

You need to implement the plugin window_size: https://github.com/google/flutter-desktop-embedding/tree/master/plugins/window_size你需要实现插件window_size: https ://github.com/google/flutter-desktop-embedding/tree/master/plugins/window_size

@override initState() {
super.initState();
Future.delayed(Duration.zero).then((finish) async {
  if (Platform.isMacOS) {
    // * FULL SCREEN
    PlatformWindow window = await getWindowInfo();
    setWindowFrame(Rect.fromLTWH(0, 0, window.screen.frame.width, window.screen.frame.height));
  }
});

} }

I think it must change the cpp file now.我认为它现在必须更改 cpp 文件。

win32_window.cpp replace "WS_OVERLAPPEDWINDOW" with "WS_POPUP | WS_MAXIMIZE" win32_window.cpp 将“WS_OVERLAPPEDWINDOW”替换为“WS_POPUP | WS_MAXIMIZE”

/* WS_POPUP : without title bar WS_MAXIMIZE : full screen */ /* WS_POPUP : 没有标题栏 WS_MAXIMIZE : 全屏 */

this is the link for more help window Size I have Done this in this way I get window size from here: https://docs.microsoft.com/en-us/windows/win32/winmsg/window-styles这是更多帮助窗口大小的链接我以这种方式完成了我从这里获取窗口大小: https ://docs.microsoft.com/en-us/windows/win32/winmsg/window-styles

in flutter windows app for window size : go to windows/runner/win32_window.cpp在flutter windows应用程序中获取窗口大小:转到windows/runner/win32_window.cpp

  • for fullscreen window without titlebar WS_POPUP |用于没有标题栏的全屏窗口 WS_POPUP | WS_VISIBLE WS_VISIBLE
  • for fullscreen window with titlebar(without any icon such as close,minimize,restore,logo) WS_MAXIMIZE |用于带有标题栏的全屏窗口(没有任何图标,例如关闭、最小化、恢复、徽标) WS_MAXIMIZE | WS_VISIBLE WS_VISIBLE
  • for small or customized window WS_OVERLAPPEDWINDOW |用于小窗口或自定义窗口 WS_OVERLAPPEDWINDOW | WS_VISIBLE WS_VISIBLE
  • for fullscreen window with titlebar WS_OVERLAPPEDWINDOW |用于带有标题栏的全屏窗口 WS_OVERLAPPEDWINDOW | WS_MAXIMIZE | WS_MAXIMIZE | WS_VISIBLE WS_VISIBLE

Find this CODE given below in win32_window.cpp file and change as you wish.在 win32_window.cpp 文件中找到下面给出的代码,并根据需要进行更改。

HWND window = CreateWindow(
  window_class, title.c_str(), WS_OVERLAPPEDWINDOW | WS_MAXIMIZE | WS_VISIBLE,
  Scale(origin.x, scale_factor), Scale(origin.y, scale_factor),
  Scale(size.width, scale_factor), Scale(size.height, scale_factor),
  nullptr, nullptr, GetModuleHandle(nullptr), this);

Use this package: https://github.com/leanflutter/window_manager使用这个包: https ://github.com/leanflutter/window_manager

In your main, add this code after 'RunApp':在您的主目录中,在“RunApp”之后添加以下代码:

WidgetsFlutterBinding.ensureInitialized();
// Must add this line.
await windowManager.ensureInitialized();

// Use it only after calling `hiddenWindowAtLaunch`
windowManager.waitUntilReadyToShow().then((_) async {
// Hide window title bar
await windowManager.setTitleBarStyle('hidden');
await windowManager.setFullScreen(true);
await windowManager.center();
await windowManager.show();
await windowManager.setSkipTaskbar(false);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM