Form1.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System.Diagnostics;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.Text;
  5. namespace WindowLock
  6. {
  7. public partial class Form1 : Form
  8. {
  9. public Form1()
  10. {
  11. InitializeComponent();
  12. }
  13. /// 启动tcp,接收wake命令后,唤醒屏幕
  14. private void button1_Click(object sender, EventArgs e)
  15. {
  16. Thread thread = new Thread(new ThreadStart(StartTcp));
  17. thread.Start();
  18. }
  19. private void StartTcp()
  20. {
  21. TcpListener listener = new TcpListener(IPAddress.Any, 8888);
  22. listener.Start();
  23. while (true)
  24. {
  25. TcpClient client = listener.AcceptTcpClient();
  26. NetworkStream stream = client.GetStream();
  27. byte[] buffer = new byte[1024];
  28. int length = stream.Read(buffer, 0, buffer.Length);
  29. string message = Encoding.UTF8.GetString(buffer, 0, length);
  30. if (message == "wake")
  31. {
  32. WakeUp();
  33. }
  34. stream.Close();
  35. client.Close();
  36. }
  37. }
  38. /// 蓝牙唤醒,接收到 wake 命令后,执行 WwakeUp 函数
  39. private void button2_Click(object sender, EventArgs e)
  40. {
  41. Thread thread = new Thread(new ThreadStart(StartBluetooth));
  42. thread.Start();
  43. }
  44. ///
  45. private void StartBluetooth()
  46. {
  47. /// 检测蓝牙是否打开
  48. BluetoothRadio radio = BluetoothRadio.PrimaryRadio;
  49. if (radio == null)
  50. {
  51. MessageBox.Show("蓝牙未打开");
  52. return;
  53. }
  54. BluetoothListener listener = new BluetoothListener(new Guid("00001101-0000-1000-8000-00805F9B34FB"));
  55. listener.Start();
  56. while (true)
  57. {
  58. BluetoothClient client = listener.AcceptBluetoothClient();
  59. NetworkStream stream = client.GetStream();
  60. byte[] buffer = new byte[1024];
  61. int length = stream.Read(buffer, 0, buffer.Length);
  62. string message = Encoding.UTF8.GetString(buffer, 0, length);
  63. if (message == "wake")
  64. {
  65. WakeUp();
  66. }
  67. stream.Close();
  68. client.Close();
  69. }
  70. }
  71. /// 屏幕唤醒
  72. private void WakeUp()
  73. {
  74. Process.Start("C:\\Windows\\System32\\scrnsave.scr", "/s");
  75. Thread.Sleep(1000);
  76. SendKeys.SendWait("{ENTER}");
  77. }
  78. }
  79. }