2017-08-05 55 views
0

我想弄清楚這個代碼是如何工作的,但我無法弄清楚是什麼讓它點擊。是什麼讓這個點擊槽?

是的,這段代碼不是我的,因爲我試圖學習/理解它。

假設我想要透明,但不是點擊通過需要改變什麼,爲什麼?

我一遍又一遍地翻過了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); 
    } 
} 

回答

0

此功能:

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

的伎倆。 Windows實現了Mircosoft所做的規則,即對用戶透明的窗口必須對鼠標透明。 將透明度位設置爲WS_EX_TRANSPARENT時,該窗口對於鼠標也變得透明,並單擊傳遞到透明窗口後面的已繪製圖層。

你不需要理解,但可以利用這個'OS功能',它可能被用來掩蓋別的東西。

閱讀本article這個話題,這answer解釋參數

+0

謝謝你,這是非常豐富的。我可悲的還不能upvote。 –