C# 统一、透明的背景和多个监视器
我正在努力学习一些东西,但我有点卡住了,我不知道从这里走到哪里。我不是瞎了就是糊涂了 我一直在这里看一些脚本: 现在,我的主屏幕上有了背景透明度,但我想知道我应该怎么做才能在我的第二个显示器上实现这一点。或者第三,如果我有一个。从现在起,其他屏幕变黑。代码如下:C# 统一、透明的背景和多个监视器,c#,windows,visual-studio,unity3d,transparency,C#,Windows,Visual Studio,Unity3d,Transparency,我正在努力学习一些东西,但我有点卡住了,我不知道从这里走到哪里。我不是瞎了就是糊涂了 我一直在这里看一些脚本: 现在,我的主屏幕上有了背景透明度,但我想知道我应该怎么做才能在我的第二个显示器上实现这一点。或者第三,如果我有一个。从现在起,其他屏幕变黑。代码如下: [SerializeField] private Material m_Material; [SerializeField] private Camera mainCamera; private bool clickThrough
[SerializeField]
private Material m_Material;
[SerializeField]
private Camera mainCamera;
private bool clickThrough = true;
private bool prevClickThrough = true;
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")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong);
[DllImport("user32.dll")]
static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll", EntryPoint = "SetLayeredWindowAttributes")]
static extern int SetLayeredWindowAttributes(IntPtr hwnd, int crKey, byte bAlpha, int dwFlags);
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
private static extern int SetWindowPos(IntPtr hwnd, int hwndInsertAfter, int x, int y, int cx, int cy, int uFlags);
[DllImport("Dwmapi.dll")]
private static extern uint DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS margins);
const int GWL_STYLE = -16;
const uint WS_POPUP = 0x80000000;
const uint WS_VISIBLE = 0x10000000;
const int HWND_TOPMOST = -1;
int fWidth;
int fHeight;
IntPtr hwnd;
MARGINS margins;
public bool OverUI()
{ //Use sparingly
//Set up the new Pointer Event
PointerEventData m_PointerEventData = new PointerEventData(EventSystem.current);
m_PointerEventData.position = Input.mousePosition;
List<RaycastResult> results = new List<RaycastResult>();
EventSystem.current.RaycastAll(m_PointerEventData, results);
if (results.Count > 0) return true;
return false;
}
void Start()
{
fWidth = Screen.width;
fHeight = Screen.height;
margins = new MARGINS() { cxLeftWidth = -1 };
hwnd = GetActiveWindow();
SetWindowLong(hwnd, GWL_STYLE, WS_POPUP | WS_VISIBLE);
SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, fWidth, fHeight, 32 | 64); //SWP_FRAMECHANGED = 0x0020 (32); //SWP_SHOWWINDOW = 0x0040 (64)
DwmExtendFrameIntoClientArea(hwnd, ref margins);
Application.runInBackground = true;
}
void Update()
{
// If our mouse is overlapping an object
RaycastHit hit = new RaycastHit();
clickThrough = !Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition).origin,
mainCamera.ScreenPointToRay(Input.mousePosition).direction, out hit, 100,
Physics.DefaultRaycastLayers) && !OverUI();
if (clickThrough != prevClickThrough)
{
if (clickThrough)
{
SetWindowLong(hwnd, GWL_STYLE, WS_POPUP | WS_VISIBLE);
SetWindowLong (hwnd, -20, (uint)524288 | (uint)32);//GWL_EXSTYLE=-20; WS_EX_LAYERED=524288=&h80000, WS_EX_TRANSPARENT=32=0x00000020L
SetLayeredWindowAttributes (hwnd, 0, 255, 2);// Transparency=51=20%, LWA_ALPHA=2
SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, fWidth, fHeight, 32 | 64); //SWP_FRAMECHANGED = 0x0020 (32); //SWP_SHOWWINDOW = 0x0040 (64)
}
else
{
SetWindowLong (hwnd, -20, ~(((uint)524288) | ((uint)32)));//GWL_EXSTYLE=-20; WS_EX_LAYERED=524288=&h80000, WS_EX_TRANSPARENT=32=0x00000020L
SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, fWidth, fHeight, 32 | 64); //SWP_FRAMECHANGED = 0x0020 (32); //SWP_SHOWWINDOW = 0x0040 (64)
}
prevClickThrough = clickThrough;
}
}
void OnRenderImage(RenderTexture from, RenderTexture to)
{
Graphics.Blit(from, to, m_Material);
}
[序列化字段]
私人材料m_材料;
[序列化字段]
私人摄像机;
private bool clickThrough=真;
private bool prevClickThrough=真;
私有结构页边距
{
公共宽度;
公共宽度;
公众高度;
公共室内高度;
}
[DllImport(“user32.dll”)]
私有静态外部IntPtr GetActiveWindow();
[DllImport(“user32.dll”)]
私有静态外部int SetWindowLong(IntPtr hWnd、int nIndex、uint dwNewLong);
[DllImport(“user32.dll”)]
静态外部bool ShowWindowAsync(IntPtr hWnd、intncmdshow);
[DllImport(“user32.dll”,EntryPoint=“SetLayeredWindowAttributes”)]
静态外部int SetLayeredWindowAttributes(IntPtr hwnd、int crKey、字节bAlpha、int dwFlags);
[DllImport(“user32.dll”,EntryPoint=“SetWindowPos”)]
私有静态外部设置窗口位置(IntPtr hwnd、inthwninsertafter、intx、inty、intcx、intcy、intuflags);
[DllImport(“Dwmapi.dll”)]
专用静态外部单元DwmExtendFrameIntoClientArea(IntPtr hWnd,参考页边距);
const int GWL_STYLE=-16;
常数WS_POPUP=0x8000000;
常量WS_VISIBLE=0x10000000;
const int HWND_TOPMOST=-1;
int-fWidth;
整数倍;
IntPtr hwnd;
利润率;
公共图书馆
{//节约使用
//设置新指针事件
PointerEventData m_PointerEventData=新的PointerEventData(EventSystem.current);
m_PointerEventData.position=Input.mousePosition;
列表结果=新列表();
EventSystem.current.RaycastAll(m_PointerEventData,results);
如果(results.Count>0)返回true;
返回false;
}
void Start()
{
fWidth=屏幕宽度;
fHeight=屏幕高度;
边距=新边距(){cxLeftWidth=-1};
hwnd=GetActiveWindow();
SetWindowLong(hwnd,GWL_样式,WS_弹出窗口| WS_可见);
SetWindowPos(hwnd,hwnd_最上面的,0,0,fWidth,fHeight,32 | 64);//SWP_FRAMECHANGED=0x0020(32);//SWP_SHOWWINDOW=0x0040(64)
DwmExtendFrameIntoClientArea(hwnd,参考页边距);
Application.runInBackground=true;
}
无效更新()
{
//如果我们的鼠标与一个对象重叠
RaycastHit=新的RaycastHit();
clickThrough=!Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition).origin,
主摄像头。屏幕指针方向(输入。鼠标位置)。方向,外击,100,
Physics.DefaultRaycastLayers)和&!OverUI();
如果(点击!=上一次点击)
{
如果(点击)
{
SetWindowLong(hwnd,GWL_样式,WS_弹出窗口| WS_可见);
SetWindowLong(hwnd,-20,(uint)524288 |(uint)32);//GWL_EXSTYLE=-20;WS_EX_LAYERED=524288=&h80000,WS_EX_TRANSPARENT=32=0x00000020L
SetLayeredWindowAttributes(hwnd,0,255,2);//透明度=51=20%,LWA_ALPHA=2
SetWindowPos(hwnd,hwnd_最上面的,0,0,fWidth,fHeight,32 | 64);//SWP_FRAMECHANGED=0x0020(32);//SWP_SHOWWINDOW=0x0040(64)
}
其他的
{
SetWindowLong(hwnd,-20,~((uint)524288)|((uint)32));//GWL_EXSTYLE=-20;WS_EX_LAYERED=524288=&h80000,WS_EX_TRANSPARENT=32=0x00000020L
SetWindowPos(hwnd,hwnd_最上面的,0,0,fWidth,fHeight,32 | 64);//SWP_FRAMECHANGED=0x0020(32);//SWP_SHOWWINDOW=0x0040(64)
}
prevClickThrough=点击;
}
}
void OnRenderImage(RenderTexture from,RenderTexture to)
{
图形。Blit(从、到、m_材料);
}
谁能给我一点指点,让我明白这一点吗
编辑:我想了一点。。我想。
当Unity打开时(具有多个显示),它会在任务栏中创建两个窗口。我猜我需要找到另一个窗口,将其设置为活动,然后再次运行类似于此代码的内容。但我猜在这样做之前,我需要修正pos和保证金。
这超出了我现在对WinApi的理解,但我希望有人比我更了解这一点