Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Diagnostics;
- using System.Drawing;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading;
- using System.Windows.Forms;
- using Accord;
- using Accord.Video;
- namespace overlay
- {
- public partial class overlayform : Form
- {
- public static string gamename = "Subnautica";
- Process p = new Process();
- public struct RECT
- {
- public int left, top, right, bottom;
- }
- public struct WINDOWPLACEMENT
- {
- public int length;
- public int flags;
- public int showCmd;
- public System.Drawing.Point ptMinPosition;
- public System.Drawing.Point ptMaxPosition;
- public System.Drawing.Rectangle rcNormalPosition;
- }
- //public Point MousePosition = Cursor.Position;
- RECT outrect;
- Graphics g;
- public bool RecordVideo = true;
- public ScreenCaptureStream stream;
- #region DLL Imports
- [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
- static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);
- [DllImport("user32.dll")]
- static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
- [DllImport("user32.dll", SetLastError = true)]
- static extern int GetWindowLong(IntPtr window, int index);
- IntPtr handle = FindWindowByCaption(IntPtr.Zero, gamename);
- [DllImport("user32.dll")]
- static extern IntPtr GetForegroundWindow();
- [DllImport("user32.dll", SetLastError = true)]
- static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);
- [DllImport("user32.dll")]
- [return: MarshalAs(UnmanagedType.Bool)]
- static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
- #endregion
- public overlayform()
- {
- InitializeComponent();
- }
- public void WatchMouse()
- {
- //while (true)
- // {
- //MousePosition = Cursor.Position;
- // }
- }
- private void overlayform_Load(object sender, EventArgs e)
- {
- Thread startGameThread = new Thread(new ThreadStart(StartGame));
- startGameThread.Start();
- Thread WatchMouseThread = new Thread(new ThreadStart(WatchMouse));
- WatchMouseThread.Start();
- //Process.Start(@"C:\Program Files (x86)\Steam\steamapps\common\Subnautica\Subnautica.exe");
- //Thread.Sleep(3000);
- //Thread.Sleep(3000);
- this.SetStyle(
- ControlStyles.AllPaintingInWmPaint |
- ControlStyles.UserPaint |
- ControlStyles.DoubleBuffer,
- true);
- /* Making the RECT object that will handle info from WINApi function. */
- /* Saving data to that object */
- GetWindowRect(handle, out outrect);
- /* Change the size of the form to the size of the Game window. */
- this.Size = new Size(outrect.right - outrect.left, outrect.bottom - outrect.top);
- /* Change the position of the form to the position of the Game window. */
- this.Top = outrect.top;
- this.Left = outrect.left;
- /* Change form's style to make it transparent and to remove the border */
- //this.FormBorderStyle = FormBorderStyle.None;
- this.BackColor = System.Drawing.Color.Black;
- this.TransparencyKey = System.Drawing.Color.Black;
- this.TopMost = true;
- /* Using WINApi function GetWindowLong and SetWindowLong to be able to click throught the form */
- int initialStyle = GetWindowLong(this.Handle, -20);
- SetWindowLong(this.Handle, -20, initialStyle | 0x80000 | 0x20);
- /* Make the form on top of other windows */
- //this.TopMost = true;
- /* Change Form1 to your form's name if you have different.
- This is actually bad thing to do, but this will allow you to skip writing much code for our threading */
- overlayform.CheckForIllegalCrossThreadCalls = false;
- Thread PrePaintThread = new Thread(new ThreadStart(prepaint));
- PrePaintThread.Start();
- Thread followgamethread = new Thread(new ThreadStart(followgame));
- followgamethread.Start();
- if (RecordVideo == true)
- {
- Rectangle R = new Rectangle();
- R.Size = new Size(outrect.right - outrect.left, outrect.bottom - outrect.top);
- R.X = outrect.left;
- R.Y = outrect.top;
- stream = new ScreenCaptureStream(R);
- stream.NewFrame += new NewFrameEventHandler(video_NewFrame);
- MessageBox.Show("Recording will start after this messagebox is closed. Click P to stop recording.");
- stream.Start();
- }
- else
- {
- }
- //Thread stickThread = new Thread(new ThreadStart(stick));
- //stickThread.Start();
- }
- private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
- {
- Bitmap bitmap = eventArgs.Frame;
- }
- private void prepaint()
- {
- while (true)
- {
- this.Refresh();
- /* Refresh rate is 50ms */
- System.Threading.Thread.Sleep(50);
- }
- }
- private void painttext(System.Drawing.Graphics g)
- {
- /* Make a new font object for drawing */
- Font bigFont = new Font("Arial", 12, FontStyle.Bold);
- /* Make a colored brush for drawing text */
- Brush mybrush = new SolidBrush(Color.White);
- /* Draw 'Hello, World' at position 50, 50 of the game window */
- g.DrawString("Diwif Game Recorder", bigFont, mybrush, 23, 23);
- }
- public void followgame()
- {
- try
- {
- while (true)
- {
- this.TopMost = true;
- handle = FindWindowByCaption(IntPtr.Zero, gamename);
- GetWindowRect(handle, out outrect);
- this.TopMost = true;
- this.Top = outrect.top;
- this.Left = outrect.left;
- this.TopMost = true;
- this.Size = new Size(outrect.right - outrect.left, outrect.bottom - outrect.top);
- this.TopMost = true;
- /*
- //MINIMIZED CODE
- if (p.MainWindowHandle != IntPtr.Zero)
- {
- if (p.MainWindowTitle.Contains("Subnautica"))
- {
- WINDOWPLACEMENT placement = new WINDOWPLACEMENT();
- GetWindowPlacement(p.MainWindowHandle, ref placement);
- switch (placement.showCmd)
- {
- case 1:
- this.WindowState = FormWindowState.Normal;
- break;
- case 2:
- this.WindowState = FormWindowState.Minimized;
- break;
- case 3:
- MessageBox.Show("This overlay does not support maximized games");
- Close();
- break;
- }
- }
- }
- */
- }
- }
- catch (Exception)
- {
- MessageBox.Show("Overlay error: Cannot locate game window!");
- }
- }
- public void StartGame()
- {
- try
- {
- ProcessStartInfo i = new ProcessStartInfo();
- i.FileName = @"C:\Program Files (x86)\Steam\steamapps\common\Subnautica\Subnautica.exe";
- i.WindowStyle = ProcessWindowStyle.Normal;
- p.StartInfo = i;
- //p.Start();
- }
- catch (Exception ex)
- {
- MessageBox.Show("Error starting Subnautica.");
- }
- }
- private void overlayform_Paint(object sender, PaintEventArgs e)
- {
- g = e.Graphics;
- painttext(g);
- }
- private void CloseGamebtn_Click(object sender, EventArgs e)
- {
- try
- {
- Process[] processes;
- processes = Process.GetProcessesByName(gamename);
- foreach (Process proc in processes)
- {
- proc.Close();
- }
- Close();
- }
- catch (Exception)
- {
- MessageBox.Show("Failed to close Subnautica / Diwif Game Recorder.");
- }
- }
- private void overlayform_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.KeyCode == Keys.P)
- {
- stream.Stop();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement