Search code examples
c++windowsflutterdesktop

C++: wWinMain: How do I always unfocus my created window?


Use Case

I want to build a virtual keyboard using Flutter. I am inspired by the windows virtual keyboard "osk" (just run "osk" in cmd). Whenever I type something in a different window, I do not (!) want to lose focus.

The Problem

The flutter-app (desktop, windows) always steals the focus. I assume that it is configured in the main.cpp, but I have not found out how to achieve the same effect as in osk.

How do I strictly disallow getting any focus for my window?

Here is the main.cpp-code:

#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;
}


Solution

  • Solution 1

    Thanks to @Axalo, the Extended Window Styles WS_EX_NOACTIVATE is the correct answer. It needs to be set when creating a window.

    Solution 2 (in Flutter)

    It is actually possible to do it on runtime in flutter. I use the plugins win32 and window_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);
        }
      }
    

    You can restore your old state:

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