admin 1 year ago
parent
commit
e5c1abfed2

+ 9 - 0
example/lib/main.dart

@@ -96,6 +96,15 @@ class _MyAppState extends State<MyApp> {
 
               ElevatedButton(
                   onPressed: onIsRunAdmin, child: Text(_isAdmin)),
+
+              ElevatedButton(
+                  onPressed: () async{
+                    await _wlBaseHelpPlugin.showConsole();
+                  }, child: Text('显示控制台')),
+              ElevatedButton(
+                  onPressed: () async{
+                    await _wlBaseHelpPlugin.hideConsole();
+                  }, child: Text('隐藏控制台')),
             ],
           ),
         ),

+ 4 - 1
example/windows/runner/main.cpp

@@ -12,7 +12,10 @@ int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,
   if (!::AttachConsole(ATTACH_PARENT_PROCESS) && ::IsDebuggerPresent()) {
     CreateAndAttachConsole();
   }
-
+  else {
+    AllocConsole();
+    ShowWindow(GetConsoleWindow(), SW_HIDE);
+  }
   // Initialize COM, so that it is available for use in the library and/or
   // plugins.
   ::CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);

+ 9 - 0
lib/wl_base_help.dart

@@ -13,4 +13,13 @@ class WlBaseHelp {
   Future<bool?> isRunningAsAdmin() {
     return WlBaseHelpPlatform.instance.isRunningAsAdmin();
   }
+
+  Future<void> showConsole() async {
+    await WlBaseHelpPlatform.instance.showConsole();
+  }
+
+  Future<void> hideConsole() async {
+    await  WlBaseHelpPlatform.instance.hideConsole();
+  }
+
 }

+ 6 - 0
lib/wl_base_help_method_channel.dart

@@ -29,6 +29,12 @@ class MethodChannelWlBaseHelp extends WlBaseHelpPlatform {
     final isAdmin = await methodChannel.invokeMethod<bool>('isRunningAsAdmin');
     return isAdmin;
   }
+  Future<void> showConsole() async {
+    await methodChannel.invokeMethod('showConsole');
+  }
 
+  Future<void> hideConsole() async {
+    await methodChannel.invokeMethod('hideConsole');
+  }
 
 }

+ 9 - 0
lib/wl_base_help_platform_interface.dart

@@ -34,4 +34,13 @@ abstract class WlBaseHelpPlatform extends PlatformInterface {
   Future<bool?> isRunningAsAdmin() async {
     throw UnimplementedError('runAsAdministrator() has not been implemented.');
   }
+
+
+  Future<void> showConsole() async {
+    throw UnimplementedError('showConsole() has not been implemented.');
+  }
+
+   Future<void> hideConsole() async {
+     throw UnimplementedError('hideConsole() has not been implemented.');
+  }
 }

+ 12 - 0
test/wl_base_help_test.dart

@@ -22,6 +22,18 @@ class MockWlBaseHelpPlatform
     // TODO: implement isRunningAsAdmin
     throw UnimplementedError();
   }
+
+  @override
+  Future<void> hideConsole() {
+    // TODO: implement hideConsole
+    throw UnimplementedError();
+  }
+
+  @override
+  Future<void> showConsole() {
+    // TODO: implement showConsole
+    throw UnimplementedError();
+  }
 }
 
 void main() {

+ 22 - 1
windows/wl_base_help_plugin.cpp

@@ -40,6 +40,19 @@ bool IsWindows11OrGreater() {
         (rovi.dwMajorVersion == 10 && rovi.dwBuildNumber >= 22000);
 }
 
+void ShowConsole() {
+    HWND consoleWindow = GetConsoleWindow();
+    if (consoleWindow != NULL) {
+        ShowWindow(consoleWindow, SW_SHOW);
+    }
+}
+
+void HideConsole() {
+    HWND consoleWindow = GetConsoleWindow();
+    if (consoleWindow != NULL) {
+        ShowWindow(consoleWindow, SW_HIDE);
+    }
+}
 namespace wl_base_help {
 
 // static
@@ -67,7 +80,15 @@ WlBaseHelpPlugin::~WlBaseHelpPlugin() {}
 void WlBaseHelpPlugin::HandleMethodCall(
     const flutter::MethodCall<flutter::EncodableValue>& method_call,
     std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
-    if (method_call.method_name().compare("runAsAdministrator") == 0) {
+    
+    if (method_call.method_name().compare("showConsole") == 0) {
+        ShowConsole();
+        result->Success();
+    }
+    else if (method_call.method_name().compare("hideConsole") == 0) {
+        HideConsole();
+        result->Success();
+    }else if (method_call.method_name().compare("runAsAdministrator") == 0) {
         HWND hwnd = GetMainWindow();
         // 调用您的提权逻辑,传递 hwnd
         TryRunAsAdmin(hwnd, result);