Browse Source

camera window position S/L; Opacity panel on top for click to restore;

HOME 3 weeks ago
parent
commit
aab49f53ed
2 changed files with 142 additions and 31 deletions
  1. 1 0
      MainForm.Designer.cs
  2. 141 31
      MainForm.cs

+ 1 - 0
MainForm.Designer.cs

@@ -805,6 +805,7 @@ namespace SinMaiLauncher
             tcMain.SelectedIndex = 0;
             tcMain.Size = new Size(504, 351);
             tcMain.TabIndex = 5;
+            tcMain.SelectedIndexChanged += tcMain_SelectedIndexChanged;
             // 
             // tpMainControl
             // 

+ 141 - 31
MainForm.cs

@@ -20,7 +20,7 @@ public partial class MainForm : Form
     private Dictionary<ChildProcessKind, ChildProcessControlGroup> childProcessControlGroups;
 
     private readonly IEnumerator<string> txtAni = TextAnimation.LoadingTextSeq(8).GetEnumerator();
-    private string mainControlStatusText = "摸鱼中";
+    private string mainControlStatusText = "摸鱼中,你可以按一键启动了";
     private bool mainControlStatusTextAnimation = false;
 
     private DateTime lastCheckSinMaiMainWinPos = DateTime.Now;
@@ -30,10 +30,13 @@ public partial class MainForm : Form
 
     private CameraInterops.CameraCaptureWrap? cameraCapture;
     private bool cameraMax = false;
+    private Rectangle cameraRestoreRect;
+    private Form cameraRestoreFloatPanel = new Form { FormBorderStyle = FormBorderStyle.None, Opacity = 0.005 };
 
     public MainForm()
     {
         InitializeComponent();
+        cameraRestoreFloatPanel.Click += CameraRestoreFloatPanel_Click;
     }
 
     private WindowInterops.MoveWindowResult MoveSinMaiMainWindow()
@@ -62,6 +65,115 @@ public partial class MainForm : Form
         ExplorerInterops.OpenWindow(holdingExplorerWindowPaths);
     }
 
