Review Win32 API and win32api

Source: Internet
Author: User

Review Win32 API and win32api

A friend said that the print window function should be added to a project developed by VC ++ 6.0 to help write code for its call. Of course, I don't want to touch this old IDE, and I don't like the clumsy and unclear encapsulation of MFC. So I decided to use a pure Win32 API and then simply encapsulate it with the C ++ class.

1. Basic Ideas

The window DC and printer DC are two incompatible DC types, so bitmap transfer between them can only pass through DIB. First, copy the customer area of the window to be printed to the DDB memory bitmap through BitBlt (), then convert DDB to DIB through GetDIBits (), and finally use StretchDIBits () output to the printer DC.

2. Code Implementation

Header file WinowPrinter. h

# Pragma once /************************************ **************************************** * ** WindowPrinter: provides the screenshot window and uses the default printer to automatically center and scale the screen. The sample code is as follows. HWND hwnd = this-> GetSafeWnd (); WindowPrinter: PrintWindowClientArea (hwnd ); **************************************** **************************************** /class WindowPrinter {public: windowPrinter ();~ WindowPrinter (); public:/* function: Get the DC of the current default printer. Return: The DC of the printer is returned successfully, and return NULL */static HDC GetPrinterDC ();/* function: print the content of the client area of the window to the printer. The auto-zoom-center printing parameter is: hWnd-handle of the printed window */static void PrintWindowClientArea (HWND hwnd );};


 

Implementation file WindowPrinter. cpp

# Include "stdafx. h" # include "WindowPrinter. h" # include <Winspool. h> WindowPrinter: WindowPrinter () {} WindowPrinter ::~ WindowPrinter () {}/* function: Obtain the DC of the current default printer. Return: The DC of the printer is returned successfully. return NULL */HDC WindowPrinter: GetPrinterDC () {DWORD dwNeeded, dwReturned; HDC hdc;: EnumPrinters (values, NULL, 4, NULL, 0, & dwNeeded, & dwReturned); PRINTER_INFO_4 * pinfo4 = (PRINTER_INFO_4 *) malloc (dwNeeded );:: EnumPrinters (PRINTER_ENUM_LOCAL, NULL, 4, (BYTE *) pinfo4, dwNeeded, & dwNeeded, & dwReturned); hdc =: CreateDC (NULL, pinfo4-> pPrinterName, NULL, NULL); free (pinfo4); return hdc;}/* function: print the content of the client area of the window to the printer, and automatically scale and center. Print parameters: hWnd-handle of the printed window */void WindowPrinter: PrintWindowClientArea (HWND hWnd) {if (hWnd = NULL) return; RECT rectClient;: GetClientRect (hWnd, & rectClient); int width = rectClient. right-rectClient. left; int height = rectClient. bottom-rectClient. top; // copy the user area to the DDB bitmap through the memory dc hdc hdcWnd =: GetDC (hWnd); HBITMAP hbmWnd =: CreateCompatibleBitmap (hdcW Nd, width, height); HDC hdcMem =: CreateCompatibleDC (hdcWnd);: SelectObject (hdcMem, hbmWnd);: BitBlt (hdcMem, 0, 0, width, height, hdcWnd, 0, 0, SRCCOPY); // convert the window DDB into dibbitmap bmp wnd;: GetObject (hbmWnd, sizeof (BITMAP), & bmp wnd); BITMAPINFOHEADER bi; // information header bi. biSize = sizeof (BITMAPINFOHEADER); bi. biWidth = bmp wnd. bmWidth; bi. biHeight = bmp wnd. bmHeight; bi. biPlanes = 1; bi. biBitCount = 32; // 32 bits per pixel Indicates converting bi. biCompression = BI_RGB; bi. biSizeImage = 0; bi. biXPelsPerMeter = 0; bi. biYPelsPerMeter = 0; bi. biClrUsed = 0; bi. biClrImportant = 0; DWORD dwbmp size = (bmp wnd. bmWidth * bi. biBitCount + 31)/32) * 4 * bmp wnd. bmHeight; // each row's pixel bit 32 alignment char * lpbitmap = (char *) malloc (dwbmp size); // pixel bit pointer: GetDIBits (hdcMem, hbmWnd, 0, (UINT) bmp wnd. bmHeight, lpbitmap, (BITMAPINFO *) & bi, DIB_RGB_COLORS);: DeleteDC (hdcMem );:: DeleteObject (hbmWnd);: ReleaseDC (hWnd, hdcWnd); // save as a file (optional) BITMAPFILEHEADER bmfHeader; // File Header DWORD dwSizeofDIB = dwbmp size + sizeof (BITMAPFILEHEADER) + sizeof (BITMAPINFOHEADER); bmfHeader. bfOffBits = (DWORD) sizeof (BITMAPFILEHEADER) + (DWORD) sizeof (BITMAPINFOHEADER); bmfHeader. bfSize = dwSizeofDIB; bmfHeader. bfType = 0x4D42; FILE * fp = NULL;: _ wfopen_s (& fp, L "capture.bmp", L "w");: fwrite (& bmfHeader, siz Eof (BITMAPFILEHEADER), 1, fp); // Write File Header: fwrite (& bi, sizeof (BITMAPINFOHEADER), 1, fp); // write information header :: fwrite (lpbitmap, dwbmp size, 1, fp); // write pixel bit: fclose (fp); fp = NULL; // StretchDIBits () zoom print DIBHDC hdcPrinter = WindowPrinter :: getPrinterDC (); if (hdcPrinter = NULL) return; int pageWidth =: GetDeviceCaps (hdcPrinter, HORZRES); int pageHeight =: GetDeviceCaps (hdcPrinter, VERTRES ); float scaleX = (float) pageW Idth/(float) bmp wnd. bmWidth; float scaleY = (float) pageHeight/(float) bmp wnd. bmHeight; float scale = scaleX <scaleY? ScaleX: scaleY; int xDst, yDst, cxDst, cyDst; cxDst = (int) (float) bmp wnd. bmWidth * scale); cyDst = (int) (float) bmp wnd. bmHeight * scale); xDst = (int) (pageWidth-cxDst)/2); yDst = (int) (pageHeight-cyDst)/2 ); static DOCINFO di = {sizeof (DOCINFO), L "PRINTJOBNAME"}; if (: StartDoc (hdcPrinter, & di)> 0) {if (: StartPage (hdcPrinter)> 0) {: StretchDIBits (hdcPrinter, xDst, yDst, cxDst, cyDst, 0, 0, bmp wnd. bmWidth, bmp wnd. bmHeight, lpbitmap, (BITMAPINFO *) & bi, DIB_RGB_COLORS, SRCCOPY);: EndPage (hdcPrinter); }:: EndDoc (hdcPrinter); }:deletedc (hdcPrinter );:: free (lpbitmap );}


 

 

 

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.