为啥我要做这个东西了,是因为经常要用投影演示app ,现在有很多这样的软件可以把手机界面投到电脑上 ,但都要安装,比如说360的手机助手,我又讨厌安装,于是就自己捣鼓了下 做了这个东西,
实现了以下简单功能
1、屏幕获取(因为是截图方式获取的,所以有点卡顿)
2、实现点击功能,并在点击的时候出现一个手势图标,方便用户观看
3、实现简单的滑动功能
4、实现在界面上画图功能
5、实现拖拽安装apk功能
操作说明:鼠标左边 模拟手机点击,中键停止/开始刷新界面(画图的时候不能刷新),右键去掉画图内容
显示来看下效果图
这个就是主界面了,下面有显示手机型号
其次是在上面画图功能方便讲解
原理就是通过abd来实现的
adb shell input keyevent 26 点击power
这是截屏显示图片的代码
class="code_img_closed" src="/Upload/Images/2017091922/0015B68B3C38AA5B.gif" alt="">1 private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 2 { 3 while (true) 4 { 5 if (isStop) 6 { 7 return; 8 } 9 //死循环截屏获取图片 10 var tempFileName = "1.png"; 11 cmdAdb("shell screencap -p /sdcard/" + tempFileName); 12 // pictureBox1.ImageLocation = Environment.CurrentDirectory + "\\temp\\" + tempFileName; 13 cmdAdb("pull /sdcard/" + tempFileName); 14 if (System.IO.File.Exists(tempFileName)) 15 { 16 //pictureBox1.BackgroundImage = new Bitmap(tempFileName); 17 using (var temp = Image.FromFile(tempFileName)) 18 { 19 20 pictureBox1.Invoke(new Action(() => { 21 pictureBox1.Image = new Bitmap(temp); 22 })); 23 } 24 if (multiplierX == 0) 25 { 26 multiplierX = pictureBox1.Image.Width / (pictureBox1.Width + 0.00); 27 multiplierY = pictureBox1.Image.Height / (pictureBox1.Height + 0.00); 28 } 29 GC.Collect(); 30 if (System.IO.File.Exists(tempFileName)) 31 { 32 try 33 { 34 System.IO.File.Delete(tempFileName); 35 } 36 catch 37 { 38 39 } 40 } 41 Thread.Sleep(1000); 42 43 } 44 } 45 }logs_code_collapse">View Code
这个是画图点击以及滑动的代码
1 private void pictureBox1_MouseClick(object sender, MouseEventArgs e) 2 { 3 if (e.Button == System.Windows.Forms.MouseButtons.Right)//鼠标右键 撤销画画 4 { 5 this.Refresh(); 6 return; 7 } 8 if (e.Button == MouseButtons.Middle)//鼠标中键按下 停止或开始更新图像 9 { 10 isStop = !isStop; 11 if (!isStop && HasAndroid) 12 { 13 backgroundWorker1.RunWorkerAsync(); 14 } 15 return; 16 } 17 if (isDraw) 18 { 19 isDraw = false; 20 return; 21 } 22 if (pictureBox1.Image == null) 23 { 24 return; 25 } 26 using (Graphics g = pictureBox1.CreateGraphics()) 27 { 28 g.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)); 29 g.Dispose(); 30 } 31 var tapx = multiplierX * e.X;//计算实际坐标 32 var tapy = multiplierY * e.Y; 33 cmdAdb(string.Format("shell input tap {0} {1}", tapx.ToString("0"), tapy.ToString("0")));//点击坐标 34 } 35 36 private void pictureBox1_MouseDown(object sender, MouseEventArgs e) 37 { 38 if (e.Button == System.Windows.Forms.MouseButtons.Left) 39 { 40 lineStartX = e.X; 41 lineStartY = e.Y; 42 StartX = e.X; 43 StartY = e.Y; 44 _MouseState = MouseState.MouseLeftDown; 45 return; 46 } 47 } 48 49 private void pictureBox1_MouseUp(object sender, MouseEventArgs e) 50 { 51 _MouseState = MouseState.None; 52 if (e.Button == System.Windows.Forms.MouseButtons.Left) 53 { 54 if (StartX - e.X > 50 || StartX - e.X < -50 || StartY - e.Y > 50 || StartY - e.Y < -50) 55 { 56 isDraw = true; 57 Debug.WriteLine("执行" + isDraw); 58 cmdAdb(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 63 private void pictureBox1_SizeChanged(object sender, EventArgs e) 64 { 65 multiplierX = pictureBox1.Image.Width / (pictureBox1.Width + 0.00); 66 multiplierY = pictureBox1.Image.Height / (pictureBox1.Height + 0.00); 67 } 68 69 private void pictureBox1_MouseMove(object sender, MouseEventArgs e) 70 { 71 if (_MouseState == MouseState.None) 72 { 73 return; 74 } 75 if (_MouseState == MouseState.MouseLeftDown) 76 { 77 isDraw = true; 78 using (Graphics g = pictureBox1.CreateGraphics()) 79 { 80 g.DrawLine(new Pen(Color.Red, 2), new Point(lineStartX, lineStartY), new Point(e.X, e.Y)); 81 g.Dispose(); 82 } 83 84 lineStartX = e.X; 85 lineStartY = e.Y; 86 87 return; 88 } 89 }View Code
检测设备是否存在
1 /// <summary> 2 /// 检测是否存在手机 3 /// </summary> 4 private void CheckHasAndroidModel() 5 { 6 var text = cmdAdb("shell getprop ro.product.model",false);//获取手机型号 7 Debug.WriteLine("检查设备:" + text+" T="+DateTime.Now); 8 if (text.Contains("no devices")||string.IsNullOrWhiteSpace(text)) 9 { 10 HasAndroid = false; 11 isStop = true; 12 toolStripStatusLabel2.Text="未检测到设备"; 13 } 14 else 15 { 16 HasAndroid = true; 17 isStop = false; 18 toolStripStatusLabel2.Text = text.Trim(); 19 if (!backgroundWorker1.IsBusy) 20 { 21 backgroundWorker1.RunWorkerAsync(); 22 } 23 } 24 }
重写WndProc方法监听usb设备插入
1 protected override void WndProc(ref Message m) 2 { 3 if (m.Msg == 0x219) 4 { 5 Debug.WriteLine("WParam:{0} ,LParam:{1},Msg:{2},Result:{3}", m.WParam, m.LParam, m.Msg, m.Result); 6 if (m.WParam.ToInt32() == 7)//设备插入或拔出 7 { 8 CheckHasAndroidModel(); 9 myTimer.Start(); 10 } 11 } 12 try 13 { 14 base.WndProc(ref m); 15 } 16 catch { } 17 }View Code
最后附代码 https://files.cnblogs.com/files/dotnet-org-cn/ShowAndroidModel.rar