+    private void CameraAutoPlace()
+    {
+        cameraRestoreRect = new(Location, Size);
+
+        FormBorderStyle = FormBorderStyle.None;
+        picBoxCamera.Parent = this;
+        WindowInterops.Maximize(picBoxCamera.Handle);
+
+        Top = TargetTop;
+        Height = TargetHeight;
+
+        if (cboCameraFormats.SelectedItem is CameraInterops.CameraCaptureWrap.ICameraFormat fmt)
+        {
+            // r = w / h
+            // w = h * r
+            var r = (double)fmt.Width / fmt.Height;
+            Width = (int)(Height * r);
+        }
+
+        if (rdoCameraPlaceLeft.Checked)
+        {
+            Left = 0;
+        }
+        else if (rdoCameraPlaceRight.Checked)
+        {
+            Left = Screen.PrimaryScreen.Bounds.Right - Width;
+        }
+
+        cameraRestoreFloatPanel.Visible = true;
+        cameraRestoreFloatPanel.Width = Width;
+        cameraRestoreFloatPanel.Height = 200;
+        cameraRestoreFloatPanel.Left = Left;
+        cameraRestoreFloatPanel.Top = Top - cameraRestoreFloatPanel.Height;
+
+        cameraMax = true;
+    }
+
+    private void CameraRestore()
+    {
+        FormBorderStyle = FormBorderStyle.Sizable;
+        picBoxCamera.Parent = pnlCamera;
+        WindowInterops.Normal(picBoxCamera.Handle);
+        cameraMax = false;
+
+        cameraRestoreFloatPanel.Visible = false;
+        Location = cameraRestoreRect.Location;
+        Size = cameraRestoreRect.Size;
+    }
+
+    /// <summary>
+    /// 计算控件相对于窗体客户区的边距
+    /// </summary>
+    private Padding GetControlMarginsRelativeToForm(Control control)
+    {
+        var leftMargin = 0;
+        var topMargin = 0;
+        var rightMargin = 0;
+        var bottomMargin = 0;
+
+        var current = control;
+        while (current != null && current != this)
+        {
+            // 累加当前控件相对于其父控件的偏移
+            leftMargin += current.Left;
+            topMargin += current.Top;
+
+            if (current.Parent != null)
+            {
+                var parentPadding = current.Parent.Padding;
+                leftMargin += parentPadding.Left;
+                topMargin += parentPadding.Top;
+
+                rightMargin += (current.Parent.ClientSize.Width - current.Right) + parentPadding.Right;
+                bottomMargin += (current.Parent.ClientSize.Height - current.Bottom) + parentPadding.Bottom;
+            }
+
+            // 处理 TabControl 的选项卡区域
+            if (current is TabPage && current.Parent is TabControl tabControl)
+            {
+                // 使用 DisplayRectangle 计算选项卡区域的偏移
+                var displayRect = tabControl.DisplayRectangle;
+                var controlRect = tabControl.ClientRectangle;
+
+                switch (tabControl.Alignment)
+                {
+                    case TabAlignment.Top:
+                        topMargin += (controlRect.Height - displayRect.Height); // 整个选项卡区域高度
+                        break;
+
+                    case TabAlignment.Bottom:
+                        bottomMargin += (controlRect.Height - displayRect.Height);
+                        break;
+
+                    case TabAlignment.Left:
+                        leftMargin += (controlRect.Width - displayRect.Width);
+                        break;
+
+                    case TabAlignment.Right:
+                        rightMargin += (controlRect.Width - displayRect.Width);
+                        break;
+                }
+            }
+
+            current = current.Parent;
+        }
+
+        return new Padding(leftMargin, topMargin, rightMargin, bottomMargin);
+    }
+
     private async void MainForm_Shown(object sender, EventArgs e)
     {
         TopMost = false;
@@ -482,7 +594,7 @@ public partial class MainForm : Form
         //显示系统音量
         if (lastCheckSysVolume < DateTime.Now.AddSeconds(-1))
         {
-            lastCheckSinMaiMainWinPos = DateTime.Now;
+            lastCheckSysVolume = DateTime.Now;
             var sv = SystemAudioVolumeInterop.GetVolume();
             lblSysVolume.Text = sv.HasValue ? $"{sv:N2}%" : "-";
         }
@@ -679,47 +791,45 @@ public partial class MainForm : Form
     private void cboCameraFormats_SelectedIndexChanged(object sender, EventArgs e)
     {
         if (cboCameraFormats.SelectedItem != null)
-            cameraCapture?.ApplySelectedFormat((CameraInterops.CameraCaptureWrap.ICameraFormat)cboCameraFormats.SelectedItem);
+        {
+            var selectedItem = (CameraInterops.CameraCaptureWrap.ICameraFormat)cboCameraFormats.SelectedItem;
+            cameraCapture?.ApplySelectedFormat(selectedItem);
+
+            var pad = GetControlMarginsRelativeToForm(pnlCamera);
+
+            // r = w / h
+            // h = w / r
+            var r = (double)selectedItem.Width / selectedItem.Height;
+            var h = TargetWidth / r;
+
+            Width = pad.Left + pad.Right + TargetWidth;
+            Height = pad.Top + pad.Bottom + (int)h;
+        }
+    }
+
+    private void CameraRestoreFloatPanel_Click(object? sender, EventArgs e)
+    {
+        CameraRestore();
     }
 
     private void picBoxCamera_CameraClick(object sender, EventArgs e)
     {
         if (cameraMax)
         {
-            FormBorderStyle = FormBorderStyle.Sizable;
-            picBoxCamera.Parent = pnlCamera;
-            WindowInterops.Normal(picBoxCamera.Handle);
-            cameraMax = false;
+            CameraRestore();
         }
         else
         {
-            FormBorderStyle = FormBorderStyle.None;
-            picBoxCamera.Parent = this;
-            WindowInterops.Maximize(picBoxCamera.Handle);
-
-            Top = TargetTop;
-            Height = TargetHeight;
-
-            if (cboCameraFormats.SelectedItem is CameraInterops.CameraCaptureWrap.ICameraFormat fmt)
-            {
-                var r = (double)fmt.Width / fmt.Height;
-                // r = w / h
-                // w = h * r
-                Width = (int)(Height * r);
-            }
-
-            cameraMax = true;
+            CameraAutoPlace();
         }
+    }
 
-        if (rdoCameraPlaceLeft.Checked)
-        {
-            Left = 0;
-        }
-        else if (rdoCameraPlaceRight.Checked)
+    private void tcMain_SelectedIndexChanged(object sender, EventArgs e)
+    {
+        if (tcMain.SelectedTab == tpConsoles)
         {
-            Left = Screen.PrimaryScreen.Bounds.Right - Width;
+            Height++;
+            Height--;
         }
     }
-
-
 }