Old topic: I have written only cut screen capture software window client area (VB2010)

Original link: http://www.cnblogs.com/grenet/p/3929937.html

Now able to achieve a lot of screen capture software, will not list them, even WIN7 comes with software screenshots, screenshots have even OFFICE2010 start function.

 

Screen capture software, though many, nothing less than the three kinds of screen shots ways: full screen capture, window capture, custom rectangle screenshot.

 

Wherein screenshot of the window used more, the following is an example of a screenshot of the window:

image

 

But sometimes we just want to intercept the client area of ​​the window, as shown below:

image

 

Such software is not much compromise is to use a custom rectangular shots, but to adjust the rectangle is not a very easy thing to do.

 

So, based on the spirit of the code of agricultural self-sufficiency.

So search the Internet search solutions. There are two

1, based on the function Win API functions PrintWindow

2, a method based on the Graphics object CopyFromScreen

Both methods have advantages and disadvantages

 

PrintWindow function is Hwnd contents of the specified window to the specified Hdc in the drawing, based on the completion of the background. Even when specified window is minimized, it can also draw content to Hdc when the window is normal. Estimation principle is issuing a drawing command, the system will draw the window contents. However, this method has many limitations, if DirectX and other non-GDI window contents useful method, the image is a black interception.

 

CopyFromScreen method is actually the content on the screen capture to the Bitmap object. The advantage is through system optimization, it can intercept the content of DirectX contain non GDI method. Disadvantage is that the screen is taken, it is specified window can not be minimized, but also their calculated range to be taken.

 

Due to intercept content comprising non DirectX GDI method. Therefore, the method employed herein is for CopyFromScreen.

 

Question is how to calculate the scope of the service area of ​​the specified window.

Win API requires using the following function:

FindWindowByCaption: to find the specified window title text, return to the window handle Hwnd

GetWindowRect: get Hwnd designated area of ​​the window, return True to the region to be successful, get in the window parameter lpRect.

GetClientRect: Hwnd for the specified client area of ​​the window area and returns nonzero if successful, the window area to get in lpRect parameters in the client area. However, X and Y components are 0 in the region, i.e. the width and height can only be obtained in the region, the position of the region can not be obtained on the screen.

ClientToScreen: the coordinates of the client area converted into screen coordinates. This function can be obtained with the function GetClientRect client area of ​​the window region (including the X and Y components, i.e., the position of the region on the screen)

 

Process specific area in the client area is obtained as follows:

1, the client area of ​​the window area with GetClientRect

(0,0) coordinates on the screen 2, obtained by ClientToScreen function of the client area, also in the offset position on the screen client area.

3, the offset is added to the zone of step 1, that is the client area of ​​the complete region (including the X and Y components, i.e., the position of the region on the screen)

 

Win API reintroduction two auxiliary functions:

OpenIcon: Hwnd the specified window is reduced to a normal (i.e. minimized window to restore to the normal window)

BringWindowToTop: Hwnd the specified window at the top, not covered by other windows

 

 


    Public  Shared  Function SnapWindowByCaption(Caption As  String, Optional OnlyClient As  Boolean = False, Optional AutoRestore As  Boolean = False, Optional AutoBringToTop As  Boolean = False) As  Bitmap
        Dim Hwnd As  IntPtr = FindWindowByCaption(0, Caption)
        If Hwnd = 0 Then  Return  Nothing

        Dim R As  New  RECT(0, 0, 0, 0)

        GetWindowRect(Hwnd, R)

        If R.Width = 0 Then
            If AutoRestore = True  Then
                OpenIcon(Hwnd)
                GetWindowRect(Hwnd, R)
            Else
                Return  Nothing
            End  If
        End  If

        If AutoBringToTop = True  Then BringWindowToTop(Hwnd)

        Dim P As  New  WinPOINT(0, 0)

        If OnlyClient = True  Then
            GetClientRect(Hwnd, R)
            ClientToScreen(Hwnd, P)
            R.X += P.X
            R.Y += P.Y
        End  If

        Dim w As  Integer = R.Width
        Dim h As  Integer = R.Height
        Dim bmp As  Bitmap = New  Bitmap(w, h)
        Dim g As  Graphics = Graphics.FromImage(bmp)

        g.CopyFromScreen(R.X, R.Y, 0, 0, New  Size(w, h))

        Return bmp
    End  Function

Concluded that affirms Win API functions point the way, this article will be on the following Web site, the site is very powerful

http://www.pinvoke.net/index.aspx

Reproduced in: https: //www.cnblogs.com/grenet/p/3929937.html

Guess you like

Origin blog.csdn.net/weixin_30429201/article/details/94784267