Advertisement
Guest User

Untitled

a guest
Aug 7th, 2022
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Net;  
  9. using System.Net.Sockets;
  10. using System.Threading;
  11. using System.Text;
  12. using System.Windows.Interop;
  13.  
  14. namespace AndAnotherBackend
  15. {
  16.     /// <summary>
  17.     /// Interaction logic for App.xaml
  18.     /// </summary>
  19.     public partial class App : Application
  20.     {
  21.         TcpListener server = null;
  22.         public void SocketServer()
  23.         {
  24.             Int32 Port = 11000;
  25.             IPAddress addr = IPAddress.Parse("0.0.0.0");
  26.            
  27.             server = new TcpListener(addr, Port);
  28.            
  29.             server.Start();
  30.  
  31.             Byte[] bytes = new Byte[256];
  32.             String data = null;
  33.  
  34.             while (true)
  35.             {
  36.                 Console.Write("Waiting for a connection... ");
  37.                 TcpClient client = server.AcceptTcpClient();
  38.                 Console.WriteLine("Connected!");
  39.                 NetworkStream stream = client.GetStream();
  40.                 int i;
  41.                 while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
  42.                 {
  43.                     data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
  44.                     Console.WriteLine("Received: {0}", data);
  45.                     MessageBox.Show(data);
  46.                     data = data.ToUpper();
  47.                     byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
  48.                     stream.Write(msg, 0, msg.Length);
  49.                     Console.WriteLine("Sent: {0}", data);
  50.                 }
  51.                 client.Close();
  52.             }
  53.         }
  54.  
  55.         public String AskPy(string cmd) {
  56.             // Here we build a TCP connection with the Python backend
  57.             TcpClient client = new TcpClient();
  58.             client.Connect(IPAddress.Loopback, 11001);
  59.             NetworkStream stream = client.GetStream();
  60.             byte[] msg = System.Text.Encoding.ASCII.GetBytes(cmd);
  61.             stream.Write(msg, 0, msg.Length);
  62.             byte[] bytes = new byte[256];
  63.             String data = null;
  64.             int i = stream.Read(bytes, 0, bytes.Length);
  65.             data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
  66.             client.Close();
  67.             return data;
  68.         }
  69.  
  70.         public App()
  71.         {
  72.             MainWindow holder = new MainWindow();
  73.             holder.Loaded += (sender, e) =>
  74.             {
  75.                 holder.Hide();
  76.                 holder.WindowState = WindowState.Minimized;
  77.                 holder.ShowInTaskbar = false;
  78.  
  79.                 Thread t = new Thread(new ThreadStart(SocketServer));
  80.                 t.Start();
  81.  
  82.                 int ScreenAmount = int.Parse(AskPy("monitor_amount"));
  83.                 int NextMove = 0;
  84.                 for(int i = 0; i < ScreenAmount; i++) {
  85.                     string Sizes = AskPy("monitor_size " + i.ToString());
  86.                     string[] SizesArray = Sizes.Split('x');
  87.                     int width = int.Parse(SizesArray[0]);
  88.                     int height = int.Parse(SizesArray[1]);
  89.                     MainWindow CurrentWindow = new MainWindow();
  90.                     CurrentWindow.Title = "ScreenHelper" + i.ToString();
  91.                     CurrentWindow.Show();
  92.                     IntPtr Handle = new WindowInteropHelper(CurrentWindow).Handle;
  93.                     MoveWindow(Handle, NextMove, 0, width, height, true);
  94.                     NextMove += width;
  95.                 }
  96.                 holder.webView = null;
  97.                 holder.Content = null;
  98.             };
  99.             this.Run(holder);
  100.         }
  101.  
  102.         [System.Runtime.InteropServices.DllImport("user32.dll")]
  103.         public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
  104.         [System.Runtime.InteropServices.DllImport("user32.dll")]
  105.         public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
  106.     }
  107. }
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement