Advertisement
Guest User

C# file

a guest
Jul 13th, 2020
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.59 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO.Ports;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using System.Runtime.InteropServices;
  12.  
  13. namespace Arduino_W
  14. {
  15. public partial class Form1 : Form
  16. {
  17. public Form1()
  18. {
  19. InitializeComponent();
  20. }
  21. Boolean act = false;
  22. private void Form1_Load(object sender, EventArgs e)
  23. {
  24. string[] ports = SerialPort.GetPortNames();
  25. portBox.Items.AddRange(ports);
  26. brBox.SelectedIndex = 0;
  27. }
  28.  
  29. private void startstopBtn_Click(object sender, EventArgs e)
  30. {
  31. if(startstopBtn.Text == "Start")
  32. {
  33. try
  34. {
  35. sp.PortName = portBox.Text;
  36. sp.BaudRate = Int32.Parse(brBox.Text);
  37. sp.Open();
  38. }
  39. catch(Exception ex)
  40. {
  41. MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
  42. }
  43. if(sp.IsOpen)
  44. {
  45. act = true;
  46. startstopBtn.Text = "Stop";
  47. sendBtn.Enabled = true;
  48. }
  49. }
  50. else
  51. {
  52. act = false;
  53. startstopBtn.Text = "Start";
  54. if(sp.IsOpen)
  55. {
  56. sp.Close();
  57. }
  58. }
  59. }
  60.  
  61. private void portBox_SelectedIndexChanged(object sender, EventArgs e)
  62. {
  63. if(portBox.Text != "") startstopBtn.Enabled = true;
  64. }
  65.  
  66. private void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
  67. {
  68. string data = sp.ReadLine();
  69.  
  70. if(data[0] == 'k') //key
  71. {
  72. SendKeys.SendWait(data.Substring(2, data.Length - 2));
  73. }
  74. else if(data[0] == 'm') //mouse
  75. {
  76. string ev = data.Substring(2, data.Length - 2);
  77.  
  78. switch (ev) {
  79. case "lu":
  80. MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.LeftUp); break;
  81. case "ld":
  82. MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.LeftDown); break;
  83. case "mu":
  84. MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.MiddleUp); break;
  85. case "md":
  86. MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.MiddleDown); break;
  87. case "ru":
  88. MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.RightUp); break;
  89. case "rd":
  90. MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.RightDown); break;
  91. }
  92. }
  93. }
  94. private void sendBtn_Click(object sender, EventArgs e)
  95. {
  96. sp.WriteLine(sendTxt.Text);
  97. sendTxt.Text = "";
  98. }
  99.  
  100. private void button1_Click(object sender, EventArgs e)
  101. {
  102. portBox.Items.AddRange(SerialPort.GetPortNames());
  103. }
  104.  
  105. private void showtimeBox_CheckedChanged(object sender, EventArgs e)
  106. {
  107.  
  108. }
  109.  
  110. private void readWnd_TextChanged(object sender, EventArgs e)
  111. {
  112.  
  113. }
  114. }
  115. public class MouseOperations
  116. {
  117. [Flags]
  118. public enum MouseEventFlags
  119. {
  120. LeftDown = 0x00000002,
  121. LeftUp = 0x00000004,
  122. MiddleDown = 0x00000020,
  123. MiddleUp = 0x00000040,
  124. Move = 0x00000001,
  125. Absolute = 0x00008000,
  126. RightDown = 0x00000008,
  127. RightUp = 0x00000010
  128. }
  129.  
  130. [DllImport("user32.dll", EntryPoint = "SetCursorPos")]
  131. [return: MarshalAs(UnmanagedType.Bool)]
  132. private static extern bool SetCursorPos(int x, int y);
  133.  
  134. [DllImport("user32.dll")]
  135. [return: MarshalAs(UnmanagedType.Bool)]
  136. private static extern bool GetCursorPos(out MousePoint lpMousePoint);
  137.  
  138. [DllImport("user32.dll")]
  139. private static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
  140.  
  141. public static void SetCursorPosition(int x, int y)
  142. {
  143. SetCursorPos(x, y);
  144. }
  145.  
  146. public static void SetCursorPosition(MousePoint point)
  147. {
  148. SetCursorPos(point.X, point.Y);
  149. }
  150.  
  151. public static MousePoint GetCursorPosition()
  152. {
  153. MousePoint currentMousePoint;
  154. var gotPoint = GetCursorPos(out currentMousePoint);
  155. if (!gotPoint) { currentMousePoint = new MousePoint(0, 0); }
  156. return currentMousePoint;
  157. }
  158.  
  159. public static void MouseEvent(MouseEventFlags value)
  160. {
  161. MousePoint position = GetCursorPosition();
  162.  
  163. mouse_event
  164. ((int)value,
  165. position.X,
  166. position.Y,
  167. 0,
  168. 0)
  169. ;
  170. }
  171.  
  172. [StructLayout(LayoutKind.Sequential)]
  173. public struct MousePoint
  174. {
  175. public int X;
  176. public int Y;
  177.  
  178. public MousePoint(int x, int y)
  179. {
  180. X = x;
  181. Y = y;
  182. }
  183. }
  184. }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement