设置窗口透明、窗口置顶、鼠标穿透

  

方法一、

缺点:边缘不平滑,有毛边

参考博客:

1、https://alastaira.wordpress.com/2015/06/15/creating-windowless-unity-applications/

2、http://www.manew.com/thread-43230-1-1.html

3、https://blog.csdn.net/dark00800/article/details/70314432

关键代码

Shader "Custom/ChromakeyTransparent" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_TransparentColourKey ("Transparent Colour Key", Color) = (,,,)
_TransparencyTolerance ("Transparency Tolerance", Float) = 0.01
}
SubShader {
Pass {
Tags { "RenderType" = "Opaque" }
LOD CGPROGRAM #pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc" struct a2v
{
float4 pos : POSITION;
float2 uv : TEXCOORD0;
}; struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
}; v2f vert(a2v input)
{
v2f output;
output.pos = mul (UNITY_MATRIX_MVP, input.pos);
output.uv = input.uv;
return output;
} sampler2D _MainTex;
float3 _TransparentColourKey;
float _TransparencyTolerance; float4 frag(v2f input) : SV_Target
{
// What is the colour that *would* be rendered here?
float4 colour = tex2D(_MainTex, input.uv); // Calculate the different in each component from the chosen transparency colour
float deltaR = abs(colour.r - _TransparentColourKey.r);
float deltaG = abs(colour.g - _TransparentColourKey.g);
float deltaB = abs(colour.b - _TransparentColourKey.b); // If colour is within tolerance, write a transparent pixel
if (deltaR < _TransparencyTolerance && deltaG < _TransparencyTolerance && deltaB < _TransparencyTolerance)
{
return float4(0.0f, 0.0f, 0.0f, 0.0f);
} // Otherwise, return the regular colour
return colour;
}
ENDCG
}
}
}
using System;
using System.Runtime.InteropServices;
using UnityEngine; public class TransparentWindow : MonoBehaviour
{
[SerializeField]
private Material m_Material; private struct MARGINS
{
public int cxLeftWidth;
public int cxRightWidth;
public int cyTopHeight;
public int cyBottomHeight;
} // Define function signatures to import from Windows APIs [DllImport("user32.dll")]
private static extern IntPtr GetActiveWindow(); [DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong); [DllImport("Dwmapi.dll")]
private static extern uint DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS margins); // Definitions of window styles
const int GWL_STYLE = -;
const uint WS_POPUP = 0x80000000;
const uint WS_VISIBLE = 0x10000000; void Start()
{
#if !UNITY_EDITOR
var margins = new MARGINS() { cxLeftWidth = - }; // Get a handle to the window
var hwnd = GetActiveWindow(); // Set properties of the window
// See: https://msdn.microsoft.com/en-us/library/windows/desktop/ms633591%28v=vs.85%29.aspx
SetWindowLong(hwnd, GWL_STYLE, WS_POPUP | WS_VISIBLE); // Extend the window into the client area
See: https://msdn.microsoft.com/en-us/library/windows/desktop/aa969512%28v=vs.85%29.aspx
DwmExtendFrameIntoClientArea(hwnd, ref margins);
#endif
} // Pass the output of the camera to the custom material
// for chroma replacement
void OnRenderImage(RenderTexture from, RenderTexture to)
{
Graphics.Blit(from, to, m_Material);
}
}

下面是改进版,添加窗口置顶和鼠标穿透,不过依然存在毛边现象,shader还是用上面的

using System;
using System.Runtime.InteropServices;
using UnityEngine; public class TransparentWindow :MonoBehaviour
{ [SerializeField]
public Material m_Material; private struct MARGINS
{
public int cxLeftWidth;
public int cxRightWidth;
public int cyTopHeight;
public int cyBottomHeight;
} // Define function signatures to import from Windows APIs [DllImport("user32.dll")]
private static extern IntPtr GetActiveWindow(); [DllImport("user32.dll")]
private static extern uint SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong); [DllImport("Dwmapi.dll")]
private static extern uint DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS margins); [DllImport("user32.dll")]
private static extern uint GetWindowLong(IntPtr hwnd,int nIndex);
[DllImport("user32.dll")]
private static extern int SetLayeredWindowAttributes(IntPtr hwnd,int crKey,int bAlpha,int dwFlags); /// <summary>
/// 窗口置顶
/// </summary>
/// <returns></returns>
[DllImport("user32.dll")]
private static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int Width, int Height, int flags);
/// <summary>
/// 得到当前活动的窗口
/// </summary>
/// <returns></returns>
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow(); const uint LWA_COLORKEY = 0x1;
// Definitions of window styles
const int GWL_STYLE = -;
const uint WS_POPUP = 0x80000000;
const uint WS_VISIBLE = 0x10000000; private const uint WS_EX_LAYERED = 0x80000;
private const int WS_EX_TRANSPARENT = 0x20; private const int GWL_EXSTYLE = (-);
private const int LWA_ALPHA = 0x2;
IntPtr hwnd;
void Start()
{
Application.runInBackground=true;
var margins = new MARGINS() { cxLeftWidth = - }; hwnd = GetActiveWindow(); DwmExtendFrameIntoClientArea(hwnd, ref margins); WindowTop();
chuantoulong();
} /// <summary>
/// 设置窗体置顶
/// </summary>
private void WindowTop()
{
SetWindowPos(GetForegroundWindow(), -, , , , , | ); uint intExTemp = GetWindowLong(hwnd, GWL_EXSTYLE); } /// <summary>
/// 鼠标穿透
/// </summary>
public void chuantoulong()
{
SetWindowLong(hwnd, GWL_EXSTYLE, WS_EX_TRANSPARENT | WS_EX_LAYERED); }
public void chuantoulong_fan()
{
SetWindowLong(hwnd, GWL_EXSTYLE, WS_EX_TRANSPARENT); }
void OnRenderImage(RenderTexture from, RenderTexture to)
{ Graphics.Blit(from, to, m_Material);
}
const int WS_BORDER = ; }

方法二、

效果最佳,无毛边效果

参考博客:

1、https://blog.csdn.net/q493201681/article/details/65936592

注意,需要将Camera设置为Solid Color,并将颜色改为黑色

关键代码:

using UnityEngine;
using System.Collections;
using System;
using System.Runtime.InteropServices;
using System.IO; /// <summary>
/// 一共可选择三种样式
/// </summary>
public enum enumWinStyle
{
/// <summary>
/// 置顶
/// </summary>
WinTop,
/// <summary>
/// 置顶并且透明
/// </summary>
WinTopApha,
/// <summary>
/// 置顶透明并且可以穿透
/// </summary>
WinTopAphaPenetrate
}
public class WinSetting : MonoBehaviour
{ #region Win函数常量
private struct MARGINS
{
public int cxLeftWidth;
public int cxRightWidth;
public int cyTopHeight;
public int cyBottomHeight;
} [DllImport("user32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong); [DllImport("user32.dll")]
static extern int GetWindowLong(IntPtr hWnd, int nIndex); [DllImport("user32.dll")]
static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags); [DllImport("user32.dll")]
static extern int SetLayeredWindowAttributes(IntPtr hwnd, int crKey, int bAlpha, int dwFlags); [DllImport("Dwmapi.dll")]
static extern uint DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS margins);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong);
private const int WS_POPUP = 0x800000;
private const int GWL_EXSTYLE = -;
private const int GWL_STYLE = -;
private const int WS_EX_LAYERED = 0x00080000;
private const int WS_BORDER = 0x00800000;
private const int WS_CAPTION = 0x00C00000;
private const int SWP_SHOWWINDOW = 0x0040;
private const int LWA_COLORKEY = 0x00000001;
private const int LWA_ALPHA = 0x00000002;
private const int WS_EX_TRANSPARENT = 0x20;
//
private const int ULW_COLORKEY = 0x00000001;
private const int ULW_ALPHA = 0x00000002;
private const int ULW_OPAQUE = 0x00000004;
private const int ULW_EX_NORESIZE = 0x00000008;
#endregion
//
public string strProduct;//项目名称
public enumWinStyle WinStyle = enumWinStyle.WinTop;//窗体样式
//
public int ResWidth;//窗口宽度
public int ResHeight;//窗口高度
//
public int currentX;//窗口左上角坐标x
public int currentY;//窗口左上角坐标y
//
private bool isApha;//是否透明
private bool isAphaPenetrate;//是否要穿透窗体
// Use this for initialization
void Awake()
{
Screen.fullScreen = false;
#if UNITY_EDITOR
print("编辑模式不更改窗体");
#else
switch (WinStyle)
{
case enumWinStyle.WinTop:
isApha = false;
isAphaPenetrate = false;
break;
case enumWinStyle.WinTopApha:
isApha = true;
isAphaPenetrate = false;
break;
case enumWinStyle.WinTopAphaPenetrate:
isApha = true;
isAphaPenetrate = true;
break;
}
//
IntPtr hwnd = FindWindow(null, strProduct);
//
if (isApha)
{
//去边框并且透明
SetWindowLong(hwnd, GWL_EXSTYLE, WS_EX_LAYERED);
int intExTemp = GetWindowLong(hwnd, GWL_EXSTYLE);
if (isAphaPenetrate)//是否透明穿透窗体
{
SetWindowLong(hwnd, GWL_EXSTYLE, intExTemp | WS_EX_TRANSPARENT | WS_EX_LAYERED);
}
//
SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_BORDER & ~WS_CAPTION);
SetWindowPos(hwnd, -, currentX, currentY, ResWidth, ResHeight, SWP_SHOWWINDOW);
var margins = new MARGINS() { cxLeftWidth = - };
//
DwmExtendFrameIntoClientArea(hwnd, ref margins);
}
else
{
//单纯去边框
SetWindowLong(hwnd, GWL_STYLE, WS_POPUP);
SetWindowPos(hwnd, -, currentX, currentY, ResWidth, ResHeight, SWP_SHOWWINDOW);
}
#endif
}
}

