using System;
using System.Runtime.InteropServices;
using UnityEngine;
public class OverlayWindow : MonoBehaviour
{
[DllImport("user32.dll")]
private static extern bool EnumWindows(EnumWindowsProc enumProc, IntPtr lParam);
[DllImport("user32.dll")]
private static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern int GetWindowText(IntPtr hWnd, System.Text.StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll")]
private static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong);
[DllImport("user32.dll")]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
[DllImport("user32.dll")]
static extern int SetLayeredWindowAttributes(IntPtr hWnd, uint crKey, byte bAlpha, uint dwFlags);
private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
private const int GWL_EXSTYLE = -20;
private const int WS_EX_LAYERED = 0x80000;
private const int LWA_COLORKEY = 0x00000001;
private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
private static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
private static readonly IntPtr HWND_TOP = new IntPtr(0);
const UInt32 SWP_NOSIZE = 0x0001;
const UInt32 SWP_NOMOVE = 0x0002;
const UInt32 SWP_SHOWWINDOW = 0x0040;
private void Start()
{
EnumWindows(EnumWindowsCallback, IntPtr.Zero);
Application.runInBackground = true; //Makes sure it's always running
}
private bool EnumWindowsCallback(IntPtr hWnd, IntPtr lParam)
{
//if (!IsWindowVisible(hWnd)) return true; // Skip invisible windows
// Get the process ID associated with the window
GetWindowThreadProcessId(hWnd, out int processId);
// Filter windows based on process ID if needed
if (processId == GetCurrentProcessId())
{
// Apply settings to the window
SetWindowStyle(hWnd);
}
return true; // Continue enumeration
}
private void SetWindowStyle(IntPtr hWnd)
{
// Make the window layered and transparent
SetWindowLong(hWnd, GWL_EXSTYLE, WS_EX_LAYERED);
SetLayeredWindowAttributes(hWnd, 0, 0, LWA_COLORKEY);
// Make the window topmost
SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
}
private int GetCurrentProcessId()
{
return System.Diagnostics.Process.GetCurrentProcess().Id;
}
}