开发环境:
VS2022、.NET6
功能介绍:可以实现远程控制屏幕,F3解锁。
屏幕获取和操作的实现都是通过hdc命令实现的,截图然后转成流实现投屏的,所以会有一定延迟。网上也有其他语言的版本,我只是觉得C#打包方便才改写的,用于演示的时候使用的。(希望以后官方可以发布正式的api出来调用,还有什么修改壁纸啥的,非常期待)
MainWindow.xaml.cs代码:
using System;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
namespace OHScrcpy
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private string imagePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "scr.jpeg");
private long pressTime;
private BitmapImage originalImage;
private DispatcherTimer reloadTimer;
public MainWindow()
{
InitializeComponent();
this.Title = "OHScrcpy(By Lucky)";
this.MouseLeftButtonDown += MainWindow_MouseLeftButtonDown;
this.MouseLeftButtonUp += MainWindow_MouseLeftButtonUp;
this.Closed += (sender, e) => Application.Current.Shutdown();
// 初始化定时器
reloadTimer = new DispatcherTimer();
reloadTimer.Interval = TimeSpan.FromMilliseconds(30);
reloadTimer.Tick += ReloadTimer_Tick;
// 首次加载图片并设置窗口大小
UpdateImageAndReload();
// 启动定时器
reloadTimer.Start();
}
private void ReloadTimer_Tick(object sender, EventArgs e)
{
UpdateImageAndReload();
}
/// <summary>
/// 更新并重新加载图片
/// </summary>
private void UpdateImageAndReload()
{
UpdateImage();
LoadImage();
if (originalImage != null)
{
imageControl.Source = originalImage;
this.Width = originalImage.PixelWidth;
this.Height = originalImage.PixelHeight;
}
else
{
imageControl.Source = null;
MessageBox.Show("设备未连接或已断开连接!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
/// <summary>
/// 使用命令去截图并保存到本地
/// </summary>
private void UpdateImage()
{
// 删除本地图片
DeleteExistingImage();
try
{
// 执行外部命令更新图片
ExecuteCommand("hdc shell rm -rf /data/local/tmp/scr.jpeg & hdc shell snapshot_display -f /data/local/tmp/scr.jpeg & hdc file recv /data/local/tmp/scr.jpeg");
}
catch (Exception ex)
{
LogError(ex.Message);
}
}
/// <summary>
/// 加载图片
/// </summary>
private void LoadImage()
{
try
{
if (File.Exists(imagePath))
{
using (FileStream stream = File.OpenRead(imagePath))
{
originalImage = new BitmapImage();
originalImage.BeginInit();
originalImage.CacheOption = BitmapCacheOption.OnLoad;
originalImage.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
originalImage.StreamSource = stream;
originalImage.EndInit();
}
}
else
{
originalImage = null;
}
}
catch (Exception ex)
{
LogError(ex.Message);
originalImage = null;
}
}
/// <summary>
/// 鼠标点击事件
/// 配合命令去执行点击,长按等操作
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MainWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
pressTime = DateTimeOffset.Now.ToUnixTimeMilliseconds();
}
private void MainWindow_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
long releaseTime = DateTimeOffset.Now.ToUnixTimeMilliseconds();
if (originalImage != null)
{
double scaleX = originalImage.PixelWidth / imageControl.ActualWidth;
double scaleY = originalImage.PixelHeight / imageControl.ActualHeight;
int realX = (int)(e.GetPosition(imageControl).X * scaleX);
int realY = (int)(e.GetPosition(imageControl).Y * scaleY);
if (releaseTime - pressTime > 300)
{
// 长按
Console.WriteLine($"真实的长按坐标: ({realX}, {realY})");
ExecuteCommand($"hdc shell uinput -T -d {realX} {realY}");
}
else
{
// 点击
Console.WriteLine($"真实的点击坐标: ({realX}, {realY})");
ExecuteCommand($"hdc shell uinput -T -c {realX} {realY}");
}
}
}
/// <summary>
/// 屏幕解锁方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.F3)
{
string command = $"hdc shell uinput -T -m 923 1023 906 583";
ExecuteCommand(command);
}
}
/// <summary>
/// 执行生成命令
/// </summary>
/// <param name="command"></param>
private void ExecuteCommand(string command)
{
try
{
ProcessStartInfo psi = new ProcessStartInfo("cmd.exe", $"/c {command}");
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
Process p = Process.Start(psi);
p.WaitForExit();
}
catch (Exception ex)
{
LogError(ex.Message);
}
}
/// <summary>
/// 检查删除本地图片
/// </summary>
private void DeleteExistingImage()
{
try
{
if (File.Exists(imagePath))
{
File.Delete(imagePath);
}
}
catch (Exception ex)
{
LogError(ex.Message);
}
}
/// <summary>
/// 打印日志
/// </summary>
/// <param name="message"></param>
private void LogError(string message)
{
Console.WriteLine($"Error: {message}");
}
}
}
MainWindow.xaml代码:
<Window x:Class="OHScrcpy.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:OHScrcpy"
mc:Ignorable="d"
SizeToContent="WidthAndHeight"
KeyDown="Window_KeyDown">
<Grid>
<Image x:Name="imageControl" Stretch="Uniform" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
</Window>
最后如果有码友有新的更好的实现方式的话,请也分享一下给我,先提前感谢一下。