在 Flutter Windows 應用程式中使用外部視窗
在 Flutter 應用程式中新增外部視窗的特殊注意事項
Windows 生命週期
影響對象
#使用 Flutter 3.13 之後版本建置,並且會開啟非 Flutter 視窗的 Windows 應用程式。
概述
#當你在 Flutter Windows 應用程式中新增非 Flutter 視窗時,預設情況下該視窗不會被納入應用程式生命週期狀態更新的邏輯中。例如,這代表當外部視窗顯示或隱藏時,應用程式的生命週期狀態不會正確地更新為 inactive(非活動)或 hidden(隱藏)。因此,應用程式可能會透過 WidgetsBindingObserver.didChangeAppLifecycle 收到錯誤的生命週期狀態變更。
我需要做什麼?
若要將外部視窗納入應用程式的這項邏輯中,
該視窗的 WndProc 程序
必須呼叫 FlutterEngine::ProcessExternalWindowMessage。
為達成此目的,請將以下程式碼加入自訂外部視窗的訊息處理函式中。在 Win32 API 的 C++ 包裝器中,
這通常是從視窗 WndProc 呼叫的類別方法。
確切的檔案名稱與類別名稱取決於你的應用程式實作。
LRESULT MyExternalWindow::MessageHandler(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
std::optional<LRESULT> result = flutter_controller_->engine()->ProcessExternalWindowMessage(hwnd, msg, wparam, lparam);
if (result.has_value()) {
return *result;
}
// Original contents of WndProc...
}
Unless stated otherwise, the documentation on this site reflects Flutter 3.44.0. Page last updated on 2026-06-14. View source or report an issue.