Winfrom: simple screen acquisition for Android phones and simple Android operations; winfrom android
Why do I need to do this? It is because projection demonstration apps are often used. There are a lot of such software that can be installed to place the mobile phone interface on a computer, for example, 360 mobile assistant, I hate installation, so I made a fuss about it myself,
Implemented the following simple functions:
1. Screen acquisition (a bit choppy because it is obtained in a way)
2. Click and a gesture icon appears when you click to make it easier for you to watch
3. Simple slide
4. Drawing on the Interface
5. Implement the drag-and-drop apk installation Function
Operation instructions: Click the simulated phone on the left side of the mouse, and click "Stop/start refreshing" (the page cannot be refreshed when drawing). Right-click to remove the drawing content.
Show
This is the main interface. The following figure shows the mobile phone model.
The second is to easily explain the above drawing function
The principle is implemented through abd.
Adb shell input keyevent 26 click power
This is the screenshot display code.
1 private void backgroundworker=dowork (object sender, DoWorkEventArgs e) 2 {3 while (true) 4 {5 if (isStop) 6 {7 return; 8} 9 // obtain the image 10 var tempFileName = "1.png"; 11 faster ADB (" shell screencap-p/sdcard/"+ tempFileName); 12 // pictureBox1.ImageLocation = Environment. currentDirectory + "\ temp \" + tempFileName; 13 uploadb ("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 try33 {34 System. IO. file. delete (tempFileName); 35} 36 catch37 {38 39} 40} 41 Thread. sleep (1000); 42 43} 44} 45}
View Code
This is the code for drawing clicks and sliding.
1 private void picturebox#mouseclick (object sender, MouseEventArgs e) 2 {3 if (e. button = System. windows. forms. mouseButtons. right) // Right-click to cancel painting 4 {5 this. refresh (); 6 return; 7} 8 if (e. button = MouseButtons. middle) // press the Middle mouse button to stop or start updating image 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 {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} 31 var tapx = multiplierX * e. x; // calculate the actual coordinate 32 var tapy = multiplierY * e. y; 33 bytes ADB (string. format ("shell input tap {0} {1}", tapx. toString ("0"), tapy. toString ("0"); // click coordinates 34} 35 36 private void picturebox#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 picturebox#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 ("execution" + isDraw); 58 bytes ADB (string. format ("shell input swipe {0: 0} {0} {2: 0} {3: 0} 100", StartX * multiplierX, StartY * multiplierY, e. X * multiplierX, e. Y * multiplierY); 59} 60} 61} 62 63 private void pictureboxinclusizechanged (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 {80g. drawLine (new Pen (Color. red, 2), new Point (lineStartX, lineStartY), new Point (e. x, e. y); 81g. dispose (); 82} 83 84 lineStartX = e. x; 85 lineStartY = e. y; 86 87 return; 88} 89}
View Code
Check whether the device exists
1 /// <summary> 2 /// check whether there is a mobile phone 3 /// </summary> 4 private void CheckHasAndroidModel () 5 {6 var text = javasadb ("shell getprop ro. product. model ", false); // obtain the phone model 7 Debug. writeLine ("check device:" + text + "T =" + DateTime. now); 8 if (text. contains ("no devices") | string. isNullOrWhiteSpace (text) 9 {10 HasAndroid = false; 11 isStop = true; 12 toolStripStatusLabel2.Text = "No detected device"; 13} 14 else15 {16 HasAndroid = true; 17 IsStop = false; 18 toolStripStatusLabel2.Text = text. Trim (); 19 if (! BackgroundWorker1.IsBusy) 20 {21 backgroundWorker1.RunWorkerAsync (); 22} 23} 24}
Rewrite WndProc method to listen for usb device insertion
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) // The device inserts or pulls out 7 {8 CheckHasAndroidModel (); 9 myTimer. start (); 10} 11} 12 try13 {14 base. wndProc (ref m); 15} 16 catch {} 17}
View Code
Final attachment code https://files.cnblogs.com/files/dotnet-org-cn/ShowAndroidModel.rar