Advertisement
duel_05

C# - Windows Form Game Overlay 8/19/2019

Aug 19th, 2019
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.68 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Runtime.InteropServices;
  9. using System.Threading;
  10.  
  11. namespace diwifgc
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.  
  16.  
  17.  
  18.         public const string gamename = "Subnautica"; // Change this to your game's window name!!!
  19.  
  20.         //Including WINApi functions. You can find those on PInvoke.net
  21.         [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
  22.         static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);
  23.  
  24.         [DllImport("user32.dll")]
  25.         static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
  26.  
  27.         [DllImport("user32.dll", SetLastError = true)]
  28.         static extern int GetWindowLong(IntPtr window, int index);
  29.         IntPtr handle = FindWindowByCaption(IntPtr.Zero, gamename);
  30.  
  31.         [DllImport("user32.dll")]
  32.         static extern IntPtr GetForegroundWindow();
  33.  
  34.         [DllImport("user32.dll", SetLastError = true)]
  35.         static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);
  36.  
  37.         // Making basic RECT struct for GetWindowRect function.
  38.         public struct RECT
  39.         {
  40.             public int left, top, right, bottom;
  41.         }
  42.  
  43.         // Making the graphics object.
  44.         System.Drawing.Graphics g;
  45.  
  46.         RECT outrect;
  47.  
  48.  
  49.         public Form1()
  50.         {
  51.             InitializeComponent();
  52.         }
  53.  
  54.         private void Form1_Load(object sender, EventArgs e)
  55.         {
  56.  
  57.             this.SetStyle(
  58.             ControlStyles.AllPaintingInWmPaint |
  59.             ControlStyles.UserPaint |
  60.             ControlStyles.DoubleBuffer,
  61.             true);
  62.  
  63.             this.FormBorderStyle = FormBorderStyle.None;
  64.             this.BackColor = System.Drawing.Color.Black;
  65.             this.TransparencyKey = System.Drawing.Color.Black;
  66.             this.TopMost = true;
  67.  
  68.             /* Making the RECT object that will handle info from WINApi function. */
  69.            
  70.             /* Saving data to that object */
  71.             GetWindowRect(handle, out outrect);
  72.  
  73.             /* Change the size of the form to the size of the Game window. */
  74.             this.Size = new Size(outrect.right - outrect.left, outrect.bottom - outrect.top);
  75.  
  76.             this.Top = outrect.top;
  77.             this.Left = outrect.left;
  78.            
  79.             /* Change the position of the form to the position of the Game window. */
  80.  
  81.  
  82.             /* Change form's style to make it transparent and to remove the border */
  83.             this.FormBorderStyle = FormBorderStyle.None;
  84.             this.BackColor = System.Drawing.Color.Black;
  85.             this.TransparencyKey = System.Drawing.Color.Black;
  86.  
  87.             /* Using WINApi function GetWindowLong and SetWindowLong to be able to click throught the form */
  88.             int initialStyle = GetWindowLong(this.Handle, -20);
  89.             SetWindowLong(this.Handle, -20, initialStyle | 0x80000 | 0x20);
  90.  
  91.             /* Make the form on top of other windows */
  92.             this.TopMost = true;
  93.  
  94.             /* Change Form1 to your form's name if you have different.
  95.             This is actually bad thing to do, but this will allow you to skip writing much code for our threading */
  96.             Form1.CheckForIllegalCrossThreadCalls = false;
  97.  
  98.             Thread PrePaintThread = new Thread(new ThreadStart(prepaint));
  99.             PrePaintThread.Start();
  100.  
  101.  
  102.  
  103.             //Thread stickThread = new Thread(new ThreadStart(stick));
  104.             //stickThread.Start();
  105.  
  106.         }
  107.  
  108.  
  109.         private void prepaint()
  110.         {
  111.             while (true)
  112.             {
  113.                
  114.                 this.Refresh();
  115.  
  116.                 /* Refresh rate is 50ms */
  117.                 System.Threading.Thread.Sleep(50);
  118.                
  119.             }
  120.         }
  121.          /*
  122.         public void stick()
  123.         {
  124.             while (true)
  125.             {
  126.                 this.Top = outrect.top;
  127.                 this.Left = outrect.left;
  128.             }
  129.         }
  130.         */
  131.      
  132.  
  133.         private void Form1_Paint(object sender, PaintEventArgs e)
  134.         {
  135.             g = e.Graphics;
  136.             painttext(g);
  137.         }
  138.  
  139.         private void painttext(System.Drawing.Graphics g)
  140.         {
  141.             /* Make a new font object for drawing */
  142.             Font bigFont = new Font("Arial", 20);
  143.             /* Make a colored brush for drawing text */
  144.             Brush mybrush = new SolidBrush(Color.White);
  145.             /* Draw 'Hello, World' at position 50, 50 of the game window */
  146.             g.DrawString("Hello, World!", bigFont, mybrush, 50, 50);
  147.         }
  148.     }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement