Winfrom 簡單的安卓手機屏幕獲取和安卓簡單操做

 

爲啥我要作這個東西了,是由於常常要用投影演示app ,如今有不少這樣的軟件能夠把手機界面投到電腦上 ,但都要安裝,好比說360的手機助手,我又討厭安裝,因而就本身搗鼓了下 作了這個東西,shell

  實現瞭如下簡單功能app

     一、屏幕獲取(由於是截圖方式獲取的,因此有點卡頓)ide

   二、實現點擊功能,並在點擊的時候出現一個手勢圖標,方便用戶觀看this

    三、實現簡單的滑動功能spa

         四、實如今界面上畫圖功能code

         五、實現拖拽安裝apk功能orm

 操做說明:鼠標左邊 模擬手機點擊,中鍵中止/開始刷新界面(畫圖的時候不能刷新),右鍵去掉畫圖內容blog

顯示來看下效果圖圖片

   

 

這個就是主界面了,下面有顯示手機型號ip

其次是在上面畫圖功能方便講解

原理就是經過abd來實現的

 adb shell input keyevent 26  點擊power

這是截屏顯示圖片的代碼

 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         }
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

相關文章
相關標籤/搜索