I am trying to make a windowed transparent game in unity. I figured out how to make an transparent game but I want to make game window sit the task bar.
I tried out change y value in SetWindowPos function but it didn’t worked. It would be great if someone show me how to do it.
Here is my code:
using UnityEngine;
using System.Collections;
using System;
using System.Runtime.InteropServices; // Pro and Free!!!
public class TransparentWindow : MonoBehaviour
{
[DllImport("user32.dll", EntryPoint = "SetWindowLongA")]
static extern int SetWindowLong(int hwnd, int nIndex, long dwNewLong);
[DllImport("user32.dll")]
static extern bool ShowWindowAsync(int hWnd, int nCmdShow);
[DllImport("user32.dll", EntryPoint = "SetLayeredWindowAttributes")]
static extern int SetLayeredWindowAttributes(int hwnd, int crKey, byte bAlpha, uint dwFlags);
[DllImport("user32.dll", EntryPoint = "GetActiveWindow")]
private static extern int GetActiveWindow();
[DllImport("user32.dll", EntryPoint = "GetWindowLong")]
private static extern long GetWindowLong(int hwnd, int nIndex);
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
private static extern int SetWindowPos(int hwnd, int hwndInsertAfter, int x, int y, int cx, int cy, int uFlags);
const uint LWA_COLORKEY = 0x00000001;
const int HWND_TOPMOST = -1;
void Start()
{
int handle = GetActiveWindow();
int fWidth = Screen.width;
int fHeight = Screen.height / 10;
//Remove title bar
long lCurStyle = GetWindowLong(handle, -16); // GWL_STYLE=-16
int a = 12582912; //WS_CAPTION = 0x00C00000L
int b = 1048576; //WS_HSCROLL = 0x00100000L
int c = 2097152; //WS_VSCROLL = 0x00200000L
int d = 524288; //WS_SYSMENU = 0x00080000L
int e = 16777216; //WS_MAXIMIZE = 0x01000000L
lCurStyle &= ~(a | b | c | d);
lCurStyle &= e;
SetWindowLong(handle, -16, lCurStyle);// GWL_STYLE=-16
// Transparent windows with click through
SetWindowLong(handle, -20, 524288);//GWL_EXSTYLE=-20; WS_EX_LAYERED=524288=&h80000, WS_EX_TRANSPARENT=32=0x00000020L
SetLayeredWindowAttributes(handle, 0, 0, LWA_COLORKEY);// Transparency=51=20%, LWA_ALPHA=2
SetWindowPos(handle, HWND_TOPMOST, 0,0, fWidth, fHeight, 32 | 64); //SWP_FRAMECHANGED = 0x0020 (32); //SWP_SHOWWINDOW = 0x0040 (64)
Screen.SetResolution(fWidth, fHeight, true);
ShowWindowAsync(handle, 3); //Forces window to show in case of unresponsive app // SW_SHOWMAXIMIZED(3)
}
}
New contributor
alperrakn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.