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

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

qq2889577966 发布于 2022-11-07 20:13, 1644 次点击
上一篇介绍了投屏的基本原理(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 { }
                }
            }
        }
    }
}
3 回复
#2
龙胆草2022-11-09 07:42
感谢分享!
#3
邹生笑谈2023-09-29 22:05
(作为一个普通游客)
你的上一篇C# 手机使用USB投屏电脑(只适用Android),我下载demo文件后运行里面debug的exe,连接手机失败。表现为:在点击电脑窗口的按钮后,手机提示运行USB调试吗,点击允许后,电脑提示:无法关闭stdIN,进程(21220)已退出,因此无法处理请求。

但是这个demo能用。虽然没有自带的exe,但是我编译后有了。点击【开始】后,手机提示USb调试,【允许】后,再点击【开始】,就能显示手机屏幕了。这个真好用啊,还能鼠标操作。但是在电脑键盘输入手机内的文本框时,打字只能打进去英文,电脑的输入法是不起作用的,只能通过复制粘贴的方式粘贴中文。想电脑操控打中文字,得用鼠标点击手机的输入法键盘点击操作。

最后感谢分享
#4
qq28895779662023-09-30 22:04
回复 3楼 邹生笑谈
先谢谢关注,这个是刚开始学习Android+Usb写的,你所说的问题是播放器的问题,刚开始学么,总有这问题那问题的。下面的比较新。。跟公开发布的不太一样的,修改了好多地方了已经

下面这个是我自己目前在使用的,支持24个设备(可扩展),直接自动识别插拔设备,全自动,中文输入也改ADBKeyboard.apk了,还算是比较稳定吧.....,好吧,还有改进空间。
实际测试,一个空闲号码,满负荷400以上/月,低强度,最少200/月。关键脚本要写好,比较繁琐。
有全部源码,随意修改,由于自己用的,就不写说明了,自己摸索着看吧......,有什么问题告诉我可以改
另外:输入中文是在脚本里面编辑好一堆文字,系统按分行逐条自动写入相应位置,刷直播间用的...

链接:https://pan.baidu.com/s/1DtB2CExViQ8deiw8QvBvoQ
提取码:jrhw

[此贴子已经被作者于2023-9-30 22:28编辑过]

1