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

  

方法一、

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

参考博客:

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. 虚拟机CentOS6.8下安装mycat

    安装mycat前,首先安装jdk1.7及以上版本 安装可参照 https://www.cnblogs.com/llhhll/p/9260913.html 下载mycat 1.6版本 wget   ht ...

  2. 使用Java开发高性能网站需要关注的那些事儿2

      近期各家IT媒体举办的业内技术大会让很多网站都在披露自己的技术内幕与同行们分享,大到facebook,百度,小到刚起步的网站.facebook,百度之类的大型网站采用的技术和超凡的处理能力的确给人 ...

  3. js产生随机数的几个方法

    1.Math.random(); 结果为0-1间的一个随机数(包括0,不包括1) 2.Math.floor(num); 参数num为一个数值,函数结果为num的整数部分. 3.Math.round(n ...

  4. 在vue组件中style scoped中遇到的坑

    在uve组件中我们我们经常需要给style添加scoped来使得当前样式只作用于当前组件的节点.添加scoped之后,实际上vue在背后做的工作是将当前组件的节点添加一个像data-v-1233这样唯 ...

  5. Converting Legacy Chrome IPC To Mojo

    Converting Legacy Chrome IPC To Mojo Looking for Mojo Documentation? Contents Overview Deciding What ...

  6. Spring MVC 搭建过程中web.xml配置引入文件的路径问题

    为啥要说一下这么low的问题,因为我是一个比较low的人,哈哈.本来我技术有限,没事干自己撘个环境找找乐趣,结果被各种基础问题,弄的一脸蒙蔽.算了不多说,直接说问题. 1.首先说一下java编译后的文 ...

  7. HDU-1083 Courses 二分图 最大匹配

    题目链接:https://cn.vjudge.net/problem/HDU-1083 题意 有一些学生,有一些课程 给出哪些学生可以学哪些课程,每个学生可以选多课,但只能做一个课程的代表 问所有课能 ...

  8. layui Layui-Select多选的使用和注意事项

    1.最近买了layadmin的后台框架,使用Layui-Select总结如下 A.配置:我采用的全局引入配置的方式 赋值(选中状态)

  9. MySQL 高可用:mysql+mycat实现数据库分片(分库分表)

    本文引用于http://blog.csdn.net/kk185800961/article/details/51147029 MySQL 高可用:mysql+mycat实现数据库分片(分库分表) 什么 ...

  10. 动态Axios配置

    推荐使用Vue-cli工具来创建和管理项目,就算刚开始不熟悉,用着用着便可知晓其中的奥妙.前一段时间官方所推荐的数据请求插件还是Vue-resource,但现在已经变了,变成了Axios,不用知道为什 ...