WindowsUtil.cs 5.3 KB

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