截图c++代码

Capturing an Image

E文不好的人就直接看代码吧

一、说明

You can use a bitmap to capture an image, and you can store thecaptured image in memory, display it at a different location inyour application's window, or display it in another window.

In some cases, you may want your application to capture imagesand store them only temporarily. For example, when you scale orzoom a picture created in a drawing application, the applicationmust temporarily save the normal view of the image and display thezoomed view. Later, when the user selects the normal view, theapplication must replace the zoomed image with a copy of the normalview that it temporarily saved.

To store an image temporarily, your application must callCreateCompatibleDCto create a DC that is compatible with the current window DC. Afteryou create a compatible DC, you create a bitmap with theappropriate dimensions by calling the CreateCompatibleBitmapfunction and then select it into this device context by calling theSelectObjectfunction.

After the compatible device context is created and theappropriate bitmap has been selected into it, you can capture theimage. The BitBltfunction captures images. This function performs a bit blocktransfer that is, it copies data from a source bitmap into adestination bitmap. However, the two arguments to this function arenot bitmap handles. Instead, BitBlt receives handles thatidentify two device contexts and copies the bitmap data from abitmap selected into the source DC into a bitmap selected into thetarget DC. In this case, the target DC is the compatible DC, sowhen BitBlt completes the transfer, the image has beenstored in memory. To redisplay the image, call BitBlt asecond time, specifying the compatible DC as the source DC and awindow (or printer) DC as the target DC.

The following example code, from an application that captures animage of the entire desktop, creates a compatible device contextand a bitmap with the appropriate dimensions, selects the bitmapinto the compatible DC, and then copies the image using theBitBlt function.

二、源代码

// Create a normal DC and a memory DC for the entire screen. The // normal DC provides a "snapshot" of the screen contents. The // memory DC keeps a copy of this "snapshot" in the associated // bitmap.  hdcScreen = CreateDC("DISPLAY", NULL, NULL, NULL); hdcCompatible = CreateCompatibleDC(hdcScreen);  // Create a compatible bitmap for hdcScreen.  hbmScreen = CreateCompatibleBitmap(hdcScreen,                      GetDeviceCaps(hdcScreen, HORZRES),                      GetDeviceCaps(hdcScreen, VERTRES));  if (hbmScreen == 0)     errhandler("hbmScreen", hwnd);  // Select the bitmaps into the compatible DC.  if (!SelectObject(hdcCompatible, hbmScreen))     errhandler("Compatible Bitmap Selection", hwnd);          // Hide the application window.          ShowWindow(hwnd, SW_HIDE);           //Copy color data for the entire display into a          //bitmap that is selected into a compatible DC.          if (!BitBlt(hdcCompatible,                0,0,                bmp.bmWidth, bmp.bmHeight,                hdcScreen,                0,0,                SRCCOPY))          errhandler("Screen to Compat Blt Failed", hwnd);          // Redraw the application window.          ShowWindow(hwnd, SW_SHOW); 
 
================
三、重要的API函数解释

CreateDC