下面是改良版,将功能拆分,便于扩展

using UnityEngine;
using System.Collections;
using System;
using System.Runtime.InteropServices;
using System.IO; /// <summary>
/// 一共可选择三种样式
/// </summary>
public enum enumWinStyle
{
/// <summary>
/// 置顶
/// </summary>
WinTop,
/// <summary>
/// 透明
/// </summary>
Apha,
/// <summary>
/// 置顶并且透明
/// </summary>
WinTopApha,
/// <summary>
/// 置顶透明并且可以穿透
/// </summary>
WinTopAphaPenetrate
}
public class WinSetting : MonoBehaviour
{ #region Win函数常量
private struct MARGINS
{
public int cxLeftWidth;
public int cxRightWidth;
public int cyTopHeight;
public int cyBottomHeight;
}
[DllImport("user32.dll")]
private static extern IntPtr GetActiveWindow(); [DllImport("user32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong); [DllImport("user32.dll")]
static extern int GetWindowLong(IntPtr hWnd, int nIndex); [DllImport("user32.dll")]
static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags); [DllImport("user32.dll")]
static extern int SetLayeredWindowAttributes(IntPtr hwnd, int crKey, int bAlpha, int dwFlags); [DllImport("Dwmapi.dll")]
static extern uint DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS margins);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong);
private const int WS_POPUP = 0x800000;
private const int GWL_EXSTYLE = -;
private const int GWL_STYLE = -;
private const int WS_EX_LAYERED = 0x00080000;
private const int WS_BORDER = 0x00800000;
private const int WS_CAPTION = 0x00C00000;
private const int SWP_SHOWWINDOW = 0x0040;
private const int LWA_COLORKEY = 0x00000001;
private const int LWA_ALPHA = 0x00000002;
private const int WS_EX_TRANSPARENT = 0x20;
//
private const int ULW_COLORKEY = 0x00000001;
private const int ULW_ALPHA = 0x00000002;
private const int ULW_OPAQUE = 0x00000004;
private const int ULW_EX_NORESIZE = 0x00000008;
#endregion
//
public string strProduct;//项目名称
public enumWinStyle WinStyle = enumWinStyle.WinTop;//窗体样式
//
public int ResWidth;//窗口宽度
public int ResHeight;//窗口高度
//
public int currentX;//窗口左上角坐标x
public int currentY;//窗口左上角坐标y
//
private bool isWinTop;//是否置顶
private bool isApha;//是否透明
private bool isAphaPenetrate;//是否要穿透窗体 IntPtr hwnd; // Use this for initialization
void Awake()
{
Screen.fullScreen = false;
//hwnd = FindWindow(null, strProduct);//可作用于其他进程的窗口
hwnd = GetActiveWindow(); //测试效果
RemoveRim();
}
//去掉边框且去掉任务栏图标
void RemoveRimAndIcon()
{
SetWindowLong(hwnd, GWL_STYLE, WS_POPUP);
}
//去掉边框
void RemoveRim()
{
SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_BORDER & ~WS_CAPTION);
}
//置顶
void SetTop()
{
SetWindowPos(hwnd, -, currentX, currentY, ResWidth, ResHeight, SWP_SHOWWINDOW);
}
//设置透明:注意
//1、一定要先调用去掉边框的函数
//2、将camera 设置为Solid color,并将颜色设置为黑色
void SetTransparency()
{
var margins = new MARGINS() { cxLeftWidth = - };
DwmExtendFrameIntoClientArea(hwnd, ref margins);
}
//鼠标穿透窗体
void SetMouseThrough()
{
int intExTemp = GetWindowLong(hwnd, GWL_EXSTYLE);
SetWindowLong(hwnd, GWL_EXSTYLE, intExTemp | WS_EX_TRANSPARENT | WS_EX_LAYERED);
} }

