flutter C++ :我如何总是不聚焦我创建的窗口?

w6lpcovy  于 2023-03-19  发布在  Flutter
关注(0)|答案(1)|浏览(120)

用例

我想用Flutter构建一个virtual keyboard。我的灵感来自windows虚拟键盘“osk”(只需在cmd中运行“osk”)。每当我在不同的窗口中键入内容时,我都不(!)想失去焦点。

问题

flutter-app(桌面,windows)总是抢着焦点,我假设它是在main.cpp中配置的,但我还没有找到如何达到和osk一样的效果。
如何严格禁止窗口获得任何焦点?
下面是main.cpp代码:

#include <flutter/dart_project.h>
#include <flutter/flutter_view_controller.h>
#include <windows.h>

#include "flutter_window.h"
#include "utils.h"

int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,
                      _In_ wchar_t *command_line, _In_ int show_command)
{
  // Attach to console when present (e.g., 'flutter run') or create a
  // new console when running with a debugger.
  if (!::AttachConsole(ATTACH_PARENT_PROCESS) && ::IsDebuggerPresent())
  {
    CreateAndAttachConsole();
  }

  // Initialize COM, so that it is available for use in the library and/or
  // plugins.
  ::CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);

  flutter::DartProject project(L"data");

  std::vector<std::string> command_line_arguments =
      GetCommandLineArguments();

  project.set_dart_entrypoint_arguments(std::move(command_line_arguments));

  FlutterWindow window(project);
  Win32Window::Point origin(0, 0);
  Win32Window::Size size(1024, 768);
  if (!window.CreateAndShow(L"windows", origin, size))
  {
    return EXIT_FAILURE;
  }
  window.SetQuitOnClose(true);

  ::MSG msg;
  while (::GetMessage(&msg, nullptr, 0, 0))
  {
    ::TranslateMessage(&msg);
    ::DispatchMessage(&msg);
  }

  ::CoUninitialize();
  return EXIT_SUCCESS;
}
kzipqqlq

kzipqqlq1#

溶液1

感谢@Axalo,* 扩展窗口样式 * WS_EX_NOACTIVATE是正确答案。创建窗口时需要设置它。

溶液2(Flutter)

实际上,在flutter中运行时也可以这样做,我使用的是win32window_manager插件。

import 'dart:ffi';
import 'dart:io';
import 'package:process_run/shell_run.dart';
import 'package:win32/win32.dart';
import 'package:window_manager/window_manager.dart';

  setInactiveStyle() async {
    await windowManager.ensureInitialized();
    final windowTitle = await windowManager.getTitle();
    final hwnd = FindWindow('FLUTTER_RUNNER_WIN32_WINDOW'.toNativeUtf16(),
        windowTitle.toNativeUtf16());
    if (hwnd != 0) {
      SetWindowLongPtr(hwnd, GWL_EXSTYLE, WS_EX_NOACTIVATE);
      await windowManager.setAlwaysOnTop(true);
    }
  }

您可以恢复旧状态:

restoreStyle() async {
    if (hwnd != 0) {
      SetWindowLongPtr(hwnd, GWL_EXSTYLE, 256); // choose your style
    }
  }

相关问题