main.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,
  7. _In_ wchar_t *command_line, _In_ int show_command) {
  8. // Attach to console when present (e.g., 'flutter run') or create a
  9. // new console when running with a debugger.
  10. // if (!::AttachConsole(ATTACH_PARENT_PROCESS) && ::IsDebuggerPresent()) {
  11. // CreateAndAttachConsole();
  12. // }
  13. // https://github.com/biyidev/biyi/blob/152bc7bbced37ba41ea6de42830a001c09f093b4/windows/runner/main.cpp#L12-L19
  14. HWND hwnd = ::FindWindow(L"FLUTTER_RUNNER_WIN32_WINDOW", L"naiyouwl");
  15. if (hwnd != NULL) {
  16. DispatchToProtocolHandler(hwnd);
  17. ::ShowWindow(hwnd, SW_NORMAL);
  18. ::SetForegroundWindow(hwnd);
  19. return EXIT_FAILURE;
  20. }
  21. // https://github.com/flutter/flutter/issues/47891#issuecomment-708850435
  22. // https://github.com/flutter/flutter/issues/47891#issuecomment-869729956
  23. // https://github.com/dart-lang/sdk/issues/39945#issuecomment-870428151
  24. if (!::AttachConsole(ATTACH_PARENT_PROCESS) && ::IsDebuggerPresent()) {
  25. CreateAndAttachConsole();
  26. } else {
  27. AllocConsole();
  28. ShowWindow(GetConsoleWindow(), SW_HIDE);
  29. }
  30. // Initialize COM, so that it is available for use in the library and/or
  31. // plugins.
  32. ::CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
  33. flutter::DartProject project(L"data");
  34. std::vector<std::string> command_line_arguments =
  35. GetCommandLineArguments();
  36. project.set_dart_entrypoint_arguments(std::move(command_line_arguments));
  37. FlutterWindow window(project);
  38. Win32Window::Point origin(10, 10);
  39. Win32Window::Size size(1280, 720);
  40. if (!window.Create(L"naiyouwl", origin, size)) {
  41. return EXIT_FAILURE;
  42. }
  43. window.SetQuitOnClose(true);
  44. ::MSG msg;
  45. while (::GetMessage(&msg, nullptr, 0, 0)) {
  46. ::TranslateMessage(&msg);
  47. ::DispatchMessage(&msg);
  48. }
  49. ::CoUninitialize();
  50. return EXIT_SUCCESS;
  51. }