Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
5.2 KiB

  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. #if UNITY_EDITOR_WIN
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Runtime.InteropServices;
  7. using System.Text;
  8. public class WindowsUtil
  9. {
  10. public const int GWL_STYLE = -16; //hex constant for style changing
  11. public const int WS_BORDER = 0x00800000; //window with border
  12. public const int WS_CAPTION = 0x00C00000; //window with a title bar with border
  13. public const int WS_SYSMENU = 0x00080000; //window with no borders etc.
  14. public const int WS_MAXIMIZE = 0x01000000;
  15. public const int WS_MAXIMIZEBOX = 0x00010000;
  16. public const int WS_MINIMIZE = 0x20000000;
  17. public const int WS_MINIMIZEBOX = 0x00020000;
  18. public const int WS_SIZEBOX = 0x00040000;
  19. public const int WS_VISIBLE = 0x10000000;
  20. public const int WS_TABSTOP = 0x00010000;
  21. public const int WS_CLIPCHILDREN = 0x02000000;
  22. public const int WS_CLIPSIBLINGS = 0x04000000;
  23. [DllImport( "user32.dll", EntryPoint = "SetWindowPos" )]
  24. public static extern bool SetWindowPos( System.IntPtr hwnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags );
  25. public delegate bool EnumWindowsProc( System.IntPtr hWnd, System.IntPtr lParam );
  26. [DllImport( "user32.dll", CharSet = CharSet.Auto, ExactSpelling = true )]
  27. public static extern IntPtr GetDesktopWindow();
  28. [DllImport( "user32.dll" )]
  29. public static extern int SetWindowLong( IntPtr hWnd, int nIndex, int dwNewLong );
  30. [DllImport( "user32.dll" )]
  31. public static extern int GetWindowLong( IntPtr hWnd, int nIndex );
  32. [DllImport( "user32.dll", ExactSpelling = true, SetLastError = true )]
  33. internal static extern int MapWindowPoints( IntPtr hWndFrom, IntPtr hWndTo, [In, Out] ref Rect rect, [MarshalAs( UnmanagedType.U4 )] int cPoints );
  34. [DllImport( "user32.dll" )]
  35. public static extern bool EnumWindows( EnumWindowsProc enumProc, System.IntPtr lParam );
  36. [DllImport( "user32" )]
  37. [return: MarshalAs( UnmanagedType.Bool )]
  38. public static extern bool EnumChildWindows( IntPtr window, EnumWindowProc callback, IntPtr lParam );
  39. public delegate bool EnumWindowProc( IntPtr hwnd, IntPtr lParam );
  40. [DllImport( "user32.dll", SetLastError = true )]
  41. public static extern bool MoveWindow( IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint );
  42. [DllImport( "user32.dll", CharSet = CharSet.Auto, SetLastError = true )]
  43. public static extern int GetWindowThreadProcessId( System.IntPtr handle, out int processId );
  44. [DllImport( "user32.dll", SetLastError = true )]
  45. public static extern IntPtr FindWindowEx( string lpClassName, string lpWindowName );
  46. // Find window by Caption only. Note you must pass IntPtr.Zero as the first parameter.
  47. [DllImport( "user32.dll", EntryPoint = "FindWindow", SetLastError = true )]
  48. public static extern IntPtr FindWindowByCaptionEx( IntPtr ZeroOnly, string lpWindowName );
  49. [DllImport( "user32.dll", SetLastError = true, CharSet = CharSet.Auto )]
  50. public static extern int GetClassName( IntPtr hWnd, StringBuilder lpClassName, int nMaxCount );
  51. [DllImport( "user32.dll" )]
  52. public static extern int GetWindowText( System.IntPtr hWnd, StringBuilder text, int nMaxCount );
  53. [DllImport( "user32.dll" )]
  54. public static extern int GetWindowTextLength( System.IntPtr hWnd );
  55. [DllImport( "user32.dll" )]
  56. public static extern IntPtr FindWindowEx( IntPtr parentWindow, IntPtr previousChildWindow, string windowClass, string windowTitle );
  57. [DllImport( "user32.dll" )]
  58. public static extern IntPtr GetActiveWindow();
  59. [DllImport( "user32.dll" )]
  60. public static extern bool GetWindowRect( System.IntPtr hwnd, ref Rect rectangle );
  61. static public IntPtr[] GetProcessWindows( int processId )
  62. {
  63. List<IntPtr> output = new List<IntPtr>();
  64. IntPtr winPtr = IntPtr.Zero;
  65. do
  66. {
  67. winPtr = FindWindowEx( IntPtr.Zero, winPtr, null, null );
  68. int id;
  69. GetWindowThreadProcessId( winPtr, out id );
  70. if( id == processId )
  71. output.Add( winPtr );
  72. } while( winPtr != IntPtr.Zero );
  73. return output.ToArray();
  74. }
  75. public struct Rect
  76. {
  77. public int Left { get; set; }
  78. public int Top { get; set; }
  79. public int Right { get; set; }
  80. public int Bottom { get; set; }
  81. public int Width { get { return Right - Left; } }
  82. public int Height { get { return Bottom - Top; } }
  83. public override string ToString()
  84. {
  85. return "(l: " + Left + ", r: " + Right + ", t: " + Top + ", b: " + Bottom + ")";
  86. }
  87. }
  88. public static bool GetProcessRect( System.Diagnostics.Process process, ref Rect rect )
  89. {
  90. IntPtr[] winPtrs = WindowsUtil.GetProcessWindows( process.Id );
  91. for( int i = 0; i < winPtrs.Length; i++ )
  92. {
  93. bool gotRect = WindowsUtil.GetWindowRect( winPtrs[ i ], ref rect );
  94. if( gotRect && ( rect.Left != 0 && rect.Top != 0 ) )
  95. return true;
  96. }
  97. return false;
  98. }
  99. public static void SetWindowPosition( int x, int y, int sizeX = 0, int sizeY = 0 )
  100. {
  101. System.Diagnostics.Process process = System.Diagnostics.Process.GetCurrentProcess();
  102. process.Refresh();
  103. EnumWindows( delegate ( System.IntPtr wnd, System.IntPtr param )
  104. {
  105. int id;
  106. GetWindowThreadProcessId( wnd, out id );
  107. if( id == process.Id )
  108. {
  109. SetWindowPos( wnd, 0, x, y, sizeX, sizeY, sizeX * sizeY == 0 ? 1 : 0 );
  110. return false;
  111. }
  112. return true;
  113. }, System.IntPtr.Zero );
  114. }
  115. }
  116. #endif