Need to invoke API functions
You need to introduce namespaces using System.Runtime.InteropServices at the beginning;
Get current Window handle: GetForegroundWindow ()
[DllImport ("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] public static extern IntPtr GetForegroundWindow () ;
The return value type is IntPtr, which is the handle of the currently acquired focus window
Method of Use: IntPtr Myptr=getforegroundwindow ();
After you get to the window handle, you can manipulate the window. For example, close the window or hide it in the window so that it appears
[DllImport ("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] public static extern int ShowWindow (INTPTR hwnd, I NT nCmdShow);
Wherein ShowWindow (INTPTR hwnd, int ncmdshow);
The meaning of nCmdShow
0 closing the window
1 Normal Size display window
2 Minimizing windows
3 Maximize Window
Use example: ShowWindow (myptr, 0);
To get the window size and position: Call method GetWindowRect (IntPtr hWnd, ref RECT LpRect)
[DllImport ("User32.dll")] [Return:marshalas (Unmanagedtype.bool)] static extern Bool GetWindowRect (IntPtr hWnd, ref RECT lpRect);
[StructLayout (layoutkind.sequential)] public struct RECT {public int left;//leftmost coordinate public int Top;//top coordinate public int right ; Right-most coordinate public int Bottom; Bottom-most coordinates}
Example:
Inptr Awin = GetForegroundWindow (); Gets the current window handle of the rect rect = new rect (); GetWindowRect (Awin, ref rect); int width = rc. RIGHT-RC. Left; The width of the window int height = rc. BOTTOM-RC. Top; The height of the window is int x = RC. Left; int y = rc. Top;
API Capture Screen