重溫 Win32 API,重溫win32api

來源:互聯網
上載者:User

重溫 Win32 API,重溫win32api

朋友說在一個VC++6.0開發的項目中要加入列印視窗的功能,讓幫忙寫個代碼供其調用。這麼老的IDE當然不想碰了,而且也不喜歡MFC笨拙不清晰的封裝,所以決定採用純Win32 API,然後用C++類簡單封裝一下。

1 基本思路

視窗DC和印表機DC是兩類不相容的DC,所以它們之間傳送位元影像只能通過DIB。首先,通過BitBlt()把要列印視窗的客戶區拷貝到DDB記憶體位元影像中,然後通過GetDIBits()把DDB轉換為DIB,最後通過StretchDIBits()向印表機DC輸出。

2 代碼實現

標頭檔 WinowPrinter.h

#pragma once/********************************************************************************                         WindowPrinter 列印視窗類別功能描述:提供截屏視窗並通過預設印表機,自動進行置中縮放列印使用說明:範例代碼如下。 HWND hwnd = this->GetSafeWnd();WindowPrinter::PrintWindowClientArea(hwnd);********************************************************************************/class WindowPrinter{public:WindowPrinter();~WindowPrinter();public:/*功能:擷取當前預設印表機的DC返回:成功返回印表機的DC,失敗返回NULL*/static HDC GetPrinterDC();/*功能:列印視窗客戶區內容到印表機,自動縮放置中列印參數: hWnd-被列印視窗的控制代碼*/static void PrintWindowClientArea(HWND hwnd);};


 

實現檔案 WindowPrinter.cpp

#include "stdafx.h"#include "WindowPrinter.h"#include <Winspool.h>WindowPrinter::WindowPrinter(){}WindowPrinter::~WindowPrinter(){}/*功能:擷取當前預設印表機的DC返回:成功返回印表機的DC,失敗返回NULL*/HDC WindowPrinter::GetPrinterDC(){DWORD dwNeeded, dwReturned;HDC hdc;::EnumPrinters(PRINTER_ENUM_LOCAL, 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;}/*功能:列印視窗客戶區內容到印表機,自動縮放置中列印參數: hWnd-被列印視窗的控制代碼*/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;// 通過記憶體DC複製客戶區到DDB位元影像HDC hdcWnd = ::GetDC(hWnd);HBITMAP hbmWnd = ::CreateCompatibleBitmap(hdcWnd, width, height);HDC hdcMem = ::CreateCompatibleDC(hdcWnd);::SelectObject(hdcMem, hbmWnd);::BitBlt(hdcMem, 0, 0, width, height, hdcWnd, 0, 0, SRCCOPY);// 把視窗DDB轉為DIBBITMAP bmpWnd;::GetObject(hbmWnd, sizeof(BITMAP), &bmpWnd);BITMAPINFOHEADER bi; // 資訊頭bi.biSize = sizeof(BITMAPINFOHEADER);bi.biWidth = bmpWnd.bmWidth;bi.biHeight = bmpWnd.bmHeight;bi.biPlanes = 1;bi.biBitCount = 32; // 按照每個像素用32bits表示轉換bi.biCompression = BI_RGB;bi.biSizeImage = 0;bi.biXPelsPerMeter = 0;bi.biYPelsPerMeter = 0;bi.biClrUsed = 0;bi.biClrImportant = 0;DWORD dwBmpSize = ((bmpWnd.bmWidth * bi.biBitCount + 31) / 32) * 4 * bmpWnd.bmHeight; // 每一行像素位32對齊char *lpbitmap = (char*)malloc(dwBmpSize); // 像素位指標::GetDIBits(hdcMem, hbmWnd, 0, (UINT)bmpWnd.bmHeight,lpbitmap,(BITMAPINFO*)&bi,DIB_RGB_COLORS);::DeleteDC(hdcMem);::DeleteObject(hbmWnd);::ReleaseDC(hWnd, hdcWnd);// 存為檔案(可選)BITMAPFILEHEADER bmfHeader; // 檔案頭DWORD dwSizeofDIB = dwBmpSize + 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, sizeof(BITMAPFILEHEADER), 1, fp); // 寫入檔案頭::fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, fp);        // 寫入資訊頭::fwrite(lpbitmap, dwBmpSize, 1, fp);                  // 寫入像素位::fclose(fp);fp = NULL;// StretchDIBits()縮放列印DIBHDC hdcPrinter = WindowPrinter::GetPrinterDC();if (hdcPrinter == NULL)return;int pageWidth = ::GetDeviceCaps(hdcPrinter, HORZRES);int pageHeight = ::GetDeviceCaps(hdcPrinter, VERTRES);float scaleX = (float)pageWidth / (float)bmpWnd.bmWidth;float scaleY = (float)pageHeight / (float)bmpWnd.bmHeight;float scale = scaleX < scaleY ? scaleX : scaleY;int xDst, yDst, cxDst, cyDst;cxDst = (int)((float)bmpWnd.bmWidth * scale);cyDst = (int)((float)bmpWnd.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, bmpWnd.bmWidth, bmpWnd.bmHeight,lpbitmap,(BITMAPINFO*)&bi,DIB_RGB_COLORS,SRCCOPY);::EndPage(hdcPrinter);}::EndDoc(hdcPrinter);}::DeleteDC(hdcPrinter);::free(lpbitmap);}


 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.