The CreateDC function creates a device context (DC) for a device using the specified name.
HDC CreateDC(  LPCTSTR lpszDriver,        // driver name  LPCTSTR lpszDevice,        // device name  LPCTSTR lpszOutput,        // not used; should be NULL  CONST DEVMODE* lpInitData  // optional printer data);
Parameters
lpszDriver
Windows 95/98/Me: The lpszDriver parameter can beNULL, WINSPL16 (a print provider), or (to obtain a display DC) itcan be either the null-terminated string DISPLAY or the device nameof a specific display device. If lpszDevice specifies aparticular device, you must use NULL for lpszDriver.

Windows NT 4.0: Pointer to a null-terminated characterstring that specifies either DISPLAY or the name of a printprovider, which is usually WINSPOOL.

Windows 2000/XP: Pointer to a null-terminated characterstring that specifies either DISPLAY or the name of a specificdisplay device or the name of a print provider, which is usuallyWINSPOOL.

lpszDevice
[in] Pointer to a null-terminated character string thatspecifies the name of the specific output device being used, asshown by the Print Manager (for example, Epson FX-80). It is notthe printer model name. The lpszDevice parameter must beused.

To obtain valid names for displays, call EnumDisplayDevices.

If lpszDriver is DISPLAY or the device name of a specificdisplay device, then lpszDevice must be NULL or that samedevice name. If lpszDevice is NULL, then a DC is created forthe primary display device.

Windows NT 3.51 and Windows NT 4.0: There is only one(thus the primary) display device. Set lpszDevice toNULL.

Windows 2000 and later: If there are multiple monitors onthe system, calling CreateDC(TEXT("DISPLAY"),NULL,NULL,NULL) willcreate a DC covering all the monitors.

lpszOutput
This parameter is ignored and should be set to NULL. It isprovided only for compatibility with 16-bit Windows.
lpInitData
[in] Pointer to a DEVMODEstructure containing device-specific initialization data for thedevice driver. The DocumentPropertiesfunction retrieves this structure filled in for a specified device.The lpInitData parameter must be NULL if the device driveris to use the default initialization (if any) specified by theuser.

If lpszDriver is DISPLAY, then lpInitData must beNULL. The display device's current DEVMODE is used.

Return Values

If the function succeeds, the return value is the handle to a DC for the specified device.
If the function fails, the return value is NULL. The function will return NULL for a DEVMODE structure other than the current DEVMODE.
Windows NT/2000/XP: To get extended error information, call GetLastError.
Remarks

Note that the handle to the DC can only be used by a single thread at any one time.
For parameters lpszDriver and lpszDevice, call EnumDisplayDevices to obtain valid names for displays.
When you no longer need the DC, call the DeleteDC function.
Windows 2000/XP: If lpszDriver or lpszDevice is DISPLAY, the thread that calls CreateDC owns the HDC that is created. When this thread is destroyed, the HDC is no longer valid. Thus, if you create the HDC and pass it to another thread, then exit the first thread, the second thread will not be able to use the HDC.
ICM: To enable ICM, set the dmICMMethod member of the DEVMODE structure (pointed to by the pInitData parameter) to the appropriate value.
Windows 95/98/Me: CreateDCW is supported by the Microsoft Layer for Unicode. To use this, you must add certain files to your application, as outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems.
Example Code

For an example, see Capturing an Image.
Requirements

  Windows NT/2000/XP/Vista: Included in Windows NT 3.1 and later.
  Windows 95/98/Me: Included in Windows 95 and later.
  Header: Declared in Wingdi.h; include Windows.h.
  Library: Use Gdi32.lib.
  Unicode: Implemented as Unicode and ANSI versions on Windows NT/2000/XP. Also supported by Microsoft Layer for Unicode
================

EnumDisplayDevices


The EnumDisplayDevices function lets you obtain information about the display devices in a system.
BOOL EnumDisplayDevices(  LPCTSTR lpDevice,                // device name  DWORD iDevNum,                   // display device  PDISPLAY_DEVICE lpDisplayDevice, // device information  DWORD dwFlags                    // reserved);
Parameters
lpDevice
[in] Pointer to the device name. If NULL, function returnsinformation for the display adapter(s) on the machine, based on iDevNum. For more information, see Remarks.
iDevNum
[in] Index value that specifies the display device of interest.

The operating system identifies each display device with anindex value. The index values are consecutive integers, starting at0. If a system has three display devices, for example, they arespecified by the index values 0, 1, and 2.

lpDisplayDevice
[out] Pointer to a DISPLAY_DEVICEstructure that receives information about the display devicespecified by iDevNum.

Before calling EnumDisplayDevices, you must initializethe cb member of DISPLAY_DEVICE to the size, inbytes, of DISPLAY_DEVICE.

dwFlags
[in] Set this flag to EDD_GET_DEVICE_INTERFACE_NAME(0x00000001) to retrieve the device interface name for GUID_DEVINTERFACE_MONITOR, which is registered by the driverstack of the monitor. The value is placed in the DeviceID member ofthe DISPLAY_DEVICE structure returned in lpDisplayDevice.
Return Values

If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. The function fails if iDevNum is greater than the largest device index.
Remarks

To query all display devices in the system, call this function in a loop, starting with iDevNum set to 0, and incrementing iDevNum until the function fails. To select all display devices in the desktop, use only the display devices that have the DISPLAY_DEVICE_ATTACHED_TO_DESKTOP flag in the DISPLAY_DEVICE structure.
To get information on the display adapter, call EnumDisplayDevices with lpDevice set to NULL. For example, DISPLAY_DEVICE.DeviceString contains the adapter name.
To obtain information on a display monitor, first call EnumDisplayDevices with lpDevice set to NULL. Then call EnumDisplayDevices with lpDevice set to DISPLAY_DEVICE.DeviceName from the first call to EnumDisplayDevices and with iDevNum set to zero. Then DISPLAY_DEVICE.DeviceString is the monitor name.
If an adapter has multiple monitors, to obtain information on all of them you must repeatedly call EnumDisplayDevices. Each time you call the function, set lpDevice to the adapter name and increment iDevNum. Note thatDISPLAY_DEVICE.DeviceName changes with each call for monitor information, so you must save the adapter name. The function fails when there are no more monitors for the adapter.
Windows 98/Me: EnumDisplayDevicesW is supported by the Microsoft Layer for Unicode. To use this, you must add certain files to your application, as outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems.
Requirements

  Windows NT/2000/XP/Vista: Included in Windows 2000 and later.
  Windows 95/98/Me: Included in Windows 98 and later.
  Header: Declared in Winuser.h; include Windows.h.
  Library: Use User32.lib.
  Unicode: Implemented as Unicode and ANSI versions on Windows 2000/XP. Also supported by Microsoft Layer for Unicode.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值