黄沙百战穿金甲,不破楼兰终不还。这篇文章主要讲述Winfrom 简单的安卓手机屏幕获取和安卓简单操作相关的知识,希望能为你提供帮助。
为啥我要做这个东西了,是因为经常要用投影演示app ,现在有很多这样的软件可以把手机界面投到电脑上 ,但都要安装,比如说360的手机助手,我又讨厌安装,于是就自己捣鼓了下 做了这个东西,
实现了以下简单功能
1、屏幕获取(因为是截图方式获取的,所以有点卡顿)
2、实现点击功能,并在点击的时候出现一个手势图标,方便用户观看
3、实现简单的滑动功能
4、实现在界面上画图功能
5、实现拖拽安装apk功能
操作说明:鼠标左边 模拟手机点击,中键停止/开始刷新界面(画图的时候不能刷新),右键去掉画图内容
显示来看下效果图
文章图片
这个就是主界面了,下面有显示手机型号
其次是在上面画图功能方便讲解
文章图片
原理就是通过abd来实现的
adb shell input keyevent 26 点击power
这是截屏显示图片的代码
文章图片
文章图片
1private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 2{ 3while (true) 4{ 5if (isStop) 6{ 7return; 8} 9//死循环截屏获取图片 10var tempFileName = "1.png"; 11cmdAdb("shell screencap -p /sdcard/" + tempFileName); 12// pictureBox1.ImageLocation = Environment.CurrentDirectory + "\\\\temp\\\\" + tempFileName; 13cmdAdb("pull /sdcard/" + tempFileName); 14if (System.IO.File.Exists(tempFileName)) 15{ 16//pictureBox1.BackgroundImage = new Bitmap(tempFileName); 17using (var temp = Image.FromFile(tempFileName)) 18{ 19 20pictureBox1.Invoke(new Action(() => { 21pictureBox1.Image = new Bitmap(temp); 22})); 23} 24if (multiplierX == 0) 25{ 26multiplierX = pictureBox1.Image.Width / (pictureBox1.Width + 0.00); 27multiplierY = pictureBox1.Image.Height / (pictureBox1.Height + 0.00); 28} 29GC.Collect(); 30if (System.IO.File.Exists(tempFileName)) 31{ 32try 33{ 34System.IO.File.Delete(tempFileName); 35} 36catch 37{ 38 39} 40} 41Thread.Sleep(1000); 42 43} 44} 45}
View Code这个是画图点击以及滑动的代码
文章图片
文章图片
1private void pictureBox1_MouseClick(object sender, MouseEventArgs e) 2{ 3if (e.Button == System.Windows.Forms.MouseButtons.Right)//鼠标右键 撤销画画 4{ 5this.Refresh(); 6return; 7} 8if (e.Button == MouseButtons.Middle)//鼠标中键按下 停止或开始更新图像 9{ 10isStop = !isStop; 11if (!isStop & & Hasandroid) 12{ 13backgroundWorker1.RunWorkerAsync(); 14} 15return; 16} 17if (isDraw) 18{ 19isDraw = false; 20return; 21} 22if (pictureBox1.Image == null) 23{ 24return; 25} 26using (Graphics g = pictureBox1.CreateGraphics()) 27{ 28g.DrawImage(ShowAndroidModel.Properties.Resources.shou, e.X - Convert.ToInt32(50 / multiplierX), e.Y - Convert.ToInt32(10 / multiplierY), Convert.ToInt32(150 / multiplierX), Convert.ToInt32(150 / multiplierY)); 29g.Dispose(); 30} 31var tapx = multiplierX * e.X; //计算实际坐标 32var tapy = multiplierY * e.Y; 33cmdAdb(string.Format("shell input tap {0} {1}", tapx.ToString("0"), tapy.ToString("0"))); //点击坐标 34} 35 36private void pictureBox1_MouseDown(object sender, MouseEventArgs e) 37{ 38if (e.Button == System.Windows.Forms.MouseButtons.Left) 39{ 40lineStartX = e.X; 41lineStartY = e.Y; 42StartX = e.X; 43StartY = e.Y; 44_MouseState = MouseState.MouseLeftDown; 45return; 46} 47} 48 49private void pictureBox1_MouseUp(object sender, MouseEventArgs e) 50{ 51_MouseState = MouseState.None; 52if (e.Button == System.Windows.Forms.MouseButtons.Left) 53{ 54if (StartX - e.X > 50 || StartX - e.X < -50 || StartY - e.Y > 50 || StartY - e.Y < -50) 55{ 56isDraw = true; 57Debug.WriteLine("执行" + isDraw); 58cmdAdb(string.Format("shell input swipe {0:0} {1:0} {2:0} {3:0} 100", StartX * multiplierX, StartY * multiplierY, e.X * multiplierX, e.Y * multiplierY)); 59} 60} 61} 62 63private void pictureBox1_SizeChanged(object sender, EventArgs e) 64{ 65multiplierX = pictureBox1.Image.Width / (pictureBox1.Width + 0.00); 66multiplierY = pictureBox1.Image.Height / (pictureBox1.Height + 0.00); 67} 68 69private void pictureBox1_MouseMove(object sender, MouseEventArgs e) 70{ 71if (_MouseState == MouseState.None) 72{ 73return; 74} 75if (_MouseState == MouseState.MouseLeftDown) 76{ 77isDraw = true; 78using (Graphics g = pictureBox1.CreateGraphics()) 79{ 80g.DrawLine(new Pen(Color.Red, 2), new Point(lineStartX, lineStartY), new Point(e.X, e.Y)); 81g.Dispose(); 82} 83 84lineStartX = e.X; 85lineStartY = e.Y; 86 87return; 88} 89}
View Code检测设备是否存在
1/// < summary> 2/// 检测是否存在手机 3/// < /summary> 4private void CheckHasAndroidModel() 5{ 6var text = cmdAdb("shell getprop ro.product.model",false); //获取手机型号 7Debug.WriteLine("检查设备:" + text+"T="+DateTime.Now); 8if (text.Contains("no devices")||string.IsNullOrWhiteSpace(text)) 9{ 10HasAndroid = false; 11isStop = true; 12toolStripStatusLabel2.Text="未检测到设备"; 13} 14else 15{ 16HasAndroid = true; 17isStop = false; 18toolStripStatusLabel2.Text = text.Trim(); 19if (!backgroundWorker1.IsBusy) 20{ 21backgroundWorker1.RunWorkerAsync(); 22} 23} 24}
重写WndProc方法监听usb设备插入
文章图片
文章图片
1protected override void WndProc(ref Message m) 2{ 3if (m.Msg == 0x219) 4{ 5Debug.WriteLine("WParam:{0} ,LParam:{1},Msg:{2},Result:{3}", m.WParam, m.LParam, m.Msg, m.Result); 6if (m.WParam.ToInt32() == 7)//设备插入或拔出 7{ 8CheckHasAndroidModel(); 9myTimer.Start(); 10} 11} 12try 13{ 14base.WndProc(ref m); 15} 16catch { } 17}
View Code最后附代码 https://files.cnblogs.com/files/dotnet-org-cn/ShowAndroidModel.rar
【Winfrom 简单的安卓手机屏幕获取和安卓简单操作】
推荐阅读
- Android中的AlertDialog遇到的错误
- 全新一点公益全返公益APP开发
- appuim-java,同时连接多台机器,启动微信
- 在Web.config或App.config中的添加自定义配置
- Android开发之入口Activity
- MyBatis框架中Mapper映射配置的使用及原理解析MapperProxy,MapperProxyFactory
- Firefly-RK3288开发板Android编译环境搭建开荒
- This Jenkins instance appears to be offline
- Join 和 App