ATL COM components, save the specified DC handle as BMP

xiaoxiao2021-03-06  90

Used to save the specified DC as a BMP bitmap, which is suitable for the dynamic program of exposure to its own surface capture. // HDCTOBMPX.H: CHDCTOBMPX declaration

#pragma overce # include "resource.h" // master symbol

// IhdctobmpX [object, uuid ( "7C3141C9-A96E-4775-93C7-FEE42C738B94"), dual, helpstring ( "IhdctobmpX Interface"), pointer_default (unique)] __ interface IhdctobmpX: IDispatch {[propput, id (1), helpstring ("Attribute HWnd")] HRESULT HWND ([in] Ulong NewVal; [PropPut, ID (2), Helpstring ("Properties SavePath")] HRESULT SavePath ([In] BSTR newVal); [ID (3), Helpstring ("Method Savetobmp")] HRESULT Savetobmp (Void); [PropPut, ID (4), Helpstring ("Attribute BWidth")] HRESULT BWIDTH ([In] Double NewVal); [PropPut, ID (5), Helpstring (" Attribute BHEIGHT ")] HRESULT BHEIGHT ([IN] double newval); [ID (6), Helpstring (" Method SavetobMP2 ")] HRESULT SAVETOBMP2 (VOID);

// CHDCTOBMPX