去掉加载时窗体闪一下的效果

using UnityEngine;
using System.Collections;
using System;
using System.Runtime.InteropServices;
using System.IO; /// <summary>
/// 一共可选择三种样式
/// </summary>
public enum enumWinStyle
{
/// <summary>
/// 置顶
/// </summary>
WinTop,
/// <summary>
/// 透明
/// </summary>
Apha,
/// <summary>
/// 置顶并且透明
/// </summary>
WinTopApha,
/// <summary>
/// 置顶透明并且可以穿透
/// </summary>
WinTopAphaPenetrate
}
public class WinSetting : MonoBehaviour
{ #region Win函数常量
private struct MARGINS
{
public int cxLeftWidth;
public int cxRightWidth;
public int cyTopHeight;
public int cyBottomHeight;
}
[DllImport("user32.dll")]
private static extern IntPtr GetActiveWindow(); [DllImport("user32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong); [DllImport("user32.dll")]
static extern int GetWindowLong(IntPtr hWnd, int nIndex); [DllImport("user32.dll")]
static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags); [DllImport("user32.dll")]
static extern int SetLayeredWindowAttributes(IntPtr hwnd, int crKey, int bAlpha, int dwFlags); [DllImport("Dwmapi.dll")]
static extern uint DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS margins);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong);
private const int WS_POPUP = 0x800000;
private const int GWL_EXSTYLE = -;
private const int GWL_STYLE = -;
private const int WS_EX_LAYERED = 0x00080000;
private const int WS_BORDER = 0x00800000;
private const int WS_CAPTION = 0x00C00000;
private const int SWP_SHOWWINDOW = 0x0040;
private const int LWA_COLORKEY = 0x00000001;
private const int LWA_ALPHA = 0x00000002;
private const int WS_EX_TRANSPARENT = 0x20;
//
private const int ULW_COLORKEY = 0x00000001;
private const int ULW_ALPHA = 0x00000002;
private const int ULW_OPAQUE = 0x00000004;
private const int ULW_EX_NORESIZE = 0x00000008;
#endregion
//
public string strProduct;//项目名称
public enumWinStyle WinStyle = enumWinStyle.WinTop;//窗体样式
//
public int ResWidth;//窗口宽度
public int ResHeight;//窗口高度
//
public int currentX;//窗口左上角坐标x
public int currentY;//窗口左上角坐标y
//
private bool isWinTop;//是否置顶
private bool isApha;//是否透明
private bool isAphaPenetrate;//是否要穿透窗体 IntPtr hwnd; // Use this for initialization
void Awake()
{ Screen.fullScreen = false;
//#if UNITY_EDITOR
// print("编辑模式不更改窗体");
//#else
switch (WinStyle)
{
case enumWinStyle.WinTop:
isApha = false;
isAphaPenetrate = false;
break;
case enumWinStyle.Apha:
isWinTop = false;
isApha = true;
isAphaPenetrate = false;
break;
case enumWinStyle.WinTopApha:
isApha = true;
isAphaPenetrate = false;
break;
case enumWinStyle.WinTopAphaPenetrate:
isApha = true;
isAphaPenetrate = true;
break;
} //
//IntPtr hwnd = FindWindow(null, strProduct);
hwnd = GetActiveWindow(); //
if (isApha)
{
//去边框并且透明
SetWindowLong(hwnd, GWL_EXSTYLE, WS_EX_LAYERED);
int intExTemp = GetWindowLong(hwnd, GWL_EXSTYLE);
if (isAphaPenetrate)//是否透明穿透窗体
{
SetWindowLong(hwnd, GWL_EXSTYLE, intExTemp | WS_EX_TRANSPARENT | WS_EX_LAYERED);
}
//
SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_BORDER & ~WS_CAPTION); //保持中间位置:因为是从左上角算起的,所以获得屏幕像素后要减去窗体宽高的一半
currentX = Screen.currentResolution.width / -/;
currentY = Screen.currentResolution.height / -/; SetWindowPos(hwnd, -, currentX, currentY, ResWidth, ResHeight, SWP_SHOWWINDOW);
var margins = new MARGINS() { cxLeftWidth = - };
//
DwmExtendFrameIntoClientArea(hwnd, ref margins); }
else
{
//单纯去边框
SetWindowLong(hwnd, GWL_STYLE, WS_POPUP);
SetWindowPos(hwnd, -, currentX, currentY, ResWidth, ResHeight, SWP_SHOWWINDOW);
}
Debug.Log(WinStyle);
//#endif
} void OnApplicationQuit()
{
//程序退出的时候设置窗体为0像素,从打开到走到awake也需要一定是时间
//会先有窗体边框,然后透明,这样会有闪一下的效果,
//设置窗体为0像素后,下次打开是就是0像素,走到awake再设置回来正常的窗口大小
//便能解决程序加载时会闪白色边框的现象
SetWindowPos(hwnd, -, currentX, currentY, , , SWP_SHOWWINDOW);
}
}

