How to capture window information on the desktop

Source: Internet
Author: User
Tags win32 win32 window

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;  
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.