Windows api calls

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.

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

Thank you very much for the quick response. Much appreciated.

This code works with Unity Free. :wink:

using UnityEngine;
using System.Collections;
using System;
using System.Runtime.InteropServices;

public class FindHwnd : MonoBehaviour {

	[DllImport("user32.dll")] static extern int GetForegroundWindow();

	[DllImport("user32.dll", EntryPoint="MoveWindow")]   
		static extern int  MoveWindow (int hwnd, int x, int y,int nWidth,int nHeight,int bRepaint );

	[DllImport("user32.dll", EntryPoint="SetWindowLongA")]  
		static extern int  SetWindowLong (int hwnd, int nIndex,int dwNewLong);
		
	[DllImport("user32.dll")]
		static extern bool ShowWindowAsync(int hWnd, int nCmdShow);

	void Start()
	{
		int handle = GetForegroundWindow();

		int fWidth  = Screen.width;
		int fHeight = Screen.height;
		MoveWindow(handle,2000,0,fWidth,fHeight,1); // move the Unity Projet windows >>> 2000,0 Secondary monitor ;)

		ShowWindowAsync(handle, 3); // full screen	// SW_SHOWMAXIMIZED
	}
}
3 Likes

UP!
Transparency is done :wink:

using UnityEngine;
using System.Collections;
using System;
using System.Runtime.InteropServices; // Pro and Free!!!

public class FindHwnd : MonoBehaviour {

	[DllImport("user32.dll")] static extern int GetForegroundWindow();

	[DllImport("user32.dll", EntryPoint="MoveWindow")]   
		static extern int  MoveWindow (int hwnd, int x, int y,int nWidth,int nHeight,int bRepaint );

	[DllImport("user32.dll", EntryPoint="SetWindowLongA")]  
		static extern int  SetWindowLong (int hwnd, int nIndex,int dwNewLong);
		
	[DllImport("user32.dll")]
		static extern bool ShowWindowAsync(int hWnd, int nCmdShow);
		
	[DllImport("user32.dll", EntryPoint="SetLayeredWindowAttributes")]  
		static extern int  SetLayeredWindowAttributes (int hwnd, int crKey,byte bAlpha, int dwFlags );

	void Start()
	{
		int handle = GetForegroundWindow();

		int fWidth  = Screen.width;
		int fHeight = Screen.height;
		MoveWindow(handle,0,0,fWidth,fHeight,1); // move the Unity Projet windows >>> 0,0 

		//ShowWindowAsync(handle, 3); // full screen !!!	// SW_SHOWMAXIMIZED
		
		// Transparency windows done !!!
		SetWindowLong(handle,-20,524288); // GWL_EXSTYLE=-20 , WS_EX_LAYERED=524288=&h80000
		//SetLayeredWindowAttributes(handle,0,127, 2); // Transparency=127 >> 50%  ,  LWA_ALPHA=2 

		// Tranparency color key !!!
		SetLayeredWindowAttributes(handle,0,0, 1); // handle,color key = 0 >> black, % of transparency, LWA_ALPHA=1
 	}
}

Works!!

4 Likes

Useful thanks! Used and abused the code in an answer here: Viewing desktop in-scene - Questions & Answers - Unity Discussions

1 Like

nicely done! :hushed:

It cant work on windows7.transparent when Starting and then become black background. How to fix it?

what about the stay on top part?