方法三、

参考博客:

1、http://blog.sina.com.cn/s/blog_c0e6ab9b0102wn2s.html

注意,需要将Camera设置为Solid Color,并将颜色改为黑色,有毛边

关键代码:

//using System;
//using System.Runtime.InteropServices;
//using UnityEngine;
//public class WindowMod : MonoBehaviour
//{
// public enum appStyle
// {
// FullScreen,
// WindowedFullScreen,
// Windowed,
// WindowedWithoutBorder
// }
// public enum zDepth
// {
// Normal,
// Top,
// TopMost
// }
// private const uint SWP_SHOWWINDOW = 64u;
// private const int GWL_STYLE = -16;
// private const int WS_BORDER = 1;
// private const int GWL_EXSTYLE = -20;
// private const int WS_CAPTION = 12582912;
// private const int WS_POPUP = 8388608;
// private const int SM_CXSCREEN = 0;
// private const int SM_CYSCREEN = 1;
// public WindowMod.appStyle AppWindowStyle = WindowMod.appStyle.WindowedFullScreen;
// public WindowMod.zDepth ScreenDepth;
// public int windowLeft = 10;
// public int windowTop = 10;
// public int windowWidth = 800;
// public int windowHeight = 600;
// private Rect screenPosition;
// private IntPtr HWND_TOP = new IntPtr(0);
// private IntPtr HWND_TOPMOST = new IntPtr(-1);
// private IntPtr HWND_NORMAL = new IntPtr(-2);
// private int Xscreen;
// private int Yscreen;
// private int i;
// [DllImport("user32.dll")]
// private static extern IntPtr GetForegroundWindow();
// [DllImport("user32.dll", CharSet = CharSet.Auto)]
// public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hPos, int x, int y, int cx, int cy, uint nflags);
// [DllImport("User32.dll")]
// private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
// [DllImport("User32.dll")]
// private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
// [DllImport("User32.dll")]
// private static extern int GetWindowLong(IntPtr hWnd, int dwNewLong);
// [DllImport("User32.dll")]
// private static extern bool MoveWindow(IntPtr hWnd, int x, int y, int width, int height, bool repaint);
// [DllImport("user32.dll", CharSet = CharSet.Auto)]
// public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
// [DllImport("user32.dll", CharSet = CharSet.Auto)]
// public static extern int SendMessage(IntPtr hwnd, int msg, IntPtr wP, IntPtr IP);
// [DllImport("user32.dll", CharSet = CharSet.Auto)]
// public static extern IntPtr SetParent(IntPtr hChild, IntPtr hParent);
// [DllImport("user32.dll", CharSet = CharSet.Auto)]
// public static extern IntPtr GetParent(IntPtr hChild);
// [DllImport("User32.dll")]
// public static extern IntPtr GetSystemMetrics(int nIndex);
// private void Start()
// {
// this.Xscreen = (int)WindowMod.GetSystemMetrics(0);
// this.Yscreen = (int)WindowMod.GetSystemMetrics(1);
// if (this.AppWindowStyle == WindowMod.appStyle.FullScreen)
// {
// Screen.SetResolution(this.Xscreen, this.Yscreen, true);
// }
// if (this.AppWindowStyle == WindowMod.appStyle.WindowedFullScreen)
// {
// Screen.SetResolution(this.Xscreen - 1, this.Yscreen - 1, false);
// this.screenPosition = new Rect(0f, 0f, (float)(this.Xscreen - 1), (float)(this.Yscreen - 1));
// }
// if (this.AppWindowStyle == WindowMod.appStyle.Windowed)
// {
// Screen.SetResolution(this.windowWidth, this.windowWidth, false);
// }
// if (this.AppWindowStyle == WindowMod.appStyle.WindowedWithoutBorder)
// {
// Screen.SetResolution(this.windowWidth, this.windowWidth, false);
// this.screenPosition = new Rect((float)this.windowLeft, (float)this.windowTop, (float)this.windowWidth, (float)this.windowWidth);
// }
// }
// private void Update()
// {
// if (this.i < 5)
// {
// if (this.AppWindowStyle == WindowMod.appStyle.WindowedFullScreen)
// {
// WindowMod.SetWindowLong(WindowMod.GetForegroundWindow(), -16, 369164288);
// if (this.ScreenDepth == WindowMod.zDepth.Normal)
// {
// WindowMod.SetWindowPos(WindowMod.GetForegroundWindow(), this.HWND_NORMAL, (int)this.screenPosition.x, (int)this.screenPosition.y, (int)this.screenPosition.width, (int)this.screenPosition.height, 64u);
// }
// if (this.ScreenDepth == WindowMod.zDepth.Top)
// {
// WindowMod.SetWindowPos(WindowMod.GetForegroundWindow(), this.HWND_TOP, (int)this.screenPosition.x, (int)this.screenPosition.y, (int)this.screenPosition.width, (int)this.screenPosition.height, 64u);
// }
// if (this.ScreenDepth == WindowMod.zDepth.TopMost)
// {
// WindowMod.SetWindowPos(WindowMod.GetForegroundWindow(), this.HWND_TOPMOST, (int)this.screenPosition.x, (int)this.screenPosition.y, (int)this.screenPosition.width, (int)this.screenPosition.height, 64u);
// }
// WindowMod.ShowWindow(WindowMod.GetForegroundWindow(), 3);
// }
// if (this.AppWindowStyle == WindowMod.appStyle.Windowed)
// {
// if (this.ScreenDepth == WindowMod.zDepth.Normal)
// {
// WindowMod.SetWindowPos(WindowMod.GetForegroundWindow(), this.HWND_NORMAL, 0, 0, 0, 0, 3u);
// WindowMod.SetWindowPos(WindowMod.GetForegroundWindow(), this.HWND_NORMAL, 0, 0, 0, 0, 35u);
// }
// if (this.ScreenDepth == WindowMod.zDepth.Top)
// {
// WindowMod.SetWindowPos(WindowMod.GetForegroundWindow(), this.HWND_TOP, 0, 0, 0, 0, 3u);
// WindowMod.SetWindowPos(WindowMod.GetForegroundWindow(), this.HWND_TOP, 0, 0, 0, 0, 35u);
// }
// if (this.ScreenDepth == WindowMod.zDepth.TopMost)
// {
// WindowMod.SetWindowPos(WindowMod.GetForegroundWindow(), this.HWND_TOPMOST, 0, 0, 0, 0, 3u);
// WindowMod.SetWindowPos(WindowMod.GetForegroundWindow(), this.HWND_TOPMOST, 0, 0, 0, 0, 35u);
// }
// }
// if (this.AppWindowStyle == WindowMod.appStyle.WindowedWithoutBorder)
// {
// WindowMod.SetWindowLong(WindowMod.GetForegroundWindow(), -16, 369164288);
// if (this.ScreenDepth == WindowMod.zDepth.Normal)
// {
// WindowMod.SetWindowPos(WindowMod.GetForegroundWindow(), this.HWND_NORMAL, (int)this.screenPosition.x, (int)this.screenPosition.y, (int)this.screenPosition.width, (int)this.screenPosition.height, 64u);
// }
// if (this.ScreenDepth == WindowMod.zDepth.Top)
// {
// WindowMod.SetWindowPos(WindowMod.GetForegroundWindow(), this.HWND_TOP, (int)this.screenPosition.x, (int)this.screenPosition.y, (int)this.screenPosition.width, (int)this.screenPosition.height, 64u);
// }
// if (this.ScreenDepth == WindowMod.zDepth.TopMost)
// {
// WindowMod.SetWindowPos(WindowMod.GetForegroundWindow(), this.HWND_TOPMOST, (int)this.screenPosition.x, (int)this.screenPosition.y, (int)this.screenPosition.width, (int)this.screenPosition.height, 64u);
// }
// }
// }
// this.i++;
// }
//} using UnityEngine;
using System;
using System.Collections;
using System.Runtime.InteropServices; public class WindowMod : MonoBehaviour
{ public Rect screenPosition; [DllImport("user32.dll")]
static extern int GetWindowLong(IntPtr hwnd, int _nIndex); [DllImport("user32.dll")]
static extern IntPtr SetWindowLong(IntPtr hwnd, int _nIndex, int dwNewLong); [DllImport("user32.dll")]
static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags); [DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll")]
static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, int dwFlags); const int SWP_SHOWWINDOW = 0x0040;
const int GWL_EXSTYLE = -;
const int GWL_STYLE = -;
const int WS_CAPTION = 0x00C00000;
const int WS_BORDER = 0x00800000;
const int WS_EX_LAYERED = 0x80000;
public const int LWA_ALPHA = 0x2;
public const int LWA_COLORKEY = 0x1; private IntPtr handle; void Start()
{
handle = GetForegroundWindow();
SetWindowLong(handle, GWL_EXSTYLE, WS_EX_LAYERED);
SetWindowLong(handle, GWL_STYLE, GetWindowLong(handle, GWL_STYLE) & ~WS_BORDER & ~WS_CAPTION);
SetWindowPos(handle, -, (int)screenPosition.x, (int)screenPosition.y, (int)screenPosition.width, (int)screenPosition.height, SWP_SHOWWINDOW); //把黑色透明化,不工作
// SetLayeredWindowAttributes(handle, 0, 100, LWA_COLORKEY); //把整个窗口透明化,工作
SetLayeredWindowAttributes(handle, , , LWA_ALPHA);
} void LateUpdate()
{ }
}

