英文:
C++: wWinMain: How do I always unfocus my created window?
问题
Use Case
我想使用Flutter构建一个虚拟键盘
。
我受到了Windows虚拟键盘“osk”的启发(只需在cmd中运行“osk”)。
无论我在不同的窗口中输入什么,我都不希望失去焦点。
The Problem
Flutter应用程序(桌面版,Windows)总是夺走焦点。我认为这是在main.cpp
中配置的,但我还没有找到如何实现与“osk”中相同的效果。
我如何严格禁止我的窗口获得任何焦点?
以下是main.cpp
代码:
英文:
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;
}
答案1
得分: 0
Solution 1
感谢 @Axalo,Extended Window Styles WS_EX_NOACTIVATE
是正确的答案。需要在创建窗口时设置它。
Solution 2 (在Flutter中)
实际上,在Flutter中可以在运行时执行这个操作。
我使用了插件 win32 和 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);
}
}
你可以恢复旧状态:
restoreStyle() async {
if (hwnd != 0) {
SetWindowLongPtr(hwnd, GWL_EXSTYLE, 256); // 选择你的样式
}
}
英文:
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
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论