Skip to content

Commit

Permalink
1.增加控制器吸附scrcpy窗口
Browse files Browse the repository at this point in the history
2.增加scrcpy窗口位置记忆
  • Loading branch information
pdone committed Sep 26, 2023
1 parent 7700b69 commit 27d3aaf
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 97 deletions.
10 changes: 8 additions & 2 deletions FreeControl.sln
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30104.148
# Visual Studio Version 17
VisualStudioVersion = 17.6.33815.320
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FreeControl", "FreeControl\FreeControl.csproj", "{EF7540E1-F2E7-4682-80AB-92354C55A4C6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{EF7540E1-F2E7-4682-80AB-92354C55A4C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EF7540E1-F2E7-4682-80AB-92354C55A4C6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EF7540E1-F2E7-4682-80AB-92354C55A4C6}.Debug|x86.ActiveCfg = Debug|x86
{EF7540E1-F2E7-4682-80AB-92354C55A4C6}.Debug|x86.Build.0 = Debug|x86
{EF7540E1-F2E7-4682-80AB-92354C55A4C6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EF7540E1-F2E7-4682-80AB-92354C55A4C6}.Release|Any CPU.Build.0 = Release|Any CPU
{EF7540E1-F2E7-4682-80AB-92354C55A4C6}.Release|x86.ActiveCfg = Release|x86
{EF7540E1-F2E7-4682-80AB-92354C55A4C6}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
5 changes: 3 additions & 2 deletions FreeControl/Controller.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

115 changes: 90 additions & 25 deletions FreeControl/Controller.cs
Original file line number Diff line number Diff line change
@@ -1,47 +1,101 @@
using FreeControl.Utils;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;

namespace FreeControl
{
public partial class Controller : UIForm
{
[DllImport("user32.dll")]
public static extern bool GetWindowRect(IntPtr hWnd, out Rect lpRect);

[StructLayout(LayoutKind.Sequential)]
public struct Rect
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}

/// <summary>
/// 监听scrcpy窗体移动后的坐标
/// </summary>
private readonly System.Timers.Timer timer;
/// <summary>
/// scrcpy句柄
/// </summary>
private IntPtr scrcpyWindow = IntPtr.Zero;
/// <summary>
/// 进程名称
/// </summary>
private const string scrcpyWindowName = "scrcpy";
/// <summary>
/// 上一个有效的scrcpy坐标
/// </summary>
private Rect lastRect = new Rect();

public Controller()
{
InitializeComponent();
TopMost = true;
flowPanel.FlowLayoutPanel.MouseDown += (s, e) => DragWindow();
flowPanel.FlowLayoutPanel.AutoScroll = false;

InitButton();
InitFormSizeAndLocation();

timer = new System.Timers.Timer
{
Interval = 15
};
timer.Elapsed += OnTimedEvent;
timer.Enabled = true;
Disposed += (s, e) =>
{
Main._Setting.ScrcpyPointX = lastRect.Left;
Main._Setting.ScrcpyPointY = lastRect.Top;
};
}

public void StopTimer()
{
timer.Stop();
timer.Enabled = false;
}

private void OnTimedEvent(Object source, ElapsedEventArgs e)
{
Action action = () =>
{
if (scrcpyWindow == IntPtr.Zero)
{
Process[] processes = Process.GetProcessesByName(scrcpyWindowName);
scrcpyWindow = processes[0].MainWindowHandle;
}
GetWindowRect(scrcpyWindow, out Rect rect);
if (rect.Top == lastRect.Top && rect.Left == lastRect.Left)
return;
if (rect.Left + rect.Top > 0)
{
rect.Left += 8;
rect.Top += 31;
lastRect = rect;
Location = new Point(rect.Left - 57, rect.Top);
}
};
if (InvokeRequired)
Invoke(action);
}

/// <summary>
/// 初始化窗口大小和位置
/// </summary>
void InitFormSizeAndLocation()
{
if (Main._Setting.ControllerStartPointX == 0 && Main._Setting.ControllerStartPointY == 0)
{
StartPosition = FormStartPosition.CenterScreen;
}
else
{
StartPosition = FormStartPosition.Manual;
Location = new Point(Main._Setting.ControllerStartPointX, Main._Setting.ControllerStartPointY);
}
flowPanel.FlowLayoutPanel.AutoScroll = false;
FormBorderStyle = FormBorderStyle.FixedSingle;
if (Main._Setting.ControllerStartWidth >= 57 && Main._Setting.ControllerStartHeight >= 57)
{
Width = Main._Setting.ControllerStartWidth;
Expand All @@ -52,17 +106,28 @@ void InitFormSizeAndLocation()
Width = 140;
Height = 140;
}
LocationChanged += (sender, e) =>
if (Main._Setting.ScrcpyPointX + Main._Setting.ScrcpyPointY > 0)
{
Main._Setting.ControllerStartPointX = Location.X;
Main._Setting.ControllerStartPointY = Location.Y;
};
// Location = new Point(Main._Setting.ControllerStartPointX, Main._Setting.ControllerStartPointY);
StartPosition = FormStartPosition.Manual;
Location = new Point(Main._Setting.ScrcpyPointX - 57, Main._Setting.ScrcpyPointY);
}
else
{
StartPosition = FormStartPosition.CenterScreen;
}
// LocationChanged += (sender, e) =>
// {
// Main._Setting.ControllerStartPointX = Location.X;
// Main._Setting.ControllerStartPointY = Location.Y;
// };
SizeChanged += (sender, e) =>
{
Main._Setting.ControllerStartWidth = Width;
Main._Setting.ControllerStartHeight = Height;
};
MouseDown += (s, e) => DragWindow();
// MouseDown += (s, e) => DragWindow();
// flowPanel.FlowLayoutPanel.MouseDown += (s, e) => DragWindow();
}

/// <summary>
Expand Down
6 changes: 6 additions & 0 deletions FreeControl/Controller.resx
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,10 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="flowPanel.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="$this.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
</root>
20 changes: 19 additions & 1 deletion FreeControl/FreeControl.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
<IsWebBootstrapper>false</IsWebBootstrapper>
<TargetFrameworkProfile />
<PublishUrl>publish\</PublishUrl>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
Expand All @@ -26,7 +27,6 @@
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down Expand Up @@ -69,6 +69,24 @@
<PropertyGroup>
<ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x86\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x86</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<OutputPath>bin\x86\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x86</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<ItemGroup>
<Reference Include="SunnyUI, Version=2.2.6.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
Expand Down
38 changes: 19 additions & 19 deletions FreeControl/Main.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 27d3aaf

Please sign in to comment.