Screen area intercept image implementation

xiaoxiao2021-03-06  72

Screen area intercept image implementation

Now there are many programs that may have to use the screen area to intercept the image. I don't know how to implement it. In fact, this function is very simple, it is a zone to select the screen, and then use the selected area to copy it. It's time, but the problem is how we get that selection area, which has become the key to solving this problem. In a normal application, we have a better realization of the region because most components contain mousedown, mousemove, mouseup Incident, we can get this in these three events, which we intercept the image is often a region of the screen. Our mouse as long as the program is outside the window, the program window will lose the focus immediately, so How can we achieve the selection of the region, how can this problem solve? At first glance, this problem is a bit difficult. You are right, how do we get a selection area outside the program window, but solve the answer to the question may simply scare you, since you can't get the selection area outside the program window. , Then we can use it in our program window, but also use you, if you still need you in the program window, I have already solved it, but there are many times the key to solving the problem is often very simple. However, you didn't think of it or not to believe in this simple but to ignore, the answer is in the window of the program, because only in the window we can easily complete the selection and copy of the area, just before the form is displayed Put your screen down to your program, then maximize your program window, the later operation is the same as the flat operation, in the mousedown, mousemove, MouseUp event of the graphics component, complete the copy of the area in the MouseUp event But I still want to remind everyone how to complete the dotted box of the region, this can let the user know that he (she) said the size of the selection area, I found this problem, starting my procedure, although the area Copy, but it did not show a virtual line box to the user, so that the user is very unhappy, and then inadvertently discovered that the DrawFocusRect (const trect "method of the Canvas component can help me solve this problem, its prototype is as follows:

Void __fastcall drawfocusRect (const trect & respons);

The function of this method is focused, when the method is called for the second time, if the parameter Rect is still the first rectangular area, it will delete this area, so as long as we delete first in the mousemove event The last rectangular area is called to complete the drawing of the dotted frame. code show as below:

Void __fastcall tform2 :: image1mousemove (Tobject * Sender, TshiftState Shift,

INT X, int y)

{

TRECT RECT;

IF (Drawing)

{

Rect = getRect (OrigInpt, Movept);

Image1-> Canvas-> DrawFocusRect (RECT);

Rect = GetRect (OrigInpt, Point (x, y));

Image1-> Canvas-> DrawFocusRect (RECT);

}

Movept = Point (x, y);

}

Where getRect () is a custom function, its function is based on the origin area of ​​the originpt, Movept, which defines this function because my program often uses the rectangular area, so for the simplicity of the program. .

Its code is as follows:

TRECT __FASTCALL TFORM2 :: GetRect (TPoint Second) {

// Todo: Add your source code here

TRECT R;

IF (Originpt.x <= second.x && originpt.y <= second.y) // is pulled by the upper left corner

{

R.LEFT = OriginPt.x;

R.TOP = Originpt.y;

R.right = second.x;

R.BOTTOM = Second.y;

}

IF (Originpt.x <= second.x && originpt.y> = second.y) // is pulled by the upper left corner to the upper right

{

R.LEFT = OriginPt.x;

R.TOP = second.y;

R.right = second.x;

R.BOTTOM = Originpt.y;

}

IF (Originpt.x> = second.x && originpt.y <= second.y) // is pulled by the upper right corner to the lower left corner

{

R.LEFT = Second.x;

R.TOP = Originpt.y;

R.right = ORIGINPT.X;

R.BOTTOM = Second.y;

}

IF (Originpt.x> = Second.x && Originpt.y> = Second.y) // is pulled by the lower left corner

{

R.LEFT = Second.x;

R.TOP = second.y;

R.right = ORIGINPT.X;

R.BOTTOM = Originpt.y;

}

Return R;

}

As soon as the number of words, if you add the program code, it will rise immediately. If you go to the contribution, you will not send it, of course, this is just talking, let alone, if you really submit, you still have to finish it, okay, words Not much to say (in fact, I have already said so much), let me introduce the main process of this program!

To achieve interception of screen areas, the main steps:

1. Set the WindowsTate property of the program window to WSMaximized, maximize the window, occupy the entire area of ​​the screen.

2. Complete the copy of the entire screen area in the onActivate event of the Form form. At this time, the program window has not appeared, and the machine will be copied to the image of the copy to the image1 of the TIMAGE control (I am using the grapscreen () function to complete the screen area copy , This is a custom function, and the code is described later)

3, use image1's onmousemove event to track the mouse coordinate, record it into the Movept structure, and complete the selection of the user's dashed box in this event (using the canvas DrawFocusRect () method)

4, iMAGE1 onMousedown and OnMouseUp complete the copy of the image area selection status and image area, respectively.

5, final, but the user presses the ESC key or press the exit button exits the program in the right mouse button, where you can click the right click to complete the image save function!

After knowing the principle of intercepting the image, you can also complete a similar program according to the above steps. Here is my sample program. Of course, I still only columnize the key code. If you are interested in the program, you can go to http://www.zccfamily.com/zqget/ website to download this sample program, there are other Things may help you, you can also download it back.

According to the above steps, my code is as follows:

Step 1: Set the WindowsTate property of the Form Form to WSMaximized in the Properties Editor, and set the align property of Image1 to AlClient.

Step 2: Copy screen area

Void __fastcall tform2 :: formactivate (TOBJECT * Sender)

{

Image1-> Picture-> Bitmap-> Handle = grapscreen ();

}

The grapScreen () function is defined as follows:

HDC __fastcall tform2 :: grapscreen ()

{

// Todo: Add your source code here

// The window must be the full screen, which is WindowState, which can guarantee the high and wide of Clienheight, ClientWidth as the entire screen.

HDC Sourcedc = (HDC) Createdc ("Display", "," ", NULL);

HDC DESTDC = (HDC) CreateCompatiBLEDC (SOURCEDC);

HDC Bhandle = (HDC) CreateCompaPaultBitmap (Sourcedc, ClientWidth, ClientHeight);

SelectObject (destdc, bhandle);

Bitblt (Destdc, 0,0, ClientWidth, ClientHeight, Sourcedc, 0,0, srcopy;

Deletedc (destdc);

ReleaseDC (Bhaldle, Sourcedc);

Return bhandle;

}

There are some WinAPI functions to complete the copy of the screen, and the usage of these API functions can be referred to MSDN.

Step 3: I have already listed above.

the fourth step:

OnMouseDown event:

Void __fastcall tform2 :: image1mousedown (TOBJECT * SENDER,

TMOUSEBUTTON button, TshiftState Shift, Int X, Int Y)

{

IF (Button == Mbright)

{

Return; // If the user presses the right mouse button, return and pop up the pop-up menu for the user to select the menu command.

}

Drawing = True;

Originpt = Point (x, y);

Movept = Point (x, y);

IF (SELECTED)

{

Image1-> Canvas-> DrawFocusRect (SELECTRECT);

}

}

This code mainly marks the status of the picture interception and the start point position of the record mouse. If the user points right click, the pop-up menu is popped, which needs to set the image1 PopupMenu property to your pop-up menu in the Properties Editor. Name, of course, you can also use code to do.

IF (Button == Mbright)

{

Popupmenu1-> Popup (x, y);

Return;

}

OnMouseUp event:

Void __fastcall tform2 :: image1mouseup (TOBJECT * Sender, TMouseButton Button,

TshiftState Shift, Int X, Int Y)

{

IF (Drawing)

{

SelectRect = GetRect (OrigInpt, Movept);

GetSelectImage (SELECTRECT);

SELECTED = TRUE;

Drawing = false;

}

}

Where GetSelectImage (SELECTRECT) is also a custom function, its function is to copy the image of the SelectRect Rectangular area to SELECTIMAGE to save the variables of the captured image, that is, selectImage is the image we taken. code show as below:

Void __fastcall tform2 :: getSelectImage (TRECT ARECT)

{

// Todo: Add your source code here

TRECT R;

SelectImage-> width = (all ());

SelectiMage-> height = (all ();

R = Rect (0, 0, SelectImage-> width, selectimage-> height);

SelectImage-> Canvas-> CopyRect (r, image1-> canvas, all);

}

According to the above steps, I already enumerate the key code of the program, and the rest is only the definition of some variables, I want to read by the source, I should be able to understand, I will not listen to it here.

转载请注明原文地址:https://www.9cbs.com/read-109223.html

New Post(0)