这个就是QQ里截取屏幕的功能实现代码,很简单,完整代码如下。 在ScreenCaptureDlg.cpp中声明2个全局变量: - HWND hwndCapture;
- CRect rectCapture;
相关代码: - void CScreenCaptureDlg::onMouseMove(UINT nFlags, CPoint point)
- {
-
- if(nFlags==MK_LBUTTON)
- {
-
- ShowWindow(SW_HIDE);
-
- HCURSOR cur=LoadCursor(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDC_CAMERA));
- SetCursor(cur);
- SetCapture();
-
- ClientToScreen(&point);
- hwndCapture=(HWND)::WindowFromPoint(point);
-
- HDC hDC=::GetDC(NULL);
-
- HPEN hPen =CreatePen(PS_INSIDEFRAME,6, RGB(255,0,0));
-
- int nMode =SetROP2(hDC, R2_NOTXORPEN);
- HPEN hpenOld= (HPEN)SelectObject(hDC, hPen);
-
- ::GetWindowRect(hwndCapture,&rectCapture);
-
- POINT pt[5];
- pt[0] = CPoint(rectCapture.left, rectCapture.top);
- pt[1] = CPoint(rectCapture.right,rectCapture.top);
- pt[2] = CPoint(rectCapture.right,rectCapture.bottom);
- pt[3] = CPoint(rectCapture.left, rectCapture.bottom);
- pt[4] = CPoint(rectCapture.left, rectCapture.top);
-
- ::Polyline(hDC,pt,5);
-
- Sleep(100);
- ::Polyline(hDC,pt,5);
- ::SelectObject(hDC,hpenOld);
- ::ReleaseDC(NULL,hDC);
-
- }
-
- CDialog::onMouseMove(nFlags, point);
- }
-
- void CScreenCaptureDlg::OnLButtonUp(UINT nFlags, CPoint point)
- {
-
- int nWidth=rectCapture.Width();
- int nHeight=rectCapture.Height();
- HDC hdcScreen, hMemDC;
- HBITMAP hBitmap, hOldBitmap;
-
- hdcScreen = CreateDC("DISPLAY", NULL, NULL, NULL);
- hMemDC = CreateCompatibleDC(hdcScreen);
-
- hBitmap = CreateCompatibleBitmap(hdcScreen, nWidth, nHeight);
-
- hOldBitmap =(HBITMAP)SelectObject(hMemDC, hBitmap);
-
- BitBlt(hMemDC, 0, 0, nWidth, nHeight, hdcScreen,rectCapture.left
- ,rectCapture.top,SRCCOPY);
-
- hBitmap =(HBITMAP)SelectObject(hMemDC, hOldBitmap);
- DeleteDC(hdcScreen);
- DeleteDC(hMemDC);
-
-
-
- OpenClipboard() ;
- EmptyClipboard();
- SetClipboardData(CF_BITMAP, hBitmap);
-
- CloseClipboard();
-
- MessageBox("屏幕内容已经拷到剪贴板上!");
-
- ReleaseCapture();
-
- ShowWindow(SW_NORMAL);
|