Use VC to intercept programming Cool

zhaozj2021-02-11  256

---- Screen interception is something that is more interested. Although there are now many applications such as Hypersnap can be used to intercept the screen screen you like, but if you can add this function to your own program, It is more powerful to use it.

---- The following uses VC to gradually introduce the implementation process under Windows 95. First we want to determine the area intercepted in the screen, define using the LPRect structure. You can intercept a window, or the entire screen. The following code copies the selected screen area In place.

HbitMap CopyScreenTOBITMAP (LPRECT LPRECT)

// LPRECT represents the selected area

{

HDC HSCRDC, HMEMDC;

// Screen and Memory Device Description Table

Hbitmap hbitmap, holdbitmap;

// bit map handle

INT NX, NY, NX2, NY2;

// Select region coordinates

INT NWIDTH, NHEIGHT;

// bitmap width and height

Int xscrn, yscrn;

// Screen Resolution

/ / Make sure the selected area is not empty rectangle

IF (IsRectempty (LPRECT))

Return NULL;

/ / Create a device description table for the screen

HSCRDC = CREATEDC ("Display", NULL, NULL, NULL;

/ / Create a compatible memory device description for the screen device description table

HMEMDC = CREATECOMPATIBLEDC (HSCRDC);

/ / Get selected area coordinates

Nx = lprect-> left;

NY = lpRect-> TOP;

NX2 = lpRect-> Right;

NY2 = LPRECT-> BOTTOM;

// Get the screen resolution

Xscrn = getDeviceCaps (HSCRDC, Horzres);

YSCRN = GetDeviceCaps (HSCRDC, Vertres);

/ / Ensure that the selected area is visible

IF (NX <0)

Nx = 0;

IF (NY <0)

NY = 0;

IF (nx2> xscrn)

NX2 = xscrn;

IF (NY2> YSCRN)

NY2 = YSCRN;

NWIDTH = NX2 - NX;

NHEIGHT = NY2 - NY;

// Create a bitmap compatible with the screen device description table

hbitmap = createcompatibleBitmap

(HSCRDC, NWIDTH, NHEIGHT);

/ / Put the new bitmap in the memory device descriptor table

HoldbitMap = SELECTOBJECT (HMEMDC, HBitmap);

/ / Copy the screen device description table to the memory device descriptor table

Bitblt (Hmemdc, 0, 0, NWIDTH, NHEIGHT,

HSCRDC, NX, NY, SRCCOPY;

/ / Get the handle of the screen bitmap

Hbitmap = selectObject (HMEMDC, Holdbitmap);

// Clear

Deletedc (HSCRDC);

Deletedc (HMEMDC);

// Return the bitmap section

Return hbitmap;

}

After getting the screen bitmap handle, we

You can paste the screen content onto the clipboard.

OpenClipboard (hwnd))

// hwnd is the program window handle

{

// Empty clipboard

EMPTYCLIPBOARD ();

// Paste the screen content into the clipboard,

Hbitmap is the screen bitmap handle

SetClipboardData (cf_bitmap, hbitmap);

// Close the clipboard

CloseClipboard ();

}

We can also save the screen content to disk files in a bitmap format.

Int SaveBitmaptofile (Hbitmap Hbitmap,

LPSTR LPFILENAME) // hbitmap for the screen bitmap handle {// lpfilename is a bitmap file name

HDC HDC;

// Device Description Table

INT IBITS;

/ / The number of items per pixel at the current display resolution

Word wbitcount;

////Bit of each pixel accounts for each pixel

/ / Define the palette size, the pixel byte size in the bit map,

Bitmap file size, write file byte

DWORD dwpalettesize = 0,

DWBMBITSSIZE,

DWDIBSIZE, DWWWWRITEN

Bitmap bitmap;

// bit map attribute structure

BitmapfileHeader BMFHDR;

// bitmap file head structure

BitmapInfoheader Bi;

// bitmap information head structure

LpbitmapInfoHeader LPBI;

/ (Pointing the bitmap information head structure

Handle FH, HDIB, HPAL, HOLDPAL = NULL

/ / Define files, allocate memory handles, palette handles

/ / Calculate the number of bytes per pixel per pixel

HDC = CREATEDC ("Display", NULL, NULL, NULL;

Ibits = getDeviceCaps (HDC, Bitspixel) *

GetDeviceCaps (HDC, Plaso);

Deletedc (HDC);

IF (iBITS <= 1)

Wbitcount = 1;

Else IF (iBITS <= 4)

Wbitcount = 4;

ELSE IF (iBITS <= 8)

Wbitcount = 8;

Else IF (iBITS <= 24)

Wbitcount = 24;

/ / Calculate the size of the palette

IF (wbitcount <= 8)

DWPALETESIZE = (1

SizeOf (RGBQUAD);

/ / Set the bitmap information head structure

GetObject (HbitMap, Sizeof (Bitmap), (LPSTR) & Bitmap;

bi.bisize = sizeof (bitmapinfohead);

bi.biwidth = bitmap.bmwidth;

bi.biheight = bitmap.bmheight;

Bi.Biplanes = 1;

Bi.biBitcount = WbitCount;

Bi.Bicompression = BI_RGB;

Bi.bisizeImage = 0;

bi.bixpelspermeter = 0;

Bi.biypelspermeter = 0;

Bi.bi.biRused = 0;

Bi.Biclrimportant = 0;

DWBMBITSSIZE = ((bitmap.bmwidth *

Wbitcount 31) / 32) * 4

* Bitmap.bmheight;

/ / Distribute memory for bitmap content

HDIB = GLOBALLOC (GHND, DWBMBITSSIZE

DWPALETESIZE SIZEOF (BitmapInfoHeader);

LPBI = (LPbitMapInfoHeader) Globalock (HDIB);

* lpbi = bi;

// Process the palette hpal = getStockObject (Default_palette);

IF (HPAL)

{

HDC = GetDC (NULL);

Holdpal = SELECTPALETTTE (HDC, HPAL, FALSE);

RealizePalette (HDC);

}

/ / Get new pixel values ​​under this palette

GetDibits (HDC, Hbitmap, 0, (UINT) Bitmap.Bmheight,

(LPSTR) LPBI SIZEOF (BitMapInfoHeader)

DWPALETESIZE,

(BitMapInfoHeader *)

LPBI, DIB_RGB_COLORS);

// Restore palette

IF (Holdpal)

{

SelectPalette (HDC, Holdpal, True);

RealizePalette (HDC);

ReleaseDC (NULL, HDC);

}

// Create a bitmap file

FH = CREATEFILE (lpfilename, generic_write,

0, NULL, CREATE_ALWAYS,

FILE_ATTRIBUTE_NORMAL | FILE_

Flag_sequential_scan, null);

IF (fH == invalid_handle_value)

Return False;

/ / Set the bitmap file header

BMFHDR.BFTYPE = 0x4d42; // "BM"

DWDIBSIZE = Sizeof (BitmapfileHeader)

Sizeof (BitmapInfoHeader)

DWPALETESIZE DWBMBITSSIZE;

BMFHDR.BFSIZE = DWDIBSIZE;

BMFHDR.BFRESERVED1 = 0;

BMFHDR.BFRESERVED2 = 0;

BMFHDR.BFOFFBITS = (DWORD) SIZEOF

(BitmapFileHeader)

(DWORD) SIZEOF (BitmapInfoHeader)

DWPALETESIZE;

// Write the bitmap file header

Writefile (FH, (LPSTR) & BMFHDR, SIZEOF

(BitmapfileHeader), & dwwritten, null;

/ / Write the rest of the file

Writefile (FH, (LPSTR) LPBI, DWDIBSIZE,

& dwwritten, null;

// Clear

GlobalUnlock (HDIB);

GlobalFree (HDIB);

CloseHandle (FH);

}