Advertisement
duel_05

Untitled

Aug 21st, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.51 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.Drawing;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Windows.Forms;
  11. using Accord;
  12. using Accord.Video;
  13.  
  14. namespace overlay
  15. {
  16.     public partial class overlayform : Form
  17.     {
  18.         public static string gamename = "Subnautica";
  19.         Process p = new Process();
  20.  
  21.         public struct RECT
  22.         {
  23.             public int left, top, right, bottom;
  24.         }
  25.         public struct WINDOWPLACEMENT
  26.         {
  27.             public int length;
  28.             public int flags;
  29.             public int showCmd;
  30.             public System.Drawing.Point ptMinPosition;
  31.             public System.Drawing.Point ptMaxPosition;
  32.             public System.Drawing.Rectangle rcNormalPosition;
  33.         }
  34.  
  35.  
  36.  
  37.  
  38.         //public Point MousePosition = Cursor.Position;
  39.  
  40.  
  41.         RECT outrect;
  42.         Graphics g;
  43.         public bool RecordVideo = true;
  44.         public ScreenCaptureStream stream;
  45.  
  46.         #region DLL Imports
  47.  
  48.         [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
  49.         static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);
  50.  
  51.         [DllImport("user32.dll")]
  52.         static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
  53.  
  54.         [DllImport("user32.dll", SetLastError = true)]
  55.         static extern int GetWindowLong(IntPtr window, int index);
  56.         IntPtr handle = FindWindowByCaption(IntPtr.Zero, gamename);
  57.  
  58.         [DllImport("user32.dll")]
  59.         static extern IntPtr GetForegroundWindow();
  60.  
  61.         [DllImport("user32.dll", SetLastError = true)]
  62.         static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);
  63.  
  64.         [DllImport("user32.dll")]
  65.         [return: MarshalAs(UnmanagedType.Bool)]
  66.         static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
  67.  
  68.         #endregion
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.         public overlayform()
  76.         {
  77.             InitializeComponent();
  78.         }
  79.  
  80.  
  81.  
  82.         public void WatchMouse()
  83.         {
  84.             //while (true)
  85.            // {
  86.                 //MousePosition = Cursor.Position;
  87.            // }          
  88.         }
  89.  
  90.        
  91.  
  92.  
  93.  
  94.         private void overlayform_Load(object sender, EventArgs e)
  95.         {
  96.  
  97.             Thread startGameThread = new Thread(new ThreadStart(StartGame));
  98.             startGameThread.Start();
  99.             Thread WatchMouseThread = new Thread(new ThreadStart(WatchMouse));
  100.             WatchMouseThread.Start();
  101.          
  102.  
  103.  
  104.  
  105.             //Process.Start(@"C:\Program Files (x86)\Steam\steamapps\common\Subnautica\Subnautica.exe");
  106.  
  107.  
  108.             //Thread.Sleep(3000);
  109.             //Thread.Sleep(3000);
  110.  
  111.  
  112.             this.SetStyle(
  113.             ControlStyles.AllPaintingInWmPaint |
  114.             ControlStyles.UserPaint |
  115.             ControlStyles.DoubleBuffer,
  116.             true);
  117.  
  118.            
  119.            
  120.  
  121.             /* Making the RECT object that will handle info from WINApi function. */
  122.  
  123.             /* Saving data to that object */
  124.             GetWindowRect(handle, out outrect);
  125.  
  126.             /* Change the size of the form to the size of the Game window. */
  127.             this.Size = new Size(outrect.right - outrect.left, outrect.bottom - outrect.top);
  128.  
  129.  
  130.  
  131.             /* Change the position of the form to the position of the Game window. */
  132.             this.Top = outrect.top;
  133.             this.Left = outrect.left;
  134.  
  135.             /* Change form's style to make it transparent and to remove the border */
  136.             //this.FormBorderStyle = FormBorderStyle.None;
  137.             this.BackColor = System.Drawing.Color.Black;
  138.             this.TransparencyKey = System.Drawing.Color.Black;
  139.             this.TopMost = true;
  140.  
  141.             /* Using WINApi function GetWindowLong and SetWindowLong to be able to click throught the form */
  142.             int initialStyle = GetWindowLong(this.Handle, -20);
  143.             SetWindowLong(this.Handle, -20, initialStyle | 0x80000 | 0x20);
  144.  
  145.             /* Make the form on top of other windows */
  146.             //this.TopMost = true;
  147.  
  148.             /* Change Form1 to your form's name if you have different.
  149.             This is actually bad thing to do, but this will allow you to skip writing much code for our threading */
  150.             overlayform.CheckForIllegalCrossThreadCalls = false;
  151.  
  152.             Thread PrePaintThread = new Thread(new ThreadStart(prepaint));
  153.             PrePaintThread.Start();
  154.             Thread followgamethread = new Thread(new ThreadStart(followgame));
  155.             followgamethread.Start();
  156.  
  157.  
  158.             if (RecordVideo == true)
  159.             {
  160.  
  161.                 Rectangle R = new Rectangle();
  162.                 R.Size = new Size(outrect.right - outrect.left, outrect.bottom - outrect.top);
  163.                 R.X = outrect.left;
  164.                 R.Y = outrect.top;
  165.  
  166.  
  167.                 stream = new ScreenCaptureStream(R);
  168.  
  169.                 stream.NewFrame += new NewFrameEventHandler(video_NewFrame);
  170.                 MessageBox.Show("Recording will start after this messagebox is closed. Click P to stop recording.");
  171.                 stream.Start();
  172.             }
  173.             else
  174.             {
  175.  
  176.             }
  177.            
  178.  
  179.  
  180.  
  181.             //Thread stickThread = new Thread(new ThreadStart(stick));
  182.             //stickThread.Start();
  183.         }
  184.  
  185.         private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
  186.         {
  187.             Bitmap bitmap = eventArgs.Frame;
  188.         }
  189.  
  190.         private void prepaint()
  191.         {
  192.             while (true)
  193.             {
  194.  
  195.                 this.Refresh();
  196.  
  197.                 /* Refresh rate is 50ms */
  198.                 System.Threading.Thread.Sleep(50);
  199.  
  200.             }
  201.         }
  202.  
  203.         private void painttext(System.Drawing.Graphics g)
  204.         {
  205.             /* Make a new font object for drawing */
  206.             Font bigFont = new Font("Arial", 12, FontStyle.Bold);
  207.             /* Make a colored brush for drawing text */
  208.             Brush mybrush = new SolidBrush(Color.White);
  209.             /* Draw 'Hello, World' at position 50, 50 of the game window */
  210.             g.DrawString("Diwif Game Recorder", bigFont, mybrush, 23, 23);
  211.  
  212.            
  213.  
  214.         }
  215.  
  216.         public void followgame()
  217.         {
  218.  
  219.             try
  220.             {
  221.  
  222.            
  223.             while (true)
  224.             {
  225.                 this.TopMost = true;
  226.                 handle = FindWindowByCaption(IntPtr.Zero, gamename);
  227.                 GetWindowRect(handle, out outrect);
  228.                 this.TopMost = true;
  229.                 this.Top = outrect.top;
  230.                 this.Left = outrect.left;
  231.                 this.TopMost = true;
  232.                 this.Size = new Size(outrect.right - outrect.left, outrect.bottom - outrect.top);
  233.                 this.TopMost = true;
  234.  
  235.  
  236.                
  237.                
  238.                   /*
  239.                 //MINIMIZED CODE
  240.                 if (p.MainWindowHandle != IntPtr.Zero)
  241.                 {
  242.                     if (p.MainWindowTitle.Contains("Subnautica"))
  243.                     {
  244.                         WINDOWPLACEMENT placement = new WINDOWPLACEMENT();
  245.                         GetWindowPlacement(p.MainWindowHandle, ref placement);
  246.                         switch (placement.showCmd)
  247.                         {
  248.                             case 1:
  249.                                 this.WindowState = FormWindowState.Normal;
  250.                                 break;
  251.                             case 2:
  252.                                 this.WindowState = FormWindowState.Minimized;
  253.                                 break;
  254.                             case 3:
  255.                                 MessageBox.Show("This overlay does not support maximized games");
  256.                                 Close();
  257.                                 break;
  258.                         }
  259.                     }
  260.                 }
  261.                 */
  262.  
  263.  
  264.  
  265.             }
  266.             }
  267.             catch (Exception)
  268.             {
  269.                 MessageBox.Show("Overlay error: Cannot locate game window!");
  270.             }
  271.  
  272.         }
  273.         public void StartGame()
  274.         {
  275.             try
  276.             {
  277.                 ProcessStartInfo i = new ProcessStartInfo();
  278.                 i.FileName = @"C:\Program Files (x86)\Steam\steamapps\common\Subnautica\Subnautica.exe";
  279.                 i.WindowStyle = ProcessWindowStyle.Normal;
  280.                 p.StartInfo = i;
  281.                 //p.Start();
  282.             }
  283.             catch (Exception ex)
  284.             {
  285.                 MessageBox.Show("Error starting Subnautica.");
  286.             }
  287.          
  288.         }
  289.  
  290.         private void overlayform_Paint(object sender, PaintEventArgs e)
  291.         {
  292.             g = e.Graphics;
  293.             painttext(g);
  294.         }
  295.  
  296.         private void CloseGamebtn_Click(object sender, EventArgs e)
  297.         {
  298.             try
  299.             {
  300.                 Process[] processes;
  301.                 processes = Process.GetProcessesByName(gamename);
  302.                 foreach (Process proc in processes)
  303.                 {
  304.                     proc.Close();
  305.                 }
  306.                 Close();
  307.             }
  308.             catch (Exception)
  309.             {
  310.                 MessageBox.Show("Failed to close Subnautica / Diwif Game Recorder.");
  311.             }
  312.            
  313.         }
  314.  
  315.         private void overlayform_KeyDown(object sender, KeyEventArgs e)
  316.         {
  317.             if (e.KeyCode == Keys.P)
  318.             {
  319.                 stream.Stop();
  320.             }
  321.         }
  322.     }
  323. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement