【Windows编程】实时捕捉屏幕

来源:互联网 发布:重置网络命令win10 编辑:程序博客网 时间:2024/06/05 00:26

实时捕捉屏幕

主要分为三个部分

  • 获取屏幕
// 两个函数都可以进行位图复制,不过StretchBlt可以按照位图进行拉伸或者压缩BitBlt(HDC hdc,int x,int y,int cx,int cy,HDC hdcSrc,int x1,int y1,DWORD rop);或者StretchBlt(HDC hdcDest,int xDest,int yDest,int wDest,int hDest,HDC hdcSrc,int xSrc,int ySrc,int wSrc,int hSrc,DWORD rop);
  • 输出到客户端上
  • 实时刷新

代码

// 输出到屏幕case WM_PAINT:{     PAINTSTRUCT ps;     HDC hdc = BeginPaint(hWnd, &ps);// 获得客户端的Rect     RECT rectClient;     GetClientRect(hWnd, &rectClient);// 获得屏幕窗口句柄    HWND hwnd = GetDesktopWindow();    HDC hdcScreen = GetDC(hwnd);// 获得屏幕的Rect    RECT rectScreen;    GetWindowRect(hwnd, &rectScreen);// 输出    StretchBlt(hdc, 0, 0, rectClient.right, rectClient.bottom, hdcScreen, 0, 0,     rectScreen.right, rectScreen.bottom, SRCCOPY);//这个也可以//BitBlt(hdc, 0, 0, rectClient.right, rectClient.bottom, hdcScreen,0, 0, SRCCOPY);     EndPaint(hWnd, &ps);}// 实时刷新case WM_CREATE:    SetTimer(hWnd, 1, 100, NULL); // 设置定时器的刷新时间    return 0;case WM_TIMER:{    InvalidateRect(hWnd, NULL, TRUE); //刷新窗口用}

这里写图片描述

不过真正运行起来后,我们会发现这个是一闪闪亮晶晶的捕捉屏幕。
解决方法很简单

BOOL InvalidateRect(HWND hWnd, // handle of window with changed update regionCONST RECT *lpRect, // address of rectangle coordinatesBOOL bErase // erase-background flag);

bErase:指出无效矩形被标记为有效后,是否重画该区域,重画时用预先定义好的画刷。当指定TRUE时需要重画。
我们将最后一项改为FALSE即可

    InvalidateRect(hWnd, NULL, FALSE); //刷新窗口用

这里写图片描述
不要看太久,会眼晕

0 0
原创粉丝点击