main.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include <flutter/dart_project.h>
  2. #include <flutter/flutter_view_controller.h>
  3. #include <windows.h>
  4. #include <flutter/method_channel.h>
  5. #include <flutter/plugin_registrar_windows.h>
  6. #include "flutter_window.h"
  7. #include "utils.h"
  8. #include <protocol_handler/protocol_handler_plugin.h>
  9. // 展示控制台
  10. void ShowConsole() {
  11. HWND hWnd = ::GetConsoleWindow();
  12. if (hWnd != NULL) {
  13. ::ShowWindow(hWnd, SW_SHOW);
  14. }
  15. }
  16. // 隐藏控制台
  17. void HideConsole() {
  18. HWND hWnd = ::GetConsoleWindow();
  19. if (hWnd != NULL) {
  20. ::ShowWindow(hWnd, SW_HIDE);
  21. }
  22. }
  23. // 一个例子:展示如何注册处理函数来响应Dart的调用。
  24. void RegisterWithFlutter(flutter::PluginRegistrarWindows *registrar) {
  25. auto channel =
  26. std::make_unique<flutter::MethodChannel<flutter::EncodableValue>>(
  27. registrar->messenger(), "com.naiyouwl.native_code",
  28. &flutter::StandardMethodCodec::GetInstance());
  29. channel->SetMethodCallHandler(
  30. [](const flutter::MethodCall<flutter::EncodableValue>& call,
  31. std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
  32. if (call.method_name().compare("showConsole") == 0) {
  33. ShowConsole();
  34. result->Success(flutter::EncodableValue(true));
  35. } else if (call.method_name().compare("hideConsole") == 0) {
  36. HideConsole();
  37. result->Success(flutter::EncodableValue(true));
  38. } else {
  39. result->NotImplemented();
  40. }
  41. });
  42. }
  43. int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,
  44. _In_ wchar_t *command_line, _In_ int show_command) {
  45. // 注册Flutter插件
  46. RegisterPlugins(&flutter_controller->engine());
  47. HWND hwnd = ::FindWindow(L"FLUTTER_RUNNER_WIN32_WINDOW", L"naiyouwl");
  48. if (hwnd != NULL) {
  49. DispatchToProtocolHandler(hwnd);
  50. ::ShowWindow(hwnd, SW_NORMAL);
  51. ::SetForegroundWindow(hwnd);
  52. return EXIT_FAILURE;
  53. }
  54. /*
  55. *
  56. else {
  57. AllocConsole();
  58. ShowWindow(GetConsoleWindow(), SW_HIDE);
  59. }
  60. * */
  61. // https://github.com/flutter/flutter/issues/47891#issuecomment-708850435
  62. // https://github.com/flutter/flutter/issues/47891#issuecomment-869729956
  63. // https://github.com/dart-lang/sdk/issues/39945#issuecomment-870428151
  64. if (!::AttachConsole(ATTACH_PARENT_PROCESS) && ::IsDebuggerPresent()) {
  65. CreateAndAttachConsole();
  66. } else {
  67. AllocConsole();
  68. ShowWindow(GetConsoleWindow(), SW_HIDE);
  69. }
  70. // Initialize COM, so that it is available for use in the library and/or
  71. // plugins.
  72. ::CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
  73. flutter::DartProject project(L"data");
  74. std::vector<std::string> command_line_arguments =
  75. GetCommandLineArguments();
  76. project.set_dart_entrypoint_arguments(std::move(command_line_arguments));
  77. FlutterWindow window(project);
  78. Win32Window::Point origin(10, 10);
  79. Win32Window::Size size(1280, 720);
  80. if (!window.Create(L"naiyouwl", origin, size)) {
  81. return EXIT_FAILURE;
  82. }
  83. window.SetQuitOnClose(true);
  84. ::MSG msg;
  85. while (::GetMessage(&msg, nullptr, 0, 0)) {
  86. ::TranslateMessage(&msg);
  87. ::DispatchMessage(&msg);
  88. }
  89. ::CoUninitialize();
  90. return EXIT_SUCCESS;
  91. }