Andre
Andre

Reputation: 43

Flutter windows: get win32 window handle of flutter app

If I'm not mistaken, flutter is implemented with win32 libraries at the moment for windows (although some documentation page stated that this might change to .NET or some other framework because flutter windows is at a very early development stage and could change at any time without keeping backwards compatibility). Is there some way to get the win32 window handle for the current flutter app?

Upvotes: 2

Views: 3313

Answers (2)

Kirill
Kirill

Reputation: 1539

static final _user32 = DynamicLibrary.open('user32.dll');

static final findWindowA = _user32.lookupFunction<
      Int32 Function(Pointer<Utf8> _lpClassName, Pointer<Utf8> _lpWindowName),
      int Function(Pointer<Utf8> _lpClassName,
          Pointer<Utf8> _lpWindowName)>('FindWindowA');

int hWnd = findWindowA('FLUTTER_RUNNER_WIN32_WINDOW'.toNativeUtf8(), nullptr);
print(hWnd);

'FLUTTER_RUNNER_WIN32_WINDOW' is the flutter's window class name defined in windows/runner/win32_window.cpp, you can make it unique in case user is running multiple flutter applications.

toNativeUtf8 is a function from dart ffi.

Upvotes: 7

smorgan
smorgan

Reputation: 21599

Yes:

  • In the current framework API you can get the handle of the Flutter view (which isn't the top-level window) from the framework using GetNativeWindow(). If you want the top-level window, you can walk up from there using standard Win32 APIs.

  • Assuming you are using the FDE Windows runner, you can also get the top-level window directly in main.cpp using GetHandle().

Upvotes: 3

Related Questions