win_console.dart 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import 'dart:ffi';
  2. import 'package:ffi/ffi.dart';
  3. import 'dart:io' show Platform;
  4. final DynamicLibrary user32 = DynamicLibrary.open('user32.dll');
  5. final DynamicLibrary kernel32 = DynamicLibrary.open('kernel32.dll');
  6. typedef ShowWindowC = Int32 Function(IntPtr hWnd, Int32 nCmdShow);
  7. typedef ShowWindowDart = int Function(int hWnd, int nCmdShow);
  8. final ShowWindowDart ShowWindow = user32
  9. .lookupFunction<ShowWindowC, ShowWindowDart>('ShowWindow');
  10. typedef GetConsoleWindowC = IntPtr Function();
  11. typedef GetConsoleWindowDart = int Function();
  12. final GetConsoleWindowDart GetConsoleWindow = kernel32
  13. .lookupFunction<GetConsoleWindowC, GetConsoleWindowDart>('GetConsoleWindow');
  14. typedef AllocConsoleC = Int32 Function();
  15. typedef AllocConsoleDart = int Function();
  16. final AllocConsoleDart AllocConsole = kernel32
  17. .lookupFunction<AllocConsoleC, AllocConsoleDart>('AllocConsole');
  18. typedef FreeConsoleC = Int32 Function();
  19. typedef FreeConsoleDart = int Function();
  20. final FreeConsoleDart FreeConsole = kernel32
  21. .lookupFunction<FreeConsoleC, FreeConsoleDart>('FreeConsole');
  22. const int SW_HIDE = 0;
  23. const int SW_SHOW = 5;
  24. void createConsole() {
  25. AllocConsole();
  26. // Now the console is created, you can show or hide it using the ShowWindow function
  27. }
  28. void hideConsole() {
  29. var consoleWnd = GetConsoleWindow();
  30. ShowWindow(consoleWnd, SW_HIDE);
  31. }
  32. void showConsole() {
  33. var consoleWnd = GetConsoleWindow();
  34. ShowWindow(consoleWnd, SW_SHOW);
  35. }
  36. void freeConsole() {
  37. FreeConsole();
  38. }