//捕获桌面屏幕 BOOL CFunction::ScreenCapture() { // 获取桌面窗口句柄 HWND hDesktopWnd = ::GetDesktopWindow(); if (hDesktopWnd == NULL) { return FALSE; } // 获取桌面窗口DC HDC hdc = ::GetDC(hDesktopWnd); if (hdc == NULL) { return FALSE; } // 创建兼容DC HDC mdc = ::CreateCompatibleDC(hdc); if (mdc == NULL) { return FALSE; } // 获取计算机屏幕的宽和高 DWORD dwScreenWidth = ::GetSystemMetrics(SM_CXSCREEN); if (dwScreenWidth == NULL) { return FALSE; } DWORD dwScreenHeight = ::GetSystemMetrics(SM_CYSCREEN); if (dwScreenHeight == NULL) { return FALSE; } // 创建兼容位图 HBITMAP bmp = ::CreateCompatibleBitmap(hdc, dwScreenWidth, dwScreenHeight); if (bmp == NULL) { return FALSE; } // 选中位图 HBITMAP holdbmp = (HBITMAP)::SelectObject(mdc, bmp); if (holdbmp == NULL) { return FALSE; } // 将窗口内容绘制到位图上 ::BitBlt(mdc, 0, 0, dwScreenWidth, dwScreenHeight, hdc, 0, 0, SRCCOPY); // 绘制鼠标 PaintMouse(mdc); // 保存为图片 SaveBmp(bmp); return TRUE; } //绘制鼠标 BOOL CFunction::PaintMouse(HDC hdc) { // 创建兼容DC HDC bufdc = ::CreateCompatibleDC(hdc); //获取光标信息 CURSORINFO cursorInfo = { sizeof(CURSORINFO) }; ::GetCursorInfo(&cursorInfo); // 获取光标的图标信息 ICONINFO iconInfo = { 0 }; ::GetIconInfo(cursorInfo.hCursor, &iconInfo); // 绘制白底黑鼠标(AND) ::SelectObject(bufdc, iconInfo.hbmMask); ::BitBlt(hdc, cursorInfo.ptScreenPos.x, cursorInfo.ptScreenPos.y, 20, 20, bufdc, 0, 0, SRCAND); // 绘制黑底彩色鼠标(OR) ::SelectObject(bufdc, iconInfo.hbmColor); ::BitBlt(hdc, cursorInfo.ptScreenPos.x, cursorInfo.ptScreenPos.y, 20, 20, bufdc, 0, 0, SRCPAINT); // 释放资源 DeleteObject(iconInfo.hbmColor); DeleteObject(iconInfo.hbmMask); DeleteDC(bufdc); return TRUE; } //保存为图片 BOOL CFunction::SaveBmp(HBITMAP hBmp) { CImage image; // 附加位图句柄 image.Attach(hBmp); // 保存成JPG格式图片 image.Save(_T("mybmp1.jpg")); return TRUE; }