Browse Source

Merge branch 'master' of http://git.ius.plus:88/Project-Wing/fis

loki.wu 2 years ago
parent
commit
f583fa5ffb
2 changed files with 101 additions and 10 deletions
  1. 11 5
      fis/MainWindow.axaml.cs
  2. 90 5
      fis/Win/WindowDragingHelper.cs

+ 11 - 5
fis/MainWindow.axaml.cs

@@ -89,21 +89,21 @@ namespace fis
                 {
                     if (e == ThisWindowName)
                     {
-                        Dispatcher.UIThread.Post(new Action(() => { WindowState = WindowState.Maximized; }));
+                        WindowDragingHelper.MaximizeWindow(e);
                     }
                 };
                 winFisBrowser.WindowMinimized += (sender, e) => 
                 {
                     if (e == ThisWindowName)
                     {
-                        Dispatcher.UIThread.Post(new Action(() => { WindowState = WindowState.Minimized; }));
+                        WindowDragingHelper.MinimizeWindow(e);
                     }
                 };
                 winFisBrowser.WindowRestored += (sender, e) => 
                 {
                     if (e == ThisWindowName)
                     {
-                        Dispatcher.UIThread.Post(new Action(() => { WindowState = WindowState.Normal; }));
+                        WindowDragingHelper.RestoreWindow(e);
                     }
                 };
                 winFisBrowser.WindowClosed += OnBrowserCloseTheWindow;
@@ -151,7 +151,7 @@ namespace fis
             }
 
             AppManager.ParentWindow = this;
-            Closed += MainWindow_Closed;
+            Closed += OnMainWindowClosed;
         }
 
         private void OnBrowserCloseTheWindow(object? sender, string e)
@@ -162,8 +162,14 @@ namespace fis
             }
         }
 
-        private void MainWindow_Closed(object? sender, EventArgs e)
+        private void OnMainWindowClosed(object? sender, EventArgs e)
         {
+            if (BrowserManager.MainBrowser is WinFisBrowser)
+            {
+                WindowDragingHelper.StopMouseMonitor();
+                //UnRegister the main window.
+                WindowDragingHelper.UnRegisterWindow(ThisWindowName);
+            }
             var manager = AppManager.Get<ISecondaryScreenManager>();
             manager.Dispose();
         }

+ 90 - 5
fis/Win/WindowDragingHelper.cs

@@ -768,6 +768,15 @@ namespace fis.Win
         [DllImport("user32.dll", SetLastError = true)]
         private static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
 
+        [DllImport("user32.dll")]
+        private static extern bool IsIconic(IntPtr hWnd);
+
+        [DllImport("user32.dll")]
+        private static extern bool IsZoomed(IntPtr hWnd);
+
+        [DllImport("user32.dll")]
+        private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
+
         private static readonly Dictionary<string, IntPtr> _windowHandles = new Dictionary<string, IntPtr>();
 
         private static IntPtr _dragingWindowHandle;
@@ -779,16 +788,28 @@ namespace fis.Win
         private static HookProc? _hookProc = null;
 
 
+        /// <summary>
+        /// Register one window into the draging process manager.
+        /// </summary>
+        /// <param name="windowName">The name of the window.</param>
+        /// <param name="windowHandle">The handle of the window, can be acquired from window.PlatormImpl.Handle.Handle</param>
         public static void RegisterWindow(string windowName, IntPtr windowHandle)
         {
             _windowHandles.Add(windowName, windowHandle);
         }
 
+        /// <summary>
+        /// Unregister the window from the draging process manager.
+        /// </summary>
+        /// <param name="windowName">The window name.</param>
         public static void UnRegisterWindow(string windowName)
         {
             _windowHandles.Remove(windowName);
         }
 
+        /// <summary>
+        /// Start the mouse monitor, remember stop it when the main window closed.
+        /// </summary>
         public static void StartMouseMonitor()
         {
             _hookProc = OnMouseHookProc;
@@ -801,6 +822,9 @@ namespace fis.Win
             }
         }
 
+        /// <summary>
+        /// Stop the mouse monitor.
+        /// </summary>
         public static void StopMouseMonitor()
         {
             if (_mouseHookHandle != IntPtr.Zero)
@@ -855,6 +879,42 @@ namespace fis.Win
             }
         }
 
+        //Minimize the window
+        public static void MinimizeWindow(string windowName)
+        {
+            if (_windowHandles.TryGetValue(windowName, out var windowHandle))
+            {
+                //SW_MINIMIZE = 6
+                //SW_SHOWMINIMIZED = 2
+                //SW_SHOWMINNOACTIVE = 7
+                ShowWindow(windowHandle, 6);
+                EndWindowDrag(windowName);
+            }
+        }
+
+
+        //Maximize the window
+        public static void MaximizeWindow(string windowName)
+        {
+            if (_windowHandles.TryGetValue(windowName, out var windowHandle))
+            {
+                //SW_MAXIMIZE = 3
+                ShowWindow(windowHandle, 3);
+                EndWindowDrag(windowName);
+            }
+        }
+
+        //Restore the window
+        public static void RestoreWindow(string windowName)
+        {
+            if (_windowHandles.TryGetValue(windowName, out var windowHandle))
+            {
+                //SW_RESTORE = 9
+                ShowWindow(windowHandle, 9);
+                EndWindowDrag(windowName);
+            }
+        }
+
         private static int OnMouseHookProc(int nCode, int wParam, IntPtr lParam)
         {
             if (_dragingWindowHandle != IntPtr.Zero)
@@ -862,13 +922,38 @@ namespace fis.Win
                 if (wParam == (int)MessageType.WM_MOUSEMOVE)
                 {
                     var mouseData = Marshal.PtrToStructure<MOUSEHOOKSTRUCT>(lParam);
-                    if (GetWindowRect(_dragingWindowHandle, out var rect))
+                    if(IsZoomed(_dragingWindowHandle))
+                    {
+                        //窗体最大化的时候拖动还原窗口
+                        if (GetWindowRect(_dragingWindowHandle, out var rect))
+                        {
+                            var width = rect.Right - rect.Left;
+                            var height = rect.Bottom - rect.Top;
+                            var offsetXRatio = (double)(mouseData.pt.X - rect.Left) / width;
+                            //SW_RESTORE = 9
+                            ShowWindow(_dragingWindowHandle, 9);
+                            if (GetWindowRect(_dragingWindowHandle, out rect))
+                            {
+                                width = rect.Right - rect.Left;
+                                height = rect.Bottom - rect.Top;
+                                _dragOffset.X = (int)(width * offsetXRatio);
+                                if (MoveWindow(_dragingWindowHandle, mouseData.pt.X - _dragOffset.X, mouseData.pt.Y - _dragOffset.Y, width, height, true))
+                                {
+                                    //
+                                }
+                            }
+                        }
+                    }
+                    else
                     {
-                        var width = rect.Right - rect.Left;
-                        var height = rect.Bottom - rect.Top;
-                        if (MoveWindow(_dragingWindowHandle, mouseData.pt.X - _dragOffset.X, mouseData.pt.Y - _dragOffset.Y, width, height, true))
+                        if (GetWindowRect(_dragingWindowHandle, out var rect))
                         {
-                            //
+                            var width = rect.Right - rect.Left;
+                            var height = rect.Bottom - rect.Top;
+                            if (MoveWindow(_dragingWindowHandle, mouseData.pt.X - _dragOffset.X, mouseData.pt.Y - _dragOffset.Y, width, height, true))
+                            {
+                                //
+                            }
                         }
                     }
                 }