[Coclass, Threading ("Apartment"), VI_Progid ("HDCTOBMP.HDCTOBMPX"), ProgID ("HDCTOBMP.HDCTOBMPX.1"), Version (1.0), UUID ("F4A2F19D-AF6B-434A-A104-C4AA013E7B37", Helpstring ("HDCTOBMPX CLASS"]

Class ATL_NO_VTABLE CHDCTOBMPX: Public IHDCTOBMPX {

protected:

// Save the path _BSTR_T m_path; // Incompute the handle hWnd m_hwnd;

// bit map wide Double M_Width; // bit map high double m_height;

PUBLIC: // Construct CHDCTOBMPX () {}

DECLARE_PROTECT_FINAL_CONSTRUCT () HRESULT FINALCONSTRUCT () {RETURN S_OK;} // Argument Void FinalRelease () {}

PUBLIC:

// Attribute Sets StdMethod (Ulong NewVal); stdmethod (BSTR NewVal);

// Copy bitmap STDMETHOD (Savetobmp) (Void);

STDMETHOD (Double NewVal); STDMETHOD (PUT_BHEIGHT) (Double NewVal)

private: PBITMAPINFO ChdctobmpX :: CreateBitmapInfoStruct (HWND hwnd, HBITMAP hBmp); void CreateBMPFile (HWND hwnd, LPTSTR pszFile, PBITMAPINFO pbi, HBITMAP hBMP, HDC hDC); void Error (TCHAR * pText);

PUBLIC: stdmethod (savetobmp2) (void);}; ---------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------- ----------------- // HDCTOBMPX.CPP: CHDCTOBMPX Implementation #include "stdafx.h" #include "hdctobmpx.h"

// CHDCTOBMPX

// Save BmpStdMethodimp chDCTOBMPX :: Savetobmp (void) {// Setting image save location plus .bmp extension tchar m_shortname [max_path];

WSPrintf (m_shortname, text ("% s.bmp / ​​0"), (lpctstr) m_path;

// Get the input current DC HDC HDC = getdc (this-> m_hwnd); // Create a complete DC HDC HDCMP = CreateCompAtiBEDC (HDC) by current DC (HDC); // Create a bitmap DC HbitMap HBMP = CreateCompAMPATMAP (HDC) , m_width, m_height); if (HBMP == 0) {RETURN 0;}

// Complete DC Select Bits DC IF (! SelectObject (HDCCMP, HBMP) {Return 0;}

// Current DC copies to complete DC Bitblt (HDCCMP, 0, 0, M_Width, M_HEight, HDC, 0, 0, SRCCPY);

PbitmapInfo PBI = CreateBitmapInfostruct (this-> m_hwnd, hbmp);

CreateBMPFile (this-> m_hwnd, m_shortname, pbi, hbmp, hdcmp);

Return S_OK;

STDMETHODIMP chDCTOBMPX :: SaveTobMP2 (Void) {// Settings Image Save Position plus .bmp extension tchar m_shortname [max_path];

WSPrintf (m_shortname, text ("% s.bmp / ​​0"), (lpctstr) m_path;

// Get the input current DC HDC HDC = getWindowDC (this-> m_hwnd); // Create a complete DC HDC HDCMP = CreateCompAmPATIPLEDC (HDC) by the current DC; // Creating a bitmap DC HbitMap HBMP = CreateCompAMPATMAP (HDC) , m_width, m_height); if (HBMP == 0) {RETURN 0;}

// Complete DC Select Bits DC IF (! SelectObject (HDCCMP, HBMP) {Return 0;}

// Current DC copies to complete DC Bitblt (HDCCMP, 0, 0, M_Width, M_HEight, HDC, 0, 0, SRCCPY);

PbitmapInfo PBI = CreateBitmapInfostruct (this-> m_hwnd, hbmp);

CreateBMPFile (this-> m_hwnd, m_shortname, pbi, hbmp, hdcmp);

Return S_OK;

}

/ Attribute /// STDMETHODIMP ChdctobmpX :: put_bwidth (DOUBLE newVal) {// TODO: Add this implementation code this-> m_Width = newVal; return S_OK;} STDMETHODIMP ChdctobmpX :: put_bheight (DOUBLE newVal) {// TODO: in This adds the implementation code this-> m_height = newval; return s_ok;}

STDMETHODIMP ChdctobmpX :: put_savepath (BSTR newVal) {// TODO: Add this implementation code this-> m_path = newVal; SysFreeString (newVal); return S_OK;} STDMETHODIMP ChdctobmpX :: put_HWND (ULONG newVal) {this-> m_hwnd = (Hwnd) newval; return s_ok;}

Private member // Get bitmap information PbitmapInfo ChDctobmpx :: CreateBitmapinfostruct (hwnd hwnd, hbitmap hbmp) {Bitmap BMP; PbitmapInfo PBMI; Word CclRbits;

// Retrieve The Bitmap Color Format, Width, And Height. IF (! GetObject (HBMP, SIZEOF (Bitmap), (LPSTR) & BMP) Error ("getObject");

// Convert The Color Format to a count of bits. Cclrbits = (word) (bmp.bmplanes * bmp.bmbitspixel); if (cclrbits == 1) cclrbits = 1; Else IF (cclrbits <= 4) cclrbits = 4; ELSE IF (CClrbits <= 8) cclrbits = 8; ELSE IF (CClrbits <= 16) cclrbits = 16; Else IF (CClrbits <= 24) cclrbits = 24; ELSE CCLRBITS = 32;

// Allocate Memory for the BitmapInfo Structure. (This Structure // Contains a BitmapInfoHeader structure and an array of rgbquad // data structures.)

IF (cclrbits! = 24) PBMI = (PbitmapInfo) Localalloc (LPTR, SIZEOF (BitmapInfoHeader) Sizeof (RGBQUAD) * (1 << cclrbits));

// there is no rgbquad array for the 24-bit-per-pixel format.

ELSE PBMI = (PbitmapInfo) Localalloc (LPTR, SIZEOF (BitmapInfoHead);

// Initialize the fields in the BITMAPINFO structure.pbmi-> bmiHeader.biSize = sizeof (BITMAPINFOHEADER); pbmi-> bmiHeader.biWidth = bmp.bmWidth; pbmi-> bmiHeader.biHeight = bmp.bmHeight; pbmi-> bmiHeader.biPlanes = bmp.bmplanes; pbmi-> bmiheader.bibitcount = bmp.bmbitspixel; if (cclrbits <24) PBMI-> bmiheader.birrused = (1 << cclrbits);

// if the bitmap is not compressed, set the bi_rgb flag. Pbmi-> bmiheader.biCompression = bi_rgb;

// Compute the number of bytes in the array of color // indices and store the result in biSizeImage. // For Windows NT, the width must be DWORD aligned unless // the bitmap is RLE compressed. This example shows this. // For Windows 95/98 / ME, The Width Must Be Word Aligned Unless. Pbmi-> Bmiheader.bisizeImage = ((PBMI-> Bmiheader.biwidth * CClrbits 31) & ~ 31) / 8 * PBMI-> bmiheader.biheight; // set biclrimportant to 0, Indicating That All of the // device colors area important. pbmi-> bmiheader.birlrimportant = 0; Return PBMI;

// write BMP files void ChdctobmpX :: CreateBMPFile (HWND hwnd, LPTSTR pszFile, PBITMAPINFO pbi, HBITMAP hBMP, HDC hDC) {HANDLE hf; // file handle BITMAPFILEHEADER hdr; // bitmap file-header PBITMAPINFOHEADER pbih; // bitmap info -Header LPBYTE LPBITS; // Memory Pointer DWORD DWTOTAL; // Total Count of Bytes DWORD CB; // Incremental Count Of Bytes Byte * Hp; // Byte Pointer DWORD DWTMP

PBIH = (PbitmapInfoHeader) PBI; lpbits = (lpbyte) Globalalloc (GMEM_FIXED, PBIH-> BisizeImage); if (! lpbits) error ("GlobalAlloc");

// RETRIEVE The Color Table (RGBQUAD Array) and the bits // ("GetDibits (HDC, HBMP, 0, (Word) PBIH-> Biheight, LPBITS, PBI, DIB_RGB_COLORS ) {Error ("getDibits");

// Create the .BMP file hf = CreateFile (pszFile, GENERIC_READ | GENERIC_WRITE, (DWORD) 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, (HANDLE) NULL);. If (hf == INVALID_HANDLE_VALUE) Error ( "CreateFile"); hdr .BFTYPE = 0x4d42; // 0x42 = "b" 0x4d = "m" // compute the size = (dword) (sizeof (bitmapfileHeader) PBIH-> Bisize PBIH-> biclrused * SizeOf (RGBQUAD) PBIH-> BisizeImage); HDr.bfreserved1 = 0; HDr.bfreserved2 = 0;

// compute the offset to the array of color indices. Hdr.bfoffbits = (dword) Sizeof (BitmapFileHeader) PBIH-> Bisize PBIH-> BICLRUSED * SIZEOF (RGBQUAD);

// copy the bitmapfileHeader Into the .bmp file. If (! Writefile (HF, (LPVOID) & HDR, SizeOf (BitmapfileHeader), (LPDWORD) & DWTMP, NULL) {Error ("Writefile");}

// Copy the BITMAPINFOHEADER and RGBQUAD array into the file. If (! WriteFile (hf, (LPVOID) pbih, sizeof (BITMAPINFOHEADER) pbih-> biClrUsed * sizeof (RGBQUAD), (LPDWORD) & dwTmp, (NULL))) { Error ("Writefile");} // Copy The Array of Color INDICES INTO The .BMP file. Dwtotal = cb = pbih-> bisizeImage; hp = lbits; if (! Writefile (HF, (LPSTR) HP, (INT) CB, (LPDWORD) & dWTMP, NULL) Error ("Writefile");

// close the .bmp file. If (! CloseHandle (HF)) Error ("closehandle");

// free memory. Globalfree (hglobal);}

// Error Display Void ChDCTOBMPX :: Error (Tchar * Ptext) {:: MessageBox (Null, PText, Text ("Error!"), MB_OK | MB_TASKMODAL | MB_SETFOREGROUND);}

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

New Post(0)