Screen Scraping program (bitmap DDB example)

Source: Internet
Author: User

The screen grabber means that the entire screen diagram is displayed in the user area of the application, equivalent to.

To work with a desktop window:
First you need to know the width and height of the desktop window, get wide and high need to take advantage of the window of the device handle, and get the device handle need to know the window handle, this series of links are as follows:
The window handle--and the device handle--Gets the properties of the device handle (such as width and height). (a method used in books, but at least a review of this kind of relationship is needed, it must be admitted that beginners need to repeat many times to form a sense of proficiency)

Hwnddesktop = GetDesktopWindow (); // gets the window handle of the desktop window hdcdesktop = GetDC (hwnddesktop); // Create a Device description table for a basic desktop window

There are two functions to be borrowed here, and there is another way to directly create a DC for a desktop window device.

Hdcdesktop = CreateDC ("DISPLAY", Null,null,null); // CreateDC () Gets the entire screen of the DC, one step

With the handle, you can get (or set) the various properties of the DC, such as how wide, how high, etc... Implemented by the function GetDeviceCaps (). Excerpt function prototypes are as follows:

int GetDeviceCaps (    // device DC    int// index entry returned );

There is a lot of information on the device DC, so the nindex is especially large. In this example only need to get the width and height of the screen.

Cxscreen = GetDeviceCaps (Hdcdesktop, horzres);  // gets the size of the desktop window, also equivalent to the lower-right corner of the rectangle x, y coordinates cyscreen = GetDeviceCaps (Hdcdesktop, vertres);

The next process is cumbersome, but described in the simplest way, after getting the screen DC, create a compatible DC (compatible screen device), and a compatible bitmap (compatible screen), put a compatible bitmap in a compatible DC, ready to use it to hold the screen bitmap. Can it be understood as a container in a popular sense? Compatible DC accommodates compatible bitmaps.

// Create a Memory Device Description table compatible with the display device description table hbitmap = CreateCompatibleBitmap (hdcdesktop, Cxscreen, Cyscreen); SelectObject (Hdcmem, hbitmap); // and select into the Memory Device description table

The device is ready, and the screen's bitmap is finally moved to a compatible bitmap in the compatible DC.

0 0 0 0 , srccopy);

Again it looks like the BitBlt () function, excerpt as follows:

