Advertisement
Jakobhorak28

Untitled

Apr 12th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.09 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16. using System.Threading;
  17. using System.Drawing;
  18.  
  19. namespace Mousemove_advanced
  20. {
  21.     /// <summary>
  22.     /// Interaktionslogik für MainWindow.xaml
  23.     /// </summary>
  24.     public partial class MainWindow : Window
  25.     {
  26.         Thread mousem, updatepb;
  27.         int value = 0;
  28.         string filePath;
  29.         byte[] bytes = new byte[5];
  30.         FileStream stream;
  31.  
  32.         public MainWindow()
  33.         {
  34.             InitializeComponent();
  35.             filePath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
  36.             if (File.Exists(filePath + @"\MouseMove\TimeSetting.txt"))
  37.             {
  38.                 cbSave.IsChecked = true;
  39.                 stream = new FileStream(filePath + @"\MouseMove\TimeSetting.txt", FileMode.Open);
  40.                 stream.Read(bytes, 0, 5);
  41.                 Slider.Value = bytes[0] + bytes[1] + bytes[2] + bytes[3] + bytes[4];
  42.             }
  43.         }
  44.  
  45.         private void btStart_Click(object sender, RoutedEventArgs e)
  46.         {
  47.             mousem = new Thread(new ParameterizedThreadStart(MouseM));
  48.             mousem.Start(value);
  49.             updatepb = new Thread(new ParameterizedThreadStart(UpdatePB));
  50.             updatepb.Start(value);
  51.             cvIndication.Background = System.Windows.Media.Brushes.Green;
  52.             WindowState = WindowState.Minimized;
  53.             btStart.Visibility = Visibility.Hidden;
  54.             btStop.Visibility = Visibility.Visible;
  55.         }
  56.  
  57.         private static void MouseM(object sleepTime)
  58.         {
  59.             try
  60.             {
  61.                 System.Drawing.Point oldpoint;
  62.                 int elapsed;
  63.                 while (true)
  64.                 {
  65.                     elapsed = 0;
  66.                     do
  67.                     {
  68.                         oldpoint = new System.Drawing.Point(System.Windows.Forms.Cursor.Position.X, System.Windows.Forms.Cursor.Position.Y);
  69.                         Thread.Sleep(100);
  70.                         if (oldpoint == new System.Drawing.Point(System.Windows.Forms.Cursor.Position.X, System.Windows.Forms.Cursor.Position.Y))
  71.                             elapsed += 100;
  72.                         else
  73.                             elapsed = 0;
  74.                     } while (elapsed < (int)sleepTime * 985);
  75.                     System.Windows.Forms.Cursor.Position = new System.Drawing.Point(System.Windows.Forms.Cursor.Position.X - 1, System.Windows.Forms.Cursor.Position.Y);
  76.                     Thread.Sleep(10);
  77.                     System.Windows.Forms.Cursor.Position = new System.Drawing.Point(System.Windows.Forms.Cursor.Position.X + 1, System.Windows.Forms.Cursor.Position.Y);
  78.                 }
  79.             }
  80.             catch (Exception e) { }
  81.             finally { }
  82.         }
  83.  
  84.         private void UpdatePB(object sleepTime)
  85.         {
  86.             try
  87.             {
  88.                 System.Drawing.Point oldpoint;
  89.                 int elapsed;
  90.                 while (true)
  91.                 {
  92.                     SetValue(0.0);
  93.                     elapsed = 0;
  94.                     do
  95.                     {
  96.                         oldpoint = new System.Drawing.Point(System.Windows.Forms.Cursor.Position.X, System.Windows.Forms.Cursor.Position.Y);
  97.                         Thread.Sleep(100);
  98.                         if (oldpoint == new System.Drawing.Point(System.Windows.Forms.Cursor.Position.X, System.Windows.Forms.Cursor.Position.Y))
  99.                             elapsed += 100;
  100.                         else
  101.                             elapsed = 0;
  102.                         if ((double)elapsed / ((int)sleepTime * 985) <= 1)
  103.                             SetValue(Math.Round((double)elapsed / ((int)sleepTime * 985), 3));
  104.                     } while (elapsed < (int)sleepTime * 985);
  105.                 }
  106.             }
  107.             catch (Exception e) { SetValue(0); }
  108.             finally { }
  109.         }
  110.  
  111.         private void SetValue(double value)
  112.         {
  113.             Dispatcher.Invoke(() =>
  114.             {
  115.                 pbLoading.Value = value;
  116.                 lbElapsed.Content = $"{(value * 100).ToString("0.0")}%";
  117.             });
  118.         }
  119.  
  120.         private void btStop_Click(object sender, RoutedEventArgs e)
  121.         {
  122.             mousem.Interrupt();
  123.             mousem.Join(100);
  124.             updatepb.Interrupt();
  125.             updatepb.Join(100);
  126.             cvIndication.Background = System.Windows.Media.Brushes.Red;
  127.             btStop.Visibility = Visibility.Hidden;
  128.             btStart.Visibility = Visibility.Visible;
  129.         }
  130.  
  131.         private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  132.         {
  133.             if (lbTime != null)
  134.             {
  135.                 lbTime.Content = $"{(int)Slider.Value / 60}min {(int)Slider.Value % 60}s";
  136.                 value = (int)Slider.Value;
  137.             }
  138.         }
  139.  
  140.         private void toByteArr(byte[] arr, int convvalue)
  141.         {
  142.             int rev = 0;
  143.             while (convvalue > 0)
  144.             {
  145.                 if (convvalue > 255)
  146.                 {
  147.                     convvalue -= 255;
  148.                     arr[rev] = 255;
  149.                 }
  150.                 else
  151.                 {
  152.                     arr[rev] = (byte)convvalue;
  153.                     convvalue = 0;
  154.                 }
  155.                 rev++;
  156.             }
  157.         }
  158.  
  159.         private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  160.         {
  161.             try
  162.             { stream.Close(); }
  163.             catch (Exception a) { }
  164.             try
  165.             {
  166.                 mousem.Interrupt();
  167.                 mousem.Join(100);
  168.             }
  169.             catch (Exception a) { }
  170.             try
  171.             {
  172.                 updatepb.Interrupt();
  173.                 updatepb.Join(100);
  174.             }
  175.             catch (Exception a) { }
  176.             if (cbSave.IsChecked == true)
  177.             {
  178.                 if (File.Exists(filePath + @"\MouseMove\TimeSetting.txt"))
  179.                     File.Delete(filePath + @"\MouseMove\TimeSetting.txt");
  180.                 if (!Directory.Exists(filePath + @"\MouseMove"))
  181.                     Directory.CreateDirectory(filePath + @"\MouseMove");
  182.                 stream = new FileStream(filePath + @"\MouseMove\TimeSetting.txt", FileMode.Create);
  183.                 bytes = new byte[5];
  184.                 for (int i = 0; i < 5; i++)
  185.                     bytes[i] = 0;
  186.                 toByteArr(bytes, value);
  187.                 stream.Write(bytes, 0, 5);
  188.                 stream.Close();
  189.             }
  190.             else
  191.             {
  192.                 try
  193.                 { Directory.Delete(filePath + @"\MouseMove", true); }
  194.                 catch (DirectoryNotFoundException a) { }
  195.                 finally { }
  196.             }
  197.         }
  198.     }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement