This is my second post on minimising windows today, and I have decided after Unity wouldn’t let me build with a DLL to try it without, which is causing more problems than it is quite frankly worth.
I am trying to get the window handle reference using hWnd, but nothing is working.
inside my class I have this:
#if UNITY_STANDALONE_WIN && !UNITY_EDITOR
private const int SW_SHOWNORMAL = 1;
private const int SW_SHOWMINIMIZED = 2;
private const int SW_SHOWMAXIMIZED = 3;
[DllImport("user32.dll")]
private static extern IntPtr GetActiveWindow();
[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
#endif
In my awake method:
public void Awake()
{
windowName = UnityEngine.Application.productName.ToString();
#if UNITY_STANDALONE_WIN && !UNITY_EDITOR
Window window = Window.GetWindow(this);
var wih = new WindowInteropHelper(window);
IntPtr hWnd = wih.Handle;
#endif
}
And in my callable MinimiseWindow method:
public void MinimiseWindow()
{
#if UNITY_STANDALONE_WIN && !UNITY_EDITOR
if (!hWnd.Equals(IntPtr.Zero))
{
ShowWindowAsync(hWnd, SW_SHOWMINIMIZED);
}
#endif
}
Tried plugging some of this stuff in, doesn’t seem to have made any difference.
I think the main issue is that Unity simply can’t get the reference for hWnd because it is in !UNITY_EDITOR tags, but knowing this still doesn’t help me in the slightest.
All I am trying to do is minimise the windows, which is borderless, maybe this is simply too much to ask of Unity.
There are issues with your code causing the errors you pasted above like not declaring variables in the right place - I was just trying to help with the hWnd = 0 problem which we had as well.
If you want to test hWnd stuff in the editor, maybe try doing something like;
#if UNITY_EDITOR
IntPtr hWnd = FindWindow(null, "Unity"); // or whatever the Unity Editor window is called
#else
IntPtr hWnd = FindWindow(null, "Your App Name");
#endif
if(hWnd != IntPtr.Zero){
Debug.Log("hWnd = " + hWnd);
} else {
Debug.LogWarning("hWnd problem.");
}
First of all, type Window is missing. Did you forget appropriate using directive? Secondly, hWnd variable isn’t declared (most likely in MinimizeWindow). Thirdly, you’re missing a cast in #1 to System.IntPtr or an appropriate conversion routine.
Executed app can get his hwnd independently, from himself by use winapi GetActiveWindow external function, but that works only if focus setted on runned window. Otherwise function returns System.IntPtr.Zero.
If you run app usually focus automatically moved to runned app, and in this moment you can save hwnd, and use it after.
For example:
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
// links to external dll functions
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
private static extern bool SetWindowPos(System.IntPtr hwnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);
[DllImport("user32.dll")]
static extern System.IntPtr GetActiveWindow();
public static void SetPosition(System.IntPtr ptr, int x, int y, int resX = 0, int resY = 0) {
SetWindowPos( ptr , 0, x, y, resX, resY, resX * resY == 0 ? 1 : 0);
}
System.IntPtr ptr=System.IntPtr.Zero; // varibale for save hwnd
void Update() {
if(ptr==System.IntPtr.Zero){
// if ptr==zero try to catch it.
ptr=GetActiveWindow();
}else{
// if we catch it try to move and resize window
SetPosition(ptr,0,0,100,100);
}
}