BOOL BitBlt (HDC hdcdest,//Target Device Description tableintNxdest,//x-coordinate of the upper-left corner of the destination rectangleintNydest,//y-coordinate of the upper-left corner of the destination rectangleintNwidth,//Target Rectangle widthintNheight,//Target Rectangle HeightHDC hDCSrc,//Source Device Description TableintNXDSRC,//source Rectangle x coordinate (upper left corner)intNYDSRC,//Source Rectangle y-coordinateDWORD Dwrop//Raster Operation performed);

One of the last DWORD Dwrop raster operation, let me think of the C language graphics replication, is in the process of copying, how to do some kind of mixed operation with the target, such as to make it anti-color (black to white, white to black), or, XOR, or operation, and so on, probably this meaning.

Compatible memory Device Description table--Application Device Description table

The Memory Device Description table is finished, and then the bitmap in the Memory Device description table is copied to the user area of the current application and let it be displayed.
That is: compatible memory description table and application device description table.

Books here to consider such a problem, the way to properly scale the map to adapt to the size of the user area, so the use of another copy function StretchBlt. Its parameters are similar to those of BitBlt, omitted.
However, before zooming, a zoom mode is set, which is implemented by the function Setstretchbltmode, excerpt as follows:

int Setstretchbltmode (hdc hdc,int  istretchmode); // options for Istretchmode Blackonwhite; // preserves black pixels, clears white pixels, and is typically used in monochrome bitmaps Coloroncolor; // typically used to preserve the color of a bitmap in a color bitmap Whiteonblack; // Preserve white pixels, clear black pixels Halftone; // complex processing of source images, slow but high-quality images

Parameters can be arbitrarily tested, obviously halftone parameters cut out a lot of clear figure.
This series of move-to-move copies completes the work of the screen, involving a series of bitmap functions, so it is necessary to summarize them carefully.

/*---------------Desktop Program Research edition-----------------*/#include<windows.h>LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);intWINAPI WinMain (hinstance hinstance, hinstance hprevinstance,pstr szCmdLine,inticmdshow) {     StaticTCHAR szappname[] = TEXT ("Hellowin") ;                     HWND hwnd;                     MSG msg;               Wndclass Wndclass; Wndclass.style= Cs_hredraw |Cs_vredraw; Wndclass.lpfnwndproc=WndProc; Wndclass.cbclsextra=0 ; Wndclass.cbwndextra=0 ; Wndclass.hinstance=hinstance; Wndclass.hicon=LoadIcon (NULL, idi_application); Wndclass.hcursor=loadcursor (NULL, Idc_arrow); Wndclass.hbrbackground=(Hbrush) getstockobject (White_brush); Wndclass.lpszmenuname=NULL; Wndclass.lpszclassname=Szappname; if(! RegisterClass (&wndclass)) {            return 0 ; } hwnd=CreateWindow (Szappname, TEXT ("Mouse Messages"), Ws_overlappedwindow, Cw_usedefault,                                       Cw_usedefault, Cw_usedefault,                                                 Cw_usedefault, NULL, NULL,                               HINSTANCE, NULL);             ShowWindow (hwnd, icmdshow);                           UpdateWindow (HWND);  while(GetMessage (&msg, NULL,0,0) {translatemessage (&msg); DispatchMessage (&msg); }     returnMsg.wparam;} LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM LPARAM) {StaticHDC Hdc,hdcdesktop,hdcmem;     Paintstruct PS; Static intcxscreen,cyscreen,cxclient,cyclient;     Hbitmap Hbitmap; Switch(message) { Casewm_create://hwnddesktop = GetDesktopWindow ();//gets the window handle of the desktop window//hdcdesktop = GetDC (hwnddesktop);//Create a Device description table for a basic desktop windowHdcdesktop= CreateDC ("DISPLAY", null,null,null);//CreateDC () Gets the entire screen of the DC, one stepHdcmem = CreateCompatibleDC (hdcdesktop);//creating a compatible memory Device description TableCxscreen= GetDeviceCaps (Hdcdesktop, horzres);//get the size of the desktop windowCyscreen =GetDeviceCaps (Hdcdesktop, vertres); Hbitmap= CreateCompatibleBitmap (Hdcdesktop, Cxscreen, Cyscreen);//Create a compatible bitmapSelectObject (Hdcmem, HBITMAP);//SELECT into Memory device description table//ShowWindow (hwnd, sw_hide);//is it necessary to hide the window first? window does not appear at this timeBitBlt (Hdcmem,0,0, Cxscreen, Cyscreen,hdcdesktop,0,0, srccopy);//transfer pixels from a desktop bitmap to a compatible bitmap//Sleep (1000); //ShowWindow (hwnd, sw_show);//Display windowDeleteDC (hdcdesktop); return 0 ;  Casewm_size:cxclient=LoWord (LParam); Cyclient=HiWord (LParam); return 0;  CaseWM_PAINT:HDC= BeginPaint (hwnd, &PS); //display the bitmap compression of the Memory Device description table in the program window//Setstretchbltmode (hdc, coloroncolor);//observation of arbitrary selection of istretchmode parameters//Setstretchbltmode (hdc, halftone);STRETCHBLT (HDC,0,0, Cxclient, Cyclient, Hdcmem,0,0, Cxscreen, Cyscreen, srccopy); EndPaint (hwnd,&PS); return 0 ;  CaseWm_destroy:postquitmessage (0) ; return 0 ; }     returnDefWindowProc (hwnd, message, WParam, LParam);}

Screen Grabber (bitmap DDB example)

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.