Search Unity

Create movable, transparent window.

Discussion in '2D' started by TheWebExpert, Jan 21, 2019.

  1. TheWebExpert

    TheWebExpert

    Joined:
    Apr 14, 2015
    Posts:
    198
    I have my main window, which has a transparent background. What I want to do is to be able to drag it anywhere on the desktop - and be able to click on things that are outside of the window. If I make it full screen, it drags everywhere - but I can't click on anything underneath it. If I make it windowed, it only drags around inside the area of the window, and I still can't click on anything "underneath" it.

    Here's my transparent window code:
    Code (CSharp):
    1. using System;
    2. using System.Runtime.InteropServices;
    3. using UnityEngine;
    4.  
    5. public class TransparentWindow : MonoBehaviour
    6.   {
    7.     [SerializeField]
    8.     private Material m_Material;
    9.  
    10.     private struct MARGINS
    11.       {
    12.         public int cxLeftWidth;
    13.         public int cxRightWidth;
    14.         public int cyTopHeight;
    15.         public int cyBottomHeight;
    16.       }
    17.  
    18.     // Define function signatures to import from Windows APIs
    19.  
    20.     [DllImport("user32.dll")]
    21.     private static extern IntPtr GetActiveWindow();
    22.  
    23.     [DllImport("user32.dll")]
    24.     private static extern int SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong);
    25.  
    26.     [DllImport("Dwmapi.dll")]
    27.     private static extern uint DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS margins);
    28.  
    29.     // Definitions of window styles
    30.     const int GWL_STYLE = -16;
    31.     const uint WS_POPUP = 0x80000000;
    32.     const uint WS_VISIBLE = 0x10000000;
    33.  
    34.     void Start()
    35.       {
    36.         #if !UNITY_EDITOR
    37.         var margins = new MARGINS() { cxLeftWidth = -1 };
    38.  
    39.         // Get a handle to the window
    40.         var hwnd = GetActiveWindow();
    41.  
    42.         // Set properties of the window
    43.         // See: https://msdn.microsoft.com/en-us/library/windows/desktop/ms633591%28v=vs.85%29.aspx
    44.         SetWindowLong(hwnd, GWL_STYLE, WS_POPUP | WS_VISIBLE);
    45.  
    46.         // Extend the window into the client area
    47.         See: https://msdn.microsoft.com/en-us/library/windows/desktop/aa969512%28v=vs.85%29.aspx
    48.         DwmExtendFrameIntoClientArea(hwnd, ref margins);
    49.         #endif
    50.       }
    51.  
    52.     // Pass the output of the camera to the custom material
    53.     // for chroma replacement
    54.     void OnRenderImage(RenderTexture from, RenderTexture to)
    55.       {
    56.         Graphics.Blit(from, to, m_Material);
    57.       }
    58.   }
    Can anyone help with this?? Thanks!