【问题标题】:What makes this click through?是什么让这个点击通过?
【发布时间】:2017-08-05 15:34:04
【问题描述】:

我试图弄清楚这段代码是如何工作的,但我就是不知道是什么让它点击了。

是的,这段代码不是我的,因为我正在努力学习/理解它。

假设我想要透明而不是点击需要更改的内容以及为什么?

我一遍又一遍地浏览了Windows styles 页面,但仍然无法理解点击部分。

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;
    }

    [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;

    void Start()
    {
        #if !UNITY_EDITOR // You really don't want to enable this in the editor..

        int fWidth = Screen.width;
        int fHeight = Screen.height;
        var margins = new MARGINS() { cxLeftWidth = -1 };
        var hwnd = GetActiveWindow();

        SetWindowLong(hwnd, GWL_STYLE, WS_POPUP | WS_VISIBLE);

        // Transparent windows with click through
        SetWindowLong(hwnd, -20, 524288 | 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)
        DwmExtendFrameIntoClientArea(hwnd, ref margins);

        #endif
    }

    void OnRenderImage(RenderTexture from, RenderTexture to)
    {
        Graphics.Blit(from, to, m_Material);
    }
}

【问题讨论】:

    标签: c# window click-through


    【解决方案1】:

    这个函数:

    SetWindowLong(hwnd, -20, 524288 | 32);
    

    成功了。 Windows 实现了 Mircosoft 制定的规则,即对用户透明的窗口必须对鼠标透明。 设置透明度位WS_EX_TRANSPARENT 后,窗口对鼠标也变得透明,点击传递到透明窗口后面的绘制层。

    您不需要了解,但可以使用这个“操作系统功能”,它可能是为了覆盖其他东西而实现的。

    阅读此 article 关于主题和此 answer 解释参数

    【讨论】:

    • 谢谢,这提供了非常丰富的信息。遗憾的是,我还不能投票。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-14
    • 2022-01-17
    • 1970-01-01
    相关资源
    最近更新 更多