注册 登录
编程论坛 C# 论坛

简单封装scrcpy电脑usb操作手机的简单应用(适用Android、HarmonyOS、ColorOS等等)

qq2889577966 发布于 2022-11-07 20:13, 376 次点击
上一篇介绍了投屏的基本原理(https://bbs.bccn.net/thread-510506-1-1.html),也就是实现scrcpy的基本方法。
这一篇主要介绍简单封装scrcpy的简单应用,可点击电脑屏幕操作手机。
此方式兼容性较好,适用于Android、HarmonyOS、ColorOS等等兼容安卓的所有系统,苹果除外。有时间了下一篇介绍写写Apple。
将手机打开“开发人员模式”,然后“允许USB调试”,链接电脑,此过程不会的百度查。
有空了把设置脚本加上去,在电脑上刷金币视频、刷直播间评论等等,这些功能实现起来很简单,查百度可完成全部功能。

链接:https://pan.baidu.com/s/1zX41ZlX6Et6RD4ImGyoY6g
提取码:6wqx
只有本站会员才能查看附件,请 登录



程序代码:

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;

namespace p_Demo3
{
    public partial class Form1 : Form
    {
        private Process stdoutProcess = null;
        private const int SW_HIDE = 0;
        private const int SW_RESTORE = 9;

        public Form1()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.Manual;
            this.FormBorderStyle = FormBorderStyle.FixedSingle;

            this.LocationChanged += Form1_LocationChanged;
            this.FormClosed += Form1_FormClosed;
        }

        [DllImport("user32.dll", EntryPoint = "FindWindow")]
        private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll")]
        public static extern int ShowWindow(int hwnd, int nCmdShow);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int SendMessage(int hWnd, int msg, int wParam, int lparam);

        [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "MoveWindow")]

        public static extern bool MoveWindow(System.IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);

        private void Form1_LocationChanged(object sender, EventArgs e)
        {
            if (stdoutProcess != null)
            {
                MoveWindow(stdoutProcess.MainWindowHandle, this.Location.X + 30, this.Location.Y + 80, 500, 760, true);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            stdoutProcess = new Process();
            stdoutProcess.StartInfo.FileName = @"App\scrcpy.exe";
            stdoutProcess.StartInfo.Arguments = $"--always-on-top --window-title=SQK(my)-Demo --window-borderless --window-width=500 --window-height=760 --window-x="+(this.Location.X+30)+ "  --window-y="+(this.Location.Y+80);
            stdoutProcess.StartInfo.UseShellExecute = false;
            stdoutProcess.StartInfo.RedirectStandardOutput = true;
            stdoutProcess.StartInfo.CreateNoWindow = true;
            stdoutProcess.Start();
        }

        // 关闭程序
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            IntPtr hwnd = FindWindow(null, "SQK(my)-Demo");
            SendMessage((int)hwnd, 0x0010, 0, 0);
            Kill_Process("adb");
        }

        /// 删除进程
        private void Kill_Process(string processName)
        {
            foreach (Process p in Process.GetProcesses())
            {
                if (p.ProcessName.Contains(processName))
                {
                    try
                    {
                        p.Kill();
                        p.WaitForExit();
                    }
                    catch { }
                }
            }
        }
    }
}
1 回复
#2
龙胆草2022-11-09 07:42
感谢分享!
1