Today I bring you an interesting example, a bit like Spy + + function, move through the screen with the mouse, and capture the window information at the specified coordinates in real time.
The window information includes the window caption, the window handle, the window class name, and the thumbnail that renders the captured window.
Now let's think about what technical points we need to prepare to implement these functions.
1, get the current mouse pointer screen coordinates, this use of the System.Windows.Forms namespace cursor class position properties can know the current mouse pointer position, screen coordinates.
2, how to get the window from the specified coordinates, in fact, to get the corresponding window handle, here to use an API function Windowfrompoint, it can return the specified coordinates at the window handle. This window does not necessarily refer to the complete window, in the Win32 window, a control is also a window, the desktop is also a window.
3, get the window title text, using API function GetWindowText, according to the window's handle to get the title text of the window.
4, Get window class name, use API function GetClassName, get corresponding window to belong to window class, this refers to the window class is when we develop WIN32 program, similar to the WinMain function in the RegisterClass function registered class name.
5, the window content into thumbnails, this simple, in the System.Drawing namespace of the graphics class has a CopyFromScreen method, you can copy the image from the screen, the effect is equivalent to using the BitBlt function from the desktop of the DC copy to another location The same.
6, we are not copying the entire screen, but only the corresponding position of the window, to get the window of the rectangular area, you can invoke API function GetWindowRect.
Well, now that the technical points have been solved, the next step is real thing.
The first is to import the Win32 API.
[DllImport ("User32.dll", callingconvention =
callingconvention.stdcall)] public
extern static INTPTR Windowfrompoint (int x, int y);
[DllImport ("User32.dll", CallingConvention = Callingconvention.stdcall)]
public extern static int getclassname (
[in] IntPtr hwnd,
[out, MarshalAs (UNMANAGEDTYPE.LPSTR)] StringBuilder Lpstring,
[in] int nmaxcount);
[DllImport ("User32.dll", CallingConvention = Callingconvention.stdcall)]
public extern static int GetWindowText (
[in] IntPtr hwnd,
[out, MarshalAs (UNMANAGEDTYPE.LPSTR)] StringBuilder lpstring,
[in] int nmaxcount);
[DllImport ("User32.dll")]
public extern static bool GetWindowRect (INTPTR hwnd, out RECT lprect);
[StructLayout (layoutkind.sequential)]
public struct RECT
{public
int left;
public int top;
public int right;
public int bottom;
}