main.cpp 3.1 KB

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