19,472
社区成员




class Intercept
{
public:
//初始化 传入窗口句柄
Intercept(HWND hwnd1,LPCTSTR path):m_hSrcWnd(hwnd1),filepath(path)
{
}
//根据句柄获得窗口DC
HDC GetWindowsDC(HWND hwnd)
{
return ::GetDC(hwnd);
}
//获得源窗口的宽
void GetWindowsWidth()
{
::RECT rect;
GetWindowRect(m_hSrcWnd,&rect);
m_nCxWnd = rect.right - rect.left;
}
//获得源窗口的高
void GetWindowHeigh()
{
::RECT rect;
::GetWindowRect(m_hSrcWnd,&rect);
m_nCyWnd = rect.bottom - rect.top;
}
//将源窗口画到目标窗口
BOOL BitBltPicture()
{
CString m_str(filepath);m_str+=FileName;filepath = (LPCTSTR)m_str;
m_hScrDC = GetWindowsDC(GetDesktopWindow()/*m_hSrcWnd*/);
GetWindowsWidth();
GetWindowHeigh();
//c创建位图
if(!img.Create(m_nCxWnd,m_nCyWnd,32))
return -1;
m_hdcMem = ::CreateCompatibleDC(m_hScrDC);
m_hBitmap = ::CreateCompatibleBitmap(m_hScrDC,m_nCxWnd,m_nCyWnd);
::SelectObject(m_hdcMem,m_hBitmap);
::BitBlt(m_hdcMem,0,0,m_nCxWnd,m_nCyWnd,m_hScrDC,0,0,SRCCOPY);
::BitBlt(img.GetDC(),0,0,m_nCxWnd,m_nCyWnd,m_hdcMem,0,0,SRCCOPY);
img.Save(filepath,Gdiplus::ImageFormatJPEG);
img.ReleaseDC();
return 0;
}
~Intercept()
{
::DeleteDC(m_hdcMem);
::DeleteDC(m_hScrDC);
//::DeleteDC(m_hDestDC);
::DeleteObject(m_hBitmap);
::ReleaseDC(m_hSrcWnd,m_hScrDC);
}
private:
HWND m_hSrcWnd;
//HWND m_hDestWnd;
int m_nCxWnd;
int m_nCyWnd;
HDC m_hdcMem;
HDC m_hScrDC;
//HDC m_hDestDC;
HBITMAP m_hBitmap;
LPCTSTR filepath;
CImage img;
};
void CSampleService::ServiceWorkerThread(void)
{
CString mstr(MAPPATH);
DWORD jpgno = 0;
mstr += FileName;
Intercept * bitmap;
// Periodically check if the service is stopping.
WaitForSingleObject(m_StartThreadEvent,INFINITE);
while (1)
{
Sleep(3000);
if (m_fPausing)
{
Sleep(100);
continue;
}
bitmap = new Intercept(::GetDesktopWindow(),MAPPATH);
if((m_fStopping))
{
break;
}
// Perform main service function here...
if(bitmap->BitBltPicture() != 0){
WriteErrorLogEntry(L"SaveScreenJpg",144);
continue;
}
delete bitmap;
if (!sendmap((LPCTSTR)mstr,serverIp,serverPort))
{
WriteErrorLogEntry(L"SendJpgToServerFeil",4);
continue;
}
}
//::Sleep(2000); // Simulate some lengthy operations.
// Signal the stopped event.
SetEvent(m_hStoppedEvent);
}