代码在这里:http://download.csdn.net/download/qq_36576377/10182153
#include "stdafx.h"
#include "Myseltest.h"
#include "MyseltestDlg.h"
#include "afxdialogex.h"
using namespace cv;
#include <thread>
#include <math.h>
#define PI 3.1415926
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
void savevideo();
Mat gdiScreenCapture();
bool ifwrite=true;
CString aviname;
HBITMAP hBitmap;
HDC hDDC;
HDC hCDC;
int nWidth;
int nHeight;
CRect re;
void biao(LPVOID a);
void CMyseltestDlg::OnBnClickedTrans()
{
CFileDialog fDlg(FALSE,_T("avi"),NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,_T("*.avi"),this);
if (fDlg.DoModal()==IDOK)
aviname = fDlg.GetPathName();
else
return;
std::thread th(savevideo);
th.detach();
std::thread th2(biao,this);
th2.detach();
GetDlgItem(IDC_Trans)->EnableWindow(0);
GetDlgItem(IDC_stop)->EnableWindow(1);
}
void savevideo()
{
ifwrite=true;
nWidth = GetSystemMetrics(SM_CXSCREEN);//得到屏幕的分辨率的x
nHeight = GetSystemMetrics(SM_CYSCREEN);//得到屏幕分辨率的y
hDDC = GetDC( GetDesktopWindow() );//得到屏幕的dc
hCDC = CreateCompatibleDC(hDDC);//
Mat img=gdiScreenCapture();
VideoWriter vw;
vw.open(aviname.GetBuffer(),CV_FOURCC('M', 'P', '4', '2'),
25,cvSize(img.cols,img.rows));
while(ifwrite)
{
img=gdiScreenCapture();
waitKey(25);
if(!img.data)
return;
vw<<img;
resize(img,img,Size(re.Width(),re.Height()),1);
// imshow("view",img);
}
vw.release();
}
void CMyseltestDlg::OnBnClickedstop()
{
ifwrite=false;
GetDlgItem(IDC_Trans)->EnableWindow(1);
GetDlgItem(IDC_stop)->EnableWindow(0);
}
Mat gdiScreenCapture(){
hBitmap =CreateCompatibleBitmap(hDDC,nWidth,nHeight);//得到位图
SelectObject(hCDC,hBitmap); //好像总得这么写。
BitBlt(hCDC,0,0,nWidth,nHeight,hDDC,0,0,SRCCOPY);
Mat dst;
dst.create(cvSize(nWidth,nHeight),CV_8UC4);
GetBitmapBits(hBitmap,nWidth*nHeight*4,dst.data);
cvtColor(dst,dst,CV_BGRA2BGR);
return dst;
}
void CMyseltestDlg::OnHotKey(UINT nHotKeyId, UINT nKey1, UINT nKey2)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
this->ShowWindow(SW_SHOW);
biao(this);
UnregisterHotKey(m_hWnd ,1);
CDialogEx::OnHotKey(nHotKeyId, nKey1, nKey2);
}
void biao(LPVOID a)
{
CMyseltestDlg *c=(CMyseltestDlg *)a;
CClientDC dc(c); //获取绘图DC
dc.LineTo(100,100);
// 创建内存缓存DC
CDC memDC;
memDC.CreateCompatibleDC(&dc);
CBitmap bufferMemBitmap;
CRect rect1;
CBrush whiteBrush;
whiteBrush.CreateSolidBrush(RGB(255, 255, 0));
::GetClientRect(GetDlgItem(c->GetSafeHwnd(),IDC_STATIC),rect1);
//::GetClientRect(::GetDlgItem(AfxGetMainWnd()->m_hWnd,IDC_STATIC),rect);
bufferMemBitmap.CreateCompatibleBitmap(&dc, rect1.Width(),rect1.Height());
memDC.SelectObject(&bufferMemBitmap);
memDC.MoveTo(rect1.Width()/2,rect1.Height()/2);
double x(0);double y(0);
memDC.SetBkMode(0);
for (int i=0;i<=360;i+=8)
{
x=sin((double)i*PI/180)*100+rect1.Width()/2;
y=-cos((double)i*PI/180)*100+rect1.Height()/2;
memDC.FillRect(CRect(0, 0, rect1.Width(), rect1.Height()),&whiteBrush);
for(int j=0;j<9;j++)
{
if(i==40*j)
{
if(j==0)
memDC.TextOutA(x,y,"失意");
else if(j==1)
memDC.TextOutA(x,y,"挫折");
else if(j==2)
memDC.TextOutA(x,y,"困境");
else if(j==3)
memDC.TextOutA(x,y,"沮丧");
else if(j==4)
memDC.TextOutA(x,y,"无助");
else if(j==5)
memDC.TextOutA(x,y,"迷茫");
else if(j==6)
memDC.TextOutA(x,y,"悲观");
else if(j==7)
memDC.TextOutA(x,y,"绝望");
else if(j==8)
memDC.TextOutA(x,y,"困难");
else if(j==9)
memDC.TextOutA(x,y,"....");
}
}
memDC.LineTo(x,y);
memDC.TextOutA(rect1.Width()/2-10,rect1.Height()/2-10,"2017");
memDC.MoveTo(rect1.Width()/2,rect1.Height()/2);
dc.BitBlt(rect1.left, rect1.top, rect1.Width(), rect1.Height(),&memDC, 0, 0, SRCCOPY);
Sleep(500);
}
memDC.FillRect(CRect(0, 0, rect1.Width(), rect1.Height()),&whiteBrush);
memDC.TextOutA(rect1.Width()/2-10,rect1.Height()/2-30,"2018 Is Comming...");
dc.BitBlt(rect1.left, rect1.top, rect1.Width(), rect1.Height(),&memDC, 0, 0, SRCCOPY);
bufferMemBitmap.DeleteObject();
memDC.DeleteDC();
}