标题:简单封装scrcpy电脑usb操作手机的简单应用(适用Android、HarmonyOS、ColorO ...
取消只看楼主
qq2889577966
Rank: 4
等 级:业余侠客
威 望:5
帖 子:57
专家分:267
注 册:2021-4-14
结帖率:100%
已结贴  问题点数:20 回复次数:0 
简单封装scrcpy电脑usb操作手机的简单应用(适用Android、HarmonyOS、ColorOS等等)
上一篇介绍了投屏的基本原理(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 { }
                }
            }
        }
    }
}
搜索更多相关主题的帖子: int this private using System 
2022-11-07 20:13



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-510579-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.053897 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved