Switching between fullscreen and maximized #700
Unanswered
explorer-source
asked this question in
Q&A
Replies: 1 comment
-
import 'dart:ffi';
import 'package:ffi/ffi.dart';
import 'package:flutter/material.dart';
import 'package:win32/win32.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
bool _isFullscreen = false;
late int hwnd;
@override
void initState() {
hwnd = findMainWindow()!;
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.black,
body: Center(
child: ElevatedButton(
onPressed: () {
fullScreen = !fullScreen;
},
child: const Text('TOGGLE'),
),
),
),
);
}
set fullScreen(bool isFullScreen) {
_isFullscreen = isFullScreen;
if (isFullScreen) {
var style = GetWindowLongPtr(hwnd, GWL_STYLE);
style &= ~(WS_CAPTION);
SetWindowLongPtr(hwnd, GWL_STYLE, style);
ShowWindowAsync(hwnd, SW_SHOWMAXIMIZED);
} else {
var style = GetWindowLongPtr(hwnd, GWL_STYLE);
style |= WS_CAPTION;
SetWindowLongPtr(hwnd, GWL_STYLE, style);
ShowWindowAsync(hwnd, SW_RESTORE);
}
}
bool get fullScreen => _isFullscreen;
}
int? findMainWindow() {
final processId = GetCurrentProcessId();
final data = calloc<EnumWindowsData>()
..ref.hwnd = 0
..ref.processId = processId;
final callback = Pointer.fromFunction<EnumWindowsProc>(
_enumWindowsCallback,
0,
);
try {
do {
EnumWindows(callback, data.address);
} while (data.ref.hwnd == 0);
} finally {
free(data);
}
return data.ref.hwnd == 0 ? null : data.ref.hwnd;
}
final class EnumWindowsData extends Struct {
@IntPtr()
external int hwnd;
@IntPtr()
external int processId;
}
int _enumWindowsCallback(int hwnd, int lParam) {
final data = Pointer<EnumWindowsData>.fromAddress(lParam);
final processId = calloc<Uint32>();
try {
GetWindowThreadProcessId(hwnd, processId);
if (processId.value != data.ref.processId) {
return TRUE;
}
} finally {
free(processId);
}
final own = GetWindow(hwnd, GW_OWNER);
if (own != 0) {
return TRUE;
}
data.ref.hwnd = hwnd;
return FALSE; // stop enums
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm trying to rewrite existing .cpp code to pure dart but it doesn't work and I don't know why it's not throwing Exception also
this is my .cpp code that I'm trying to convert, it works
Beta Was this translation helpful? Give feedback.
All reactions