点机键盘上的Print键即可抓取当前屏幕,然后打开一个绘图软件,新建一个文件然后ctrl+v拷贝一下即可.
解决方案 »
- 获取的网页和浏览器获取的网页内容不一样?
- MFC]怎么把一个对话框的Edit控件参数传递到另一个对话框中
- 如何让对话框菜单漂亮
- 一个关于Kernel32.lib引发的问题
- 用SetWindowsHooKEx加载修改进程IAT的DLL实现OpenProcess拦截,导致Explorer或taskmgr出错???
- 关于CString数组作函数参数的问题。
- 熟悉ActiveX的朋友帮解决一个问题
- what's the reason for the changing var?
- 关于使用IDispatch的奇怪的问题,可能你不曾遇到过
- 哪有Advanced Windows的电子书,英文中文均可
- 老程序员请进 MAKEPOINT 函数怎么没有了
- 安装soft-ice的问题
再根据位图格式生成位图文件,以下给出源代码://=>
#ifndef PRINTBITMAP_H
#define PRINTBITMAP_Hclass CPrintBitmap : public CBitmap
{
public:
DECLARE_DYNAMIC(CPrintBitmap)// Constructors
CPrintBitmap(); void Capture(CRect &rect);
CPalette *GetPalette(){return m_pPalette;};
HANDLE CreateDIB(int *pbmData=NULL);
void Print(CDC *pDC);// Implementation
public:
virtual ~CPrintBitmap();// Attributes
int m_nWidth;
int m_nHeight;
// Operationsprivate:
CPalette *m_pPalette;
};
#endif
//=>
// PrintBtmap.cpp : implementation of the CPrintBitmap class
//#include "stdafx.h"
#include "PrintBtmap.h"/////////////////////////////////////////////////////////////////////////////
// CPrintBitmapIMPLEMENT_DYNAMIC(CPrintBitmap, CBitmap)
CPrintBitmap::CPrintBitmap()
{
m_pPalette=NULL;
}CPrintBitmap::~CPrintBitmap()
{
if (m_pPalette)
{
delete m_pPalette;
}
}void CPrintBitmap::Capture(CRect &rect)
{
// cleanup from last capture
if (m_pPalette)
{
delete m_pPalette;
DeleteObject();
}// save width and height
m_nWidth=rect.Width();
m_nHeight=rect.Height();////////////////////////////////////////
// copy screen image into a bitmap object
//////////////////////////////////////// // create a device context that accesses the whole screen
CDC dcScreen;
dcScreen.CreateDC("DISPLAY", NULL, NULL, NULL); // create an empty bitmap in memory
CDC dcMem;
dcMem.CreateCompatibleDC(&dcScreen);
CreateCompatibleBitmap(&dcScreen, m_nWidth, m_nHeight);
dcMem.SelectObject(this); // copy screen into empty bitmap
dcMem.BitBlt(0,0,m_nWidth,m_nHeight,&dcScreen,rect.left,rect.top,SRCCOPY);// this bitmap is worthless without the current system palette, so...///////////////////////////////////////////
// save system palette in this bitmap's palette
/////////////////////////////////////////// // create an empty logical palette that's big enough to hold all the colors
int nColors = (1 << (dcScreen.GetDeviceCaps(BITSPIXEL) *
dcScreen.GetDeviceCaps(PLANES)));
LOGPALETTE *pLogPal = (LOGPALETTE *)new BYTE[
sizeof(LOGPALETTE) + (nColors * sizeof(PALETTEENTRY))]; // initialize this empty palette's header
pLogPal->palVersion = 0x300;
pLogPal->palNumEntries = nColors; // load this empty palette with the system palette's colors
::GetSystemPaletteEntries(dcScreen.m_hDC, 0, nColors,
(LPPALETTEENTRY)(pLogPal->palPalEntry)); // create the palette with this logical palette
m_pPalette=new CPalette;
m_pPalette->CreatePalette(pLogPal); // clean up
delete []pLogPal;
dcMem.DeleteDC();
dcScreen.DeleteDC();
}
HANDLE CPrintBitmap::CreateDIB(int *pbmData)
{
///////////////////////////////////////////
// create DIB header from our BITMAP header
/////////////////////////////////////////// BITMAPINFOHEADER bi;
memset(&bi, 0, sizeof(bi));
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biPlanes = 1;
bi.biCompression = BI_RGB; // get and store dimensions of bitmap
BITMAP bm;
GetObject(sizeof(bm),(LPSTR)&bm);
bi.biWidth = bm.bmWidth;
bi.biHeight = bm.bmHeight; // get number of bits required per pixel
int bits = bm.bmPlanes * bm.bmBitsPixel;
if (bits <= 1)
bi.biBitCount = 1;
else if (bits <= 4)
bi.biBitCount = 4;
else if (bits <= 8)
bi.biBitCount = 8;
else
bi.biBitCount = 24;
// calculate color table size
int biColorSize=0;
if (bi.biBitCount!=24) biColorSize=(1<<bi.biBitCount);
biColorSize*=sizeof(RGBQUAD); // calculate picture data size
bi.biSizeImage=(DWORD)bm.bmWidth * bi.biBitCount; //bits per row
bi.biSizeImage=(((bi.biSizeImage) + 31) / 32) * 4;//DWORD aligned
bi.biSizeImage*=bm.bmHeight; //bytes required for whole bitmap // return size to caler in case they want to save to file
if (pbmData)
*pbmData=bi.biSize + biColorSize;///////////////////////////////////////////
// get DIB color table and picture data
/////////////////////////////////////////// // allocate a hunk of memory to hold header, color table and picture data
HANDLE hDIB = ::GlobalAlloc(GHND, bi.biSize + biColorSize + bi.biSizeImage); // get a memory pointer to this hunk by locking it
LPBITMAPINFOHEADER lpbi = (LPBITMAPINFOHEADER)::GlobalLock(hDIB); // copy our header structure into hunk
*lpbi=bi; // get a device context and select our bitmap's palette into it
CDC dc;
dc.Attach(::GetDC(NULL));
CPalette *pPal = dc.SelectPalette(m_pPalette,FALSE);
dc.RealizePalette(); // load our memory hunk with the color table and picture data
::GetDIBits(dc.m_hDC, (HBITMAP)m_hObject, 0, (UINT)bi.biHeight, (LPSTR)lpbi +
(WORD)lpbi->biSize + biColorSize, (LPBITMAPINFO)lpbi,
DIB_RGB_COLORS); // clean up
::GlobalUnlock(hDIB);
dc.SelectPalette(pPal,FALSE);
dc.RealizePalette(); // return handle to the DIB
return hDIB;
}void CPrintBitmap::Print(CDC *pDC)
{
// get DIB version of bitmap
int bmData;
HANDLE hDIB = CreateDIB(&bmData); // get memory pointers to the DIB's header and data bits
LPBITMAPINFOHEADER lpDIBHdr = (LPBITMAPINFOHEADER)::GlobalLock(hDIB);
LPSTR lpDIBBits = (LPSTR)lpDIBHdr+bmData; // stretch bitmap to fill printed page with 1/4 inch borders
int cxBorder=pDC->GetDeviceCaps(LOGPIXELSX)/4;
int cyBorder=pDC->GetDeviceCaps(LOGPIXELSY)/4;
int cxPage = pDC->GetDeviceCaps(HORZRES) - (cxBorder*2);
int cyPage=(int)(((double)cxPage/(double)m_nWidth) * (double)m_nHeight); // stretch the bitmap for the best fit on the printed page
pDC->SetStretchBltMode(COLORONCOLOR);
int i=::StretchDIBits(pDC->m_hDC,
cxBorder,cyBorder,cxPage,cyPage, // destination dimensions
0,0,m_nWidth,m_nHeight, // source bitmap dimensions (use all of bitmap)
lpDIBBits, // bitmap picture data
(LPBITMAPINFO)lpDIBHdr, // bitmap header info
DIB_RGB_COLORS, // specify color table has RGB values
SRCCOPY // simple source to destination copy
); // cleanup
::GlobalUnlock(hDIB);
::GlobalFree(hDIB);
return;
}给出的.h和.cpp仅仅用于在内存生成BMP(DIBSection),而后根据内容就可以
写文件存盘了