码迷,mamicode.com
首页 > 其他好文 > 详细

屏幕截图(带光标)

时间:2015-09-17 11:27:23      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

原文链接地址:http://blog.csdn.net/VisualEleven/article/details/6093484

// 屏幕截图程序,可运行PC,WinCE,Windows Mobile平台上

void SaveScreenToFile(LPCTSTR szFileName)
{
    HDC hScrDC = ::GetDC(NULL);
    HDC hMemDC = NULL;
    
    BYTE *lpBitmapBits = NULL; 
    
    int nWidth = GetSystemMetrics(SM_CXSCREEN);
    int nHeight = GetSystemMetrics(SM_CYSCREEN); 
    
    hMemDC = ::CreateCompatibleDC(hScrDC); 
    
    BITMAPINFO bi; 
    ZeroMemory(&bi, sizeof(BITMAPINFO));
    bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bi.bmiHeader.biWidth = nWidth;
    bi.bmiHeader.biHeight = nHeight;
    bi.bmiHeader.biPlanes = 1;
    bi.bmiHeader.biBitCount = 24;

    HCURSOR hCursor = GetCursor();
    POINT ptCursor;
    GetCursorPos(&ptCursor);
    ICONINFO IconInfo = {0};
    if(GetIconInfo(hCursor, &IconInfo))
    {
        ptCursor.x -= IconInfo.xHotspot;
        ptCursor.y -= IconInfo.yHotspot;
        if(NULL != IconInfo.hbmMask)
            DeleteObject(IconInfo.hbmMask);
        if(NULL != IconInfo.hbmColor)
            DeleteObject(IconInfo.hbmColor);
    }
    
    
    HBITMAP bitmap = ::CreateDIBSection(hMemDC, &bi, DIB_RGB_COLORS, (LPVOID*)&lpBitmapBits, NULL, 0);
    HGDIOBJ oldbmp = ::SelectObject(hMemDC, bitmap); 
    
    ::BitBlt(hMemDC, 0, 0, nWidth, nHeight, hScrDC, 0, 0, SRCCOPY);
    DrawIconEx(hMemDC, ptCursor.x, ptCursor.y, hCursor, 0, 0, 0, NULL, DI_NORMAL | DI_COMPAT);

    BITMAPFILEHEADER bh;
    ZeroMemory(&bh, sizeof(BITMAPFILEHEADER));
    bh.bfType = 0x4d42;  //bitmap  
    bh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
    bh.bfSize = bh.bfOffBits + ((nWidth*nHeight)*3);
    
    CFile file;
    if(file.Open(szFileName, CFile::modeCreate | CFile::modeWrite))
    {  
        file.Write(&bh, sizeof(BITMAPFILEHEADER));
        file.Write(&(bi.bmiHeader), sizeof(BITMAPINFOHEADER));
        file.Write(lpBitmapBits, 3 * nWidth * nHeight);
        file.Close();
    }
    
    ::SelectObject(hMemDC, oldbmp);
    ::DeleteObject(bitmap);
    ::DeleteObject(hMemDC);
    ::ReleaseDC(NULL, hScrDC);
}
BOOL DrawIconEx(          HDC hdc,
    int xLeft,
    int yTop,
    HICON hIcon,
    int cxWidth,
    int cyWidth,
    UINT istepIfAniCur,
    HBRUSH hbrFlickerFreeDraw,
    UINT diFlags
);
//The DrawIconEx function draws an icon or cursor into the specified device context, performing the specified raster operations, and stretching or compressing the icon or cursor as specified.

屏幕截图(带光标)

标签:

原文地址:http://www.cnblogs.com/wuyuan2011woaini/p/4815587.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!