赞
踩
实现的关键主要有两部分:一是通过句柄后台获得截图,二是通过句柄后台对窗口发送鼠标点击消息或者按键消息
关于通过窗口发送点击消息:
可用如下函数
- SendMessage(hWnd, WM_LBUTTONDOWN, MK_LBUTTON, RightPoint);
- SendMessage(hWnd, WM_LBUTTONUP, NULL, RightPoint);
LPARAM RightPoint = MAKELPARAM(627, 136); //x坐标,y坐标
其中hwnd是窗口句柄,RightPoint是以窗口左上角为(0,0)的坐标,注意按键都是配对的,先按下,再松开。
注意:大多系统需要管理员权限才能获取到截图
另外,貌似联想笔记本win7目前发现无法获取到截图,原因,我也不知道,哈哈!
20181202更新:(后面使用的opencv3,部分Mat的转换方式有所不同)
在 GetRegionMat 函数用于截图的部分,由于内存分配有opencv执行,需要手动释放内存,
其代码如下:
- Mat CWindowsScreenShotDlg::GetRegionMat(HWND hWnd)
- {
-
- extern int m_windowFlag;
- HDC hDC = ::GetWindowDC(hWnd);
- ASSERT(hDC);
- HDC hMemDC = ::CreateCompatibleDC(hDC);
- ASSERT(hMemDC);
- RECT rc;
- ::GetWindowRect(hWnd, &rc);
- BITMAP bitmap = { 0 };
- HBITMAP hBitmap = ::CreateCompatibleBitmap(hDC, rc.right - rc.left, rc.bottom - rc.top);
- ASSERT(hBitmap);
- HBITMAP hOldBmp = (HBITMAP)::SelectObject(hMemDC, hBitmap);
-
- //::BitBlt(hMemDC,0,0,rc.right-rc.left,rc.bottom-rc.top,hDC,0,0,SRCCOPY);
- ::PrintWindow(hWnd, hMemDC, 0);//获取截图
-
- ::GetObject(hBitmap, sizeof(BITMAP), &bitmap);//猜测这个地方,图片已经赋值给bitmap完毕了
-
- /*将bitmap格式图片转换为IplImage,便于处理*/
- int depth = (bitmap.bmBitsPixel == 1) ? IPL_DEPTH_1U : IPL_DEPTH_8U;
- int nChannels = (bitmap.bmBitsPixel == 1) ? 1 : bitmap.bmBitsPixel / 8;
- IplImage* img = cvCreateImage(cvSize(bitmap.bmWidth, bitmap.bmHeight), depth, nChannels);
- BYTE *pBuffer = new BYTE[bitmap.bmHeight*bitmap.bmWidth*nChannels];
- GetBitmapBits(hBitmap, bitmap.bmHeight*bitmap.bmWidth*nChannels, pBuffer);
- memcpy(img->imageData, pBuffer, bitmap.bmHeight*bitmap.bmWidth*nChannels);
- delete[]pBuffer;
- IplImage *dst = cvCreateImage(cvGetSize(img), img->depth, 3);
- cvCvtColor(img, dst, CV_BGRA2BGR);
- cvReleaseImage(&img);
-
- ::SelectObject(hMemDC, hOldBmp);
- ::DeleteObject(hBitmap);
- ::DeleteObject(hMemDC);
- ::ReleaseDC(hWnd, hDC);
-
- if (1 == m_windowFlag)
- {
- Mat returnPic = cvarrToMat(dst, true)(Rect(0, 18, 856, 538));
- cvReleaseImage(&dst);
- return returnPic;
- }
- else
- {
- Mat returnPic = cvarrToMat(dst, true);
- cvReleaseImage(&dst);
- return returnPic;
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。