main.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,
  8. _In_ wchar_t *command_line, _In_ int show_command) {
  9. if (wcsstr(command_line, L"--runas-admin") != NULL) {
  10. // 这是一个提权重启,跳过实例检测
  11. } else {
  12. HWND hwnd = ::FindWindow(L"FLUTTER_RUNNER_WIN32_WINDOW", L"naiyouwl");
  13. if (hwnd != NULL) {
  14. DispatchToProtocolHandler(hwnd);
  15. ::ShowWindow(hwnd, SW_NORMAL);
  16. ::SetForegroundWindow(hwnd);
  17. return EXIT_FAILURE;
  18. }
  19. }
  20. /*
  21. *
  22. else {
  23. AllocConsole();
  24. ShowWindow(GetConsoleWindow(), SW_HIDE);
  25. }
  26. * */
  27. // https://github.com/flutter/flutter/issues/47891#issuecomment-708850435
  28. // https://github.com/flutter/flutter/issues/47891#issuecomment-869729956
  29. // https://github.com/dart-lang/sdk/issues/39945#issuecomment-870428151
  30. if (!::AttachConsole(ATTACH_PARENT_PROCESS) && ::IsDebuggerPresent()) {
  31. CreateAndAttachConsole();
  32. }
  33. else {
  34. AllocConsole();
  35. ShowWindow(GetConsoleWindow(), SW_HIDE);
  36. }
  37. // Initialize COM, so that it is available for use in the library and/or
  38. // plugins.
  39. ::CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
  40. flutter::DartProject project(L"data");
  41. std::vector<std::string> command_line_arguments =
  42. GetCommandLineArguments();
  43. project.set_dart_entrypoint_arguments(std::move(command_line_arguments));
  44. FlutterWindow window(project);
  45. Win32Window::Point origin(10, 10);
  46. Win32Window::Size size(1280, 720);
  47. if (!window.Create(L"naiyouwl", origin, size)) {
  48. return EXIT_FAILURE;
  49. }
  50. window.SetQuitOnClose(true);
  51. ::MSG msg;
  52. while (::GetMessage(&msg, nullptr, 0, 0)) {
  53. ::TranslateMessage(&msg);
  54. ::DispatchMessage(&msg);
  55. }
  56. ::CoUninitialize();
  57. return EXIT_SUCCESS;
  58. }