main.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include <flutter/dart_project.h>
  2. #include <flutter/flutter_view_controller.h>
  3. #include <windows.h>
  4. #include "flutter_window.h"
  5. #include "utils.h"
  6. #include <protocol_handler/protocol_handler_plugin.h>
  7. // 展示控制台
  8. void ShowConsole() {
  9. HWND hWnd = ::GetConsoleWindow();
  10. if (hWnd != NULL) {
  11. ::ShowWindow(hWnd, SW_SHOW);
  12. }
  13. }
  14. // 隐藏控制台
  15. void HideConsole() {
  16. HWND hWnd = ::GetConsoleWindow();
  17. if (hWnd != NULL) {
  18. ::ShowWindow(hWnd, SW_HIDE);
  19. }
  20. }
  21. int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,
  22. _In_ wchar_t *command_line, _In_ int show_command) {
  23. HWND hwnd = ::FindWindow(L"FLUTTER_RUNNER_WIN32_WINDOW", L"naiyouwl");
  24. if (hwnd != NULL) {
  25. DispatchToProtocolHandler(hwnd);
  26. ::ShowWindow(hwnd, SW_NORMAL);
  27. ::SetForegroundWindow(hwnd);
  28. return EXIT_FAILURE;
  29. }
  30. /*
  31. *
  32. else {
  33. AllocConsole();
  34. ShowWindow(GetConsoleWindow(), SW_HIDE);
  35. }
  36. * */
  37. // https://github.com/flutter/flutter/issues/47891#issuecomment-708850435
  38. // https://github.com/flutter/flutter/issues/47891#issuecomment-869729956
  39. // https://github.com/dart-lang/sdk/issues/39945#issuecomment-870428151
  40. if (!::AttachConsole(ATTACH_PARENT_PROCESS) && ::IsDebuggerPresent()) {
  41. CreateAndAttachConsole();
  42. } else {
  43. AllocConsole();
  44. ShowWindow(GetConsoleWindow(), SW_HIDE);
  45. }
  46. // Initialize COM, so that it is available for use in the library and/or
  47. // plugins.
  48. ::CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
  49. flutter::DartProject project(L"data");
  50. std::vector<std::string> command_line_arguments =
  51. GetCommandLineArguments();
  52. project.set_dart_entrypoint_arguments(std::move(command_line_arguments));
  53. FlutterWindow window(project);
  54. Win32Window::Point origin(10, 10);
  55. Win32Window::Size size(1280, 720);
  56. if (!window.Create(L"naiyouwl", origin, size)) {
  57. return EXIT_FAILURE;
  58. }
  59. window.SetQuitOnClose(true);
  60. ::MSG msg;
  61. while (::GetMessage(&msg, nullptr, 0, 0)) {
  62. ::TranslateMessage(&msg);
  63. ::DispatchMessage(&msg);
  64. }
  65. ::CoUninitialize();
  66. return EXIT_SUCCESS;
  67. }