注:去掉边框的巧妙方法,适用于任何unity打包出来的exe,而且不用写一句代码。

步骤:

1、输入cmd,打开命令行。

2、在命令行上输入exe的路径  \***\***.exe

3、然后在后面敲一个空格,输入-popupwindow

4、回车

这样操作下来,程序运行起来是没有边框的。

同样:将-popupwindow 改为,-nolog 程序运行起来就会没有output_log.txt log文件。

妙用还有很多,请参考官方文档https://docs.unity3d.com/Manual/CommandLineArguments.html

Unity 设置窗体透明的更多相关文章

  1. 设置窗体透明C#代码

    上个示例是C#调用windows api 在原来代码上加入窗体透明,控件不透明代码: using System; using System.Runtime.InteropServices; using ...

  2. Qt 设置窗体透明

    一.前言 在音频开发中,窗体多半为半透明.圆角窗体,如下为Qt 5.5 VS2013实现半透明方法总结. 二.半透明方法设置 1.窗体及子控件都设置为半透明 1)setWindowOpacity(0. ...

  3. windows sdk 设置窗体透明

    #define WINVER 0x0501 #include <windows.h> /* Declare Windows procedure */ LRESULT CALLBACK Wi ...

  4. QMenu,contextmenuevent,窗体透明

    void MainWindow::contextMenuEvent(QContextMenuEvent *event) { QMenu *menu=newQMenu; menu->addActi ...

  5. Qt之窗体透明 (三种不同的方法和效果)

    关于窗体透明,经常遇到,网上的资料倒不少,也不知道写的时候是否验证过,很多都不正确...今天就在此一一阐述!       以下各效果是利用以前写过的一个小程序作为示例进行讲解!(代码过多,贴主要部分) ...

  6. 窗体透明,但窗体上的控件不透明(简单好用)good

    1.在Delphi中,设置窗体的AlphaBlend := true;AlphaBlendValue := 0-255; AlphaBlendValue越小窗体的透明度就越高.这种方法将会使窗体和窗体 ...

  7. 【转载】Layered Window(分层窗体,透明窗体)

    本文转载自花间醉卧<Layered Window(分层窗体,透明窗体)> //为窗体添加WS_EX_LAYERED属性,该属性使窗体支持透明 ModifyStyleEx(0, WS_EX_ ...

  8. delphi 窗体透明详解TransparentColorValue,窗体透明控件不透明

    关于窗体透明的做法 来自:http://blog.csdn.net/shuaihj/article/details/8610343 关于窗体透明的做法 1.在Delphi中,设置窗体的AlphaBle ...

  9. Qt编程—去掉标题栏和设置窗口透明用法

    学习Qt编程,有时候我们很想做出好看又比较炫的画面,这时就常用到qt上的一些技巧. 这里我以一个小例子来展示qt的这些技巧,此qt编程写的,如图:(去掉标题栏和设置窗口透明后) 代码实现部分: .h文 ...

随机推荐

  1. html5+css3+javascript 自定义提示窗口

    效果图: 源码: 1.demo.jsp <%@ page contentType="text/html;charset=UTF-8" language="java& ...

  2. inline元素和inline-block元素的4px空白间距解决方案

    实在不想写了,要吐了,看到一篇讲的比较全的文章,直接粘链接了 inline元素和inline-block元素的4px空白间距解决方案 出自脚本之家

  3. Servlet学习(二)——ServletContext对象

    1.什么是ServletContext对象 ServletContext代表是一个web应用的环境(上下文)对象,ServletContext对象内部封装是该web应用的信息,一个web应用只有一个S ...

  4. TLCL

    参考阅读:http://billie66.github.io/TLCL/book/chap04.html 绝对路径 An absolute pathname begins with the root ...

  5. HDU 1506 Largest Rectangle in a Histogram【DP】

    题意:坐标轴上有连续的n个底均为1,高为h[i]的矩形,求能够构成的最大矩形的面积. 学习的别人的代码 @_@ 看底的坐标怎么找的看了好一会儿--- 记l[i]为矩形的底的左边的坐标,就将它一直向左扩 ...

  6. Monitor (synchronization)条件变量-安全对象

    In concurrent programming, a monitor is a synchronization construct that allows threads to have both ...

  7. js+css实现全局loading加载

    js var Mask = function() { //定义一个Mask对象 this.btn = ["取消", "确定"], this.init = fun ...

  8. WordPress 不错的插件

    Akismet – 防止垃圾评论 WP-PostViews Plus - 页面访问量统计 All in One SEO Pack – 搜索引擎优化的插件,自动优化搜索引擎. WP Super Cach ...

  9. 【BZOJ1014】【JSOI2008】火星人prefix

    题意: Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 ...

  10. Oralce 视图 view

    Oracle视图 Oracle的数据库对象分为五种:表,视图,序列,索引和同义词. 视图是基于一个表或多个表或视图的逻辑表,本身不包含数据,通过它可以对表里面的数据进行查询和修改.视图基于的表称为基表 ...