Search Unity

Windows api calls

Discussion in 'Editor & General Support' started by amonseti, Mar 14, 2012.

  1. amonseti

    amonseti

    Joined:
    Mar 22, 2011
    Posts:
    2
    Simple question. Can unity Make windows api calls ..

    I need to make an application where the background is transparent and shows a users desk top. The transparent areas of the background must also be click threw.
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,603
    If you own Unity Pro then yes.

    But the background will not become transparent as it is a 3d context, not a 2D window with some nice UI gadgets or WPF UI.

    Also click through does never happen, unity will eat the click events that happen into it. You would have to react to Input events and send them manually to the winapi layer again.

    question is what you really want to achieve, if it wouldn't be simpler to screengrab whats beyond and use it as background for example
     
  3. amonseti

    amonseti

    Joined:
    Mar 22, 2011
    Posts:
    2
    Thank you very much for the quick response. Much appreciated.
     
  4. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,646
    This code works with Unity Free. ;)

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System;
    5. using System.Runtime.InteropServices;
    6.  
    7. public class FindHwnd : MonoBehaviour {
    8.  
    9.     [DllImport("user32.dll")] static extern int GetForegroundWindow();
    10.  
    11.     [DllImport("user32.dll", EntryPoint="MoveWindow")]  
    12.         static extern int  MoveWindow (int hwnd, int x, int y,int nWidth,int nHeight,int bRepaint );
    13.  
    14.     [DllImport("user32.dll", EntryPoint="SetWindowLongA")]  
    15.         static extern int  SetWindowLong (int hwnd, int nIndex,int dwNewLong);
    16.        
    17.     [DllImport("user32.dll")]
    18.         static extern bool ShowWindowAsync(int hWnd, int nCmdShow);
    19.  
    20.     void Start()
    21.     {
    22.         int handle = GetForegroundWindow();
    23.  
    24.         int fWidth  = Screen.width;
    25.         int fHeight = Screen.height;
    26.         MoveWindow(handle,2000,0,fWidth,fHeight,1); // move the Unity Projet windows >>> 2000,0 Secondary monitor ;)
    27.  
    28.         ShowWindowAsync(handle, 3); // full screen  // SW_SHOWMAXIMIZED
    29.     }
    30. }
    31.  
    32.  
    33.  
     
    Last edited: Mar 17, 2012
    krougeau likes this.
  5. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,646
    UP!
    Transparency is done ;)

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System;
    5. using System.Runtime.InteropServices; // Pro and Free!!!
    6.  
    7. public class FindHwnd : MonoBehaviour {
    8.  
    9.     [DllImport("user32.dll")] static extern int GetForegroundWindow();
    10.  
    11.     [DllImport("user32.dll", EntryPoint="MoveWindow")]  
    12.         static extern int  MoveWindow (int hwnd, int x, int y,int nWidth,int nHeight,int bRepaint );
    13.  
    14.     [DllImport("user32.dll", EntryPoint="SetWindowLongA")]  
    15.         static extern int  SetWindowLong (int hwnd, int nIndex,int dwNewLong);
    16.        
    17.     [DllImport("user32.dll")]
    18.         static extern bool ShowWindowAsync(int hWnd, int nCmdShow);
    19.        
    20.     [DllImport("user32.dll", EntryPoint="SetLayeredWindowAttributes")]  
    21.         static extern int  SetLayeredWindowAttributes (int hwnd, int crKey,byte bAlpha, int dwFlags );
    22.  
    23.     void Start()
    24.     {
    25.         int handle = GetForegroundWindow();
    26.  
    27.         int fWidth  = Screen.width;
    28.         int fHeight = Screen.height;
    29.         MoveWindow(handle,0,0,fWidth,fHeight,1); // move the Unity Projet windows >>> 0,0
    30.  
    31.         //ShowWindowAsync(handle, 3); // full screen !!!    // SW_SHOWMAXIMIZED
    32.        
    33.         // Transparency windows done !!!
    34.         SetWindowLong(handle,-20,524288); // GWL_EXSTYLE=-20 , WS_EX_LAYERED=524288=&h80000
    35.         //SetLayeredWindowAttributes(handle,0,127, 2); // Transparency=127 >> 50%  ,  LWA_ALPHA=2
    36.  
    37.         // Tranparency color key !!!
    38.         SetLayeredWindowAttributes(handle,0,0, 1); // handle,color key = 0 >> black, % of transparency, LWA_ALPHA=1
    39.     }
    40. }
    41.  
    42.  
    Works!!
     
    Last edited: Mar 18, 2012
    skullthug and krougeau like this.
  6. richyrich

    richyrich

    Joined:
    Mar 22, 2014
    Posts:
    1
    krougeau likes this.
  7. ChipMan

    ChipMan

    Joined:
    Mar 4, 2015
    Posts:
    122
    nicely done! :eek:
     
  8. weixiaowang

    weixiaowang

    Joined:
    Mar 9, 2017
    Posts:
    6
    It cant work on windows7.transparent when Starting and then become black background. How to fix it?