Reputation: 160
I am having an issue trying to location information on how to set the initial size and position of a Flutter desktop applications window.
Upon searching, I came across some code within the C++ files, but does anyone know if there is a way to set this from within Dart, or can it only be done in the C++ files?
Upvotes: 6
Views: 7878
Reputation: 9
Example to set window position to top-right corner with 10px offset.
Modify windows/runner/main.cpp
:
Find:
...
FlutterWindow window(project);
Win32Window::Point origin(10, 10);
Win32Window::Size size(1280, 720);
...
Change to:
// set window position to top-right corner
// Get the size of the primary monitor
HMONITOR primaryMonitor = ::MonitorFromWindow(nullptr, MONITOR_DEFAULTTOPRIMARY);
MONITORINFO monitorInfo;
monitorInfo.cbSize = sizeof(MONITORINFO);
::GetMonitorInfo(primaryMonitor, &monitorInfo);
int screenWidth = monitorInfo.rcMonitor.right - monitorInfo.rcMonitor.left;
//int screenHeight = monitorInfo.rcMonitor.bottom - monitorInfo.rcMonitor.top; // not used
// Set the origin to the top-right corner of the screen with 10 pixels padding
int xOffset = 10;
int yOffset = 10;
Win32Window::Point origin(screenWidth - 1280 - xOffset, yOffset);
//Win32Window::Point origin(10, 10); // original
Tested on Flutter (Channel stable, 3.10.6, on Microsoft Windows [Version 10.0.19045.3208])
.
Upvotes: 0
Reputation: 65
A method of modifying by platform code. You can modify these two properties through the code in this path:windows/runner/main.cpp
.
Here is the code you want to modify:
Win32Window::Point origin(kFlutterWindowOriginX, kFlutterWindowOriginY);
Win32Window::Size size(kFlutterWindowWidth, kFlutterWindowHeight);
The initial value is here:windows/runner/window_configuration.cpp
Upvotes: 4
Reputation: 103
This package can be helpful: Bitsdojo Window. It makes it easy to customize and work with your Flutter desktop app window on Windows, macOS and Linux.
Watch the tutorial to get started.
Features of this package:
- Custom window frame - remove standard Windows/macOS/Linux titlebar and buttons
- Hide window on startup
- Show/hide window
- Move window using Flutter widget
- Minimize/Maximize/Restore/Close window
- Set window size, minimum size and maximum size
- Set window position
- Set window alignment on screen (center/topLeft/topRight/bottomLeft/bottomRight)
- Set window title
Upvotes: 3
Reputation: 333
In my case i need arrange window on right bottom corner and i hotfix CreateAndShow(...) method(${dir project}\windows\runner\win32 window.cpp) like this:
bool Win32Window::CreateAndShow(const std::wstring& title,
const Point& origin,
const Size& size) {
Destroy();
const wchar_t* window_class =
WindowClassRegistrar::GetInstance()->GetWindowClass();
const POINT target_point = {static_cast<LONG>(origin.x),
static_cast<LONG>(origin.y)};
HMONITOR monitor = MonitorFromPoint(target_point, MONITOR_DEFAULTTONEAREST);
UINT dpi = FlutterDesktopGetDpiForMonitor(monitor);
double scale_factor = dpi / 96.0;
int monitor_width = GetSystemMetrics(SM_CXSCREEN);
int monitor_hight = GetSystemMetrics(SM_CYSCREEN);
int new_x_position = monitor_width - size.width;
int new_y_position = monitor_hight - size.height;
HWND window = CreateWindow(
window_class, title.c_str(), WS_POPUP|WS_VISIBLE|WS_SYSMENU,
Scale(new_x_position, scale_factor), Scale(new_y_position, scale_factor),
Scale(size.width, scale_factor), Scale(size.height, scale_factor),
nullptr, nullptr, GetModuleHandle(nullptr), this);
if (!window) {
return false;
}
return OnCreate();
}
Upvotes: 1
Reputation: 65
There is a very simple way to do this. With this library desktop_window, you can set the window size and so on. Examples are as follows
import 'package:desktop_window/desktop_window.dart';
import 'package:flutter/material.dart';
void main() async {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: Scaffold(
appBar: AppBar(
title: const Text('desktop_window example app'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
RaisedButton(
child: Text("setMinWindowSize(300,400)"),
onPressed: () async {
await DesktopWindow.setMinWindowSize(Size(300, 400));
},
),
RaisedButton(
child: Text("setMaxWindowSize(800,800)"),
onPressed: () async {
await DesktopWindow.setMaxWindowSize(Size(800, 800));
},
),
],
),
),
),
);
}
}
Upvotes: 1
Reputation: 21599
It can currently only be done from the platform code, because the window is created before the engine.
See this issue for a possible solution to this problem; if you are familiar enough with the platform toolkits on the platform(s) you are developing for, you could implement the solution described there manually in your own runner(s), by adding a custom MethodChannel
and changing the initial creation details of the native windows.
Upvotes: 1