1. 程式人生 > >截屏當前視窗,並把截到的圖複製到剪貼簿中

截屏當前視窗,並把截到的圖複製到剪貼簿中

/* 

 * hwnd:要截圖的視窗的控制代碼 
 * fileName:要比較的圖片的路徑 
 * offsets:有4個成員的int型陣列,用於設定比較圖片時,上、下、左、右的偏移量 
 * offsets[0]:左 
 * offsets[1]:上 
 * offsets[2]:右 
 * offsets[3]:下 
 */  
bool print_screen(HWND hwnd, const char* fileName)  
{  
    LPCTSTR pFileName = NULL;  
  
    if(sizeof(TCHAR)==sizeof(char))  
    {  
        pFileName=(LPCTSTR)fileName;  
    }  
    else  
    {  
        int length= sizeof(TCHAR)*(strlen(fileName)+1);  
        LPTSTR tcBuffer=new TCHAR[length];  
        memset(tcBuffer,0,length);  
        MultiByteToWideChar(CP_ACP,0,fileName,strlen(fileName),(LPWSTR)tcBuffer,length);  
        pFileName=(LPCTSTR)tcBuffer ;  
    }  
    long t_start = ::GetTickCount();  
    CDC dc;  
    CDC *pDC = &dc;//螢幕DC  
  
        // HWND hwnd = ::GetForegroundWindow(); // 獲得當前活動視窗  
    HDC activeDC = ::GetWindowDC(hwnd);   //獲得要截圖的視窗的hDC  
  
    pDC->Attach(activeDC);//獲取當前活動視窗的DC  
    RECT rect;  
    ::GetWindowRect(hwnd,&rect);//得到視窗的大小  
    int Width = rect.right - rect.left;  
  
    int Height = rect.bottom - rect.top;  
  
    /*cout << "Width:" << Width << endl 
 
    << "Height:" << Height << endl << endl;*/  
  
    CDC memDC;//記憶體DC  
  
    memDC.CreateCompatibleDC(pDC);  
  
    CBitmap memBitmap, *oldmemBitmap;//建立和螢幕相容的bitmap  
  
    memBitmap.CreateCompatibleBitmap(pDC, Width, Height);  
  
    oldmemBitmap = memDC.SelectObject(&memBitmap);//將memBitmap選入記憶體DC  
  
    memDC.BitBlt(0, 0, Width, Height, pDC, 0, 0, SRCCOPY);//複製螢幕影象到記憶體DC  
        //以下程式碼儲存memDC中的點陣圖到檔案  
  
    BITMAP bmp;  
  
    memBitmap.GetBitmap(&bmp);//獲得點陣圖資訊  




if(::OpenClipboard(AfxGetMainWnd()->GetSafeHwnd()))//開啟剪貼簿
{
EmptyClipboard();//清空剪貼簿
SetClipboardData(CF_BITMAP,memBitmap);//將點陣圖資料儲存到剪貼簿上
CloseClipboard();//關閉剪貼簿
}


  
CString strAppPath = CBcfFile::GetAppPath();
strAppPath += "/log/bankTransfer.bmp";


    FILE *fp = fopen(strAppPath, "w+b");  
    BITMAPINFOHEADER bih = {0};//點陣圖資訊頭  
  
    bih.biBitCount = bmp.bmBitsPixel;//每個畫素位元組大小  
  
    bih.biCompression = BI_RGB;  
  
    bih.biHeight = bmp.bmHeight;//高度  
  
    bih.biPlanes = 1;  
  
    bih.biSize = sizeof(BITMAPINFOHEADER);  
  
    bih.biSizeImage = bmp.bmWidthBytes * bmp.bmHeight;//影象資料大小  
  
    bih.biWidth = bmp.bmWidth;//寬度  
  
    BITMAPFILEHEADER bfh = {0};//點陣圖檔案頭  
  
    bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);//到點陣圖資料的偏移量  
  
    bfh.bfSize = bfh.bfOffBits + bmp.bmWidthBytes * bmp.bmHeight;//檔案總的大小  
  
    bfh.bfType = (WORD)0x4d42;  
  
    fwrite(&bfh, 1, sizeof(BITMAPFILEHEADER), fp);//寫入點陣圖檔案頭  
  
    fwrite(&bih, 1, sizeof(BITMAPINFOHEADER), fp);//寫入點陣圖資訊頭  
  
    byte * p = new byte[bmp.bmWidthBytes * bmp.bmHeight];//申請記憶體儲存點陣圖資料  
  
    GetDIBits(memDC.m_hDC, (HBITMAP) memBitmap.m_hObject, 0, Height, p,  
  
        (LPBITMAPINFO) &bih, DIB_RGB_COLORS);//獲取點陣圖資料  
  
    fwrite(p, 1, bmp.bmWidthBytes * bmp.bmHeight, fp);//寫入點陣圖資料  
  
    delete [] p;  
  
    fclose(fp);  
  
        // 以下程式碼用於比較指定的BMP檔案與記憶體中的截圖  
//    CImage img;  
//    img.Load(pFileName);  
//    int nWidth = img.GetWidth();//獲取影象寬度  
//    int nHeight = img.GetHeight();//獲取影象高度  
//    if (Width != nWidth || Height != nHeight)  
//    {  
//        return false;  
//    }  
//    byte* pRealData;  
//    pRealData=(byte*)img.GetBits();  
//  
//    int pit=img.GetPitch();  
//  
//    int bitCount=img.GetBPP()/8;  
//  
//    for (int y = offsets[1]; y < Height - offsets[3]; ++y)  
//    {  
//        for (int x = offsets[0]; x < Width - offsets[2]; ++x)  
//        {  
//            int pity = pit * y;  
//            int pitx = x*bitCount;  
//            if (up[bmp.bmWidth * (bmp.bmHeight - y - 1) + x] !=   
//(*(pRealData + pity + pitx + 2) << 16) + (*(pRealData + pity + pitx + 1) << 8) +   
//*(pRealData + pity + pitx))  
//            {  
//                printf("(%d, %d) = %x, ",x, y, up[bmp.bmWidth *  
// (bmp.bmHeight - y - 1) + x]);  
//                printf("(%d, %d) = %x\n",x, y, (*(pRealData +   
//pity + pitx + 2) << 16) + (*(pRealData + pity + pitx + 1) << 8) + *(pRealData +   
//pity + pitx));  
//                return false;  
//            }  
//        }  
//    }  
    return true;  
}