我想在窗口隐藏时截取屏幕图像,相关代码如下:
HWND hDesktopWnd=::GetDesktopWindow();
HDC hDesktopDC=::GetDC(hDesktopWnd);
此时hDesktopDC不为空,但是调试时,鼠标移到它上面,显示如下:
unsued CXX0030:错误:无法计算表达式的值另外使用::GetDC(NULL);也是一样的结果

解决方案 »

  1.   


    DC不为空,只是显示“无法计算表达式的值”继续运行,使用GetDIBits后再SetDIBits,再BitBlt显示出来,图片大部分是黑的,只有少量白点
      

  2.   

    你可以参考一下这个
    HBITMAP CopyScreenToBitmap(LPRECT lpRect)
    {
    HDC        hScrDC, hMemDC;      
    HBITMAP   hOldBitmap,hBitmap;   
    int        nX, nY, nX2, nY2;      
    int        nWidth, nHeight;       if (IsRectEmpty(lpRect))
    return NULL;
    hScrDC = CreateDC("DISPLAY", NULL, NULL, NULL);
    hMemDC = CreateCompatibleDC(hScrDC);
    nX = lpRect->left;
    nY = lpRect->top;
    nX2 = lpRect->right;
    nY2 = lpRect->bottom;
    xScrn = GetDeviceCaps(hScrDC, HORZRES);
    yScrn = GetDeviceCaps(hScrDC, VERTRES);
    if (nX < 0)    nX = 0;
    if (nY < 0)   nY = 0;
    if (nX2 > xScrn)  nX2 = xScrn;
    if (nY2 > yScrn)  nY2 = yScrn;
    nWidth = nX2 - nX;
    nHeight = nY2 - nY;
    //   创建一个与屏幕设备描述表兼容的位图   
    hBitmap=CreateCompatibleBitmap(hScrDC,nWidth,nHeight);   
    //   把新位图选到内存设备描述表中   
    hOldBitmap=(HBITMAP)SelectObject(hMemDC,hBitmap);   
    //   把屏幕设备描述表拷贝到内存设备描述表中   
    ShowWindow(SW_HIDE); BitBlt(hMemDC,0,0,   nWidth,nHeight,hScrDC,   nX,   nY,   SRCCOPY);   
    //得到屏幕位图的句柄   
    hBitmap=(HBITMAP)SelectObject(hMemDC,hOldBitmap);   
    //清除     
    DeleteDC(hScrDC);   
    DeleteDC(hMemDC);    ShowWindow(SW_SHOW); //   返回位图句柄   
    return   hBitmap;       }
      

  3.   

    就这两句应该是没有问题的,另外GetDC(NULL)也能取到屏幕DC。应该是别的问题
      

  4.   

    最好不要这么创建兼容DC,失败几率很高,也就是你的黑色你既然能获取到桌面的DC,那么通过桌面DC来创建兼容DC还有兼容的bitmap,这样才一定可用不要用DISPLAY来做源
      

  5.   

    给你个屏幕截图的Demo程序void SaveScreen(LPCTSTR lpszFilePath)
    {
    TCHAR szPath[MAX_PATH] = {0};
    if(NULL == lpszFilePath)
    {
    GetModuleFileName(NULL, szPath, MAX_PATH-1);
    LPTSTR lpszPath = _tcschr(szPath, _T('\\'));
    LPTSTR lpszEnd = szPath;
    while(NULL != lpszPath)
    {
    lpszEnd = lpszPath;
    lpszPath = _tcschr(lpszPath+1, _T('\\'));
    }
    int nPos = lpszEnd - szPath + 1;
    szPath[nPos] = _T('\0');
    CString strPath(szPath); LPCTSTR lpszFile = _T("PICTURES");
    strPath += lpszFile;
    if(!PathFileExists(strPath))
    {
    if(CreateDirectory(strPath, NULL))
    {
    return ;
    }
    }
    _stprintf_s(_tcschr(szPath, _T('\0')), _countof(szPath) - _tcslen(szPath), _T("%s\\CS_%05d.bmp"), lpszFile, ++m_nIndex);
    }
    else
    {
    _stprintf_s(szPath, sizeof(szPath), _T("%s"), lpszFilePath);
    } if(0 != _tcslen(szPath))
    {
    CDC* pDC = CDC::FromHandle(::GetDC(NULL));
    int nBitsPixel = pDC->GetDeviceCaps(BITSPIXEL);
    int nWidth = pDC->GetDeviceCaps(HORZRES);
    int nHeight = pDC->GetDeviceCaps(VERTRES); CDC memDC;
    memDC.CreateCompatibleDC(pDC); CBitmap bitmap;
    bitmap.CreateCompatibleBitmap(pDC, nWidth, nHeight); CBitmap* oldBmp = memDC.SelectObject(&bitmap);
    memDC.BitBlt(0, 0, nWidth, nHeight, pDC, 0, 0, SRCCOPY); BITMAP bmp;
    bitmap.GetBitmap(&bmp); try
    {
    CFile file;
    file.Open(szPath, CFile::modeCreate | CFile::typeBinary | CFile::modeWrite);

    BITMAPINFOHEADER bih = {0};
    bih.biSize = sizeof(BITMAPINFOHEADER);
    bih.biBitCount = bmp.bmBitsPixel;
    bih.biCompression = BI_RGB;
    bih.biHeight = bmp.bmHeight;
    bih.biPlanes = 1;
    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;
    file.Write(&bfh, sizeof(BITMAPFILEHEADER));
    file.Write(&bih, sizeof(BITMAPINFOHEADER));
    BYTE* pByte = new BYTE[bmp.bmWidthBytes * bmp.bmHeight];
    GetDIBits(memDC.m_hDC, (HBITMAP)bitmap.m_hObject, 0, nHeight, pByte, (LPBITMAPINFO)&bih, DIB_RGB_COLORS);
    file.Write(pByte, bmp.bmWidthBytes * bmp.bmHeight);
    file.Flush();
    file.Close();
    delete[] pByte;
    }
    catch (CFileException* e)
    {
    e->ReportError();
    e->Delete();
    } memDC.SelectObject(oldBmp);
    bitmap.DeleteObject();
    memDC.DeleteDC();
    }
    }