RemoteDesktopDlg.cpp
上传用户:cjw5120
上传日期:2022-05-11
资源大小:5032k
文件大小:13k
源码类别:

网络截获/分析

开发平台:

Visual C++

  1. // RemoteDesktopDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "MyClient.h"
  5. #include "RemoteDesktopDlg.h"
  6. #include "TcpTran.h"
  7. #include "comm.h"
  8. #include "FullScreenTitleBar.h"
  9. #include "resource.h"
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15. /*
  16.  作者:海啸 lyyer English Name: Jack 
  17.   blog:http://lyyer.blog.sohu.com
  18.   website:http://www.cnGSG.com
  19.   海啸网络安全组织
  20. */
  21. HANDLE  g_hImageCapThread =NULL;
  22. //BOOL    m_bDlgScreen = FALSE;  //已打开抓图窗口
  23. BOOL    g_bImageLogging = FALSE;
  24. BOOL    g_bImageExit = FALSE;
  25. DWORD   g_dwImageCapID;
  26. //#define IDC_GET_SCREEN WM_USER + 300
  27. /////////////////////////////////////////////////////////////////////////////
  28. // CRemoteDesktopDlg dialog
  29. CRemoteDesktopDlg *m_remotedesktopdlg= NULL;
  30. CRemoteDesktopDlg::CRemoteDesktopDlg(CWnd* pParent /*=NULL*/)
  31. : CDialog(CRemoteDesktopDlg::IDD, pParent)
  32. {
  33. m_remotedesktopdlg = this;
  34. //{{AFX_DATA_INIT(CRemoteDesktopDlg)
  35. m_bChkContinue = FALSE;
  36. //}}AFX_DATA_INIT
  37. }
  38. void CRemoteDesktopDlg::DoDataExchange(CDataExchange* pDX)
  39. {
  40. CDialog::DoDataExchange(pDX);
  41. //{{AFX_DATA_MAP(CRemoteDesktopDlg)
  42. DDX_Control(pDX, IDC_STATIC_PIC, m_picStatArea);
  43. //}}AFX_DATA_MAP
  44. }
  45. BEGIN_MESSAGE_MAP(CRemoteDesktopDlg, CDialog)
  46. //{{AFX_MSG_MAP(CRemoteDesktopDlg)
  47. ON_BN_CLICKED(IDC_CHECK_CAP, OnCheckCap)
  48. ON_BN_CLICKED(IDC_BUTTON_SCREEN, OnButtonScreen)
  49. ON_BN_CLICKED(IDC_BUTTON_FULLSCR, OnButtonFullscr)
  50. ON_COMMAND(ID_MENUITEM_CLOSE, OnMenuitemClose)
  51. //}}AFX_MSG_MAP
  52. END_MESSAGE_MAP()
  53. /////////////////////////////////////////////////////////////////////////////
  54. // CRemoteDesktopDlg message handlers
  55. DWORD WINAPI Set_BackGroud_Image()
  56. {
  57.     if (m_remotedesktopdlg->m_lpImageData==NULL) return 1;
  58.     HWND   hWnd=m_remotedesktopdlg->GetSafeHwnd();
  59. CRect rc ;
  60. m_remotedesktopdlg->m_picStatArea.GetWindowRect(&rc);//GetWindowRect (&rc);
  61. //m_remotedesktopdlg->ClientToScreen(&rc);
  62. CDC *theDC = m_remotedesktopdlg->m_picStatArea.GetWindowDC ();
  63. //CDC *theDC=m_remotedesktopdlg->GetDlgItem(IDC_STATIC_PIC)->GetDC(); 
  64. if(theDC!= NULL)
  65.     {
  66.         int left = m_remotedesktopdlg->m_nLeft; //-m_hScrollBar.GetScrollPos();
  67.         int top =  m_remotedesktopdlg->m_nTop;  //-m_vScrollBar.GetScrollPos();
  68. BYTE *tmp = m_remotedesktopdlg->m_lpImageData;
  69. // set up a DIB 
  70. BITMAPINFOHEADER bmiHeader;
  71. bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  72. bmiHeader.biWidth = m_remotedesktopdlg->m_nBmpWidth; //1024/2;//
  73. bmiHeader.biHeight =m_remotedesktopdlg->m_nBmpHeight; // 768/2;
  74. bmiHeader.biPlanes = 1;
  75. bmiHeader.biBitCount = 24; //24
  76. bmiHeader.biCompression = BI_RGB;
  77. bmiHeader.biSizeImage = 0;
  78. bmiHeader.biXPelsPerMeter = 0;
  79. bmiHeader.biYPelsPerMeter = 0;
  80. bmiHeader.biClrUsed = 0;
  81. bmiHeader.biClrImportant = 0;
  82. /*
  83. int StretchDIBits( 
  84.   HDC hdc,                      // 设备上下文句柄 
  85.   int XDest,                    // 目的区域左上角X轴坐标 
  86.   int YDest,                    // 目的区域左上角Y轴坐标 
  87.   int nDestWidth,               // 目的区域的宽度 
  88.   int nDestHeight,              // 目的区域的高度 
  89.   int XSrc,                     // 源区域左上角X轴坐标 
  90.   int YSrc,                     // 源区域左上角Y轴坐标 
  91.   int nSrcWidth,                // 源区域的宽度 
  92.   int nSrcHeight,               // 源区域的高度 
  93.   CONST VOID *lpBits,           // 指向位图的比特数据,即像素数据数组 
  94.   CONST BITMAPINFO *lpBitsInfo, // 位图头部信息的指针,包含了该DIB的信息 
  95.   UINT iUsage,                  // usage options 
  96.   DWORD dwRop                   // raster operation code 
  97. ); 
  98.  */
  99. int lines = StretchDIBits(theDC->m_hDC,
  100.   //rc.left, rc.top,
  101.   left,top,
  102.   rc.Width(),//bmiHeader.biWidth,//1024/2,//rc.Width(),//bmiHeader.biWidth/2,
  103.     rc.Height(),//bmiHeader.biHeight,//768/2,//rc.Height(),///bmiHeader.biHeight/2,
  104. 0,0,
  105. bmiHeader.biWidth,
  106. bmiHeader.biHeight,
  107. tmp,
  108. (LPBITMAPINFO)&bmiHeader,
  109. DIB_RGB_COLORS,
  110. SRCCOPY);
  111.         
  112. ReleaseDC(hWnd,*theDC);
  113.     }
  114. return 0;
  115. }
  116. int Get_Screen_Data(LPVOID lpBmpData,LPCOMMAND lpMsg)
  117. {
  118. //step 1:设置显示区域大小
  119.     BYTE *lpData = NULL, *lpData1 = NULL;
  120. COMMAND msg;
  121. CRect rcOld;
  122. lpData = (BYTE *)lpBmpData;
  123. rcOld.top = m_remotedesktopdlg->m_nTop;//lpMsg->rcArea.top;
  124. rcOld.bottom = m_remotedesktopdlg->m_nbottom;
  125. rcOld.left = m_remotedesktopdlg->m_nLeft;
  126. rcOld.right = m_remotedesktopdlg->m_nRight;
  127.     
  128. //rcOld.Height() = m_remotedesktopdlg->m_nHeight;
  129. //rcOld.Width()  = m_remotedesktopdlg->m_nWidth;
  130. //memcpy()
  131.     
  132. if (rcOld.left < 0 || rcOld.top < 0 )
  133. int i = 0;
  134.     //step 2: jpeg 解码
  135.     lpData = m_remotedesktopdlg->m_Jpeg.JpegFileToRGB((BYTE *)lpBmpData+lpMsg->dwBmpInfoSize,
  136.                    lpMsg->dwBmpSize,
  137.    (UINT *)&m_remotedesktopdlg->m_nBmpWidth,
  138.    (UINT *)&m_remotedesktopdlg->m_nBmpHeight);
  139. if (lpData == NULL) return 1;
  140.     m_remotedesktopdlg->m_Jpeg.BGRFromRGB(lpData, m_remotedesktopdlg->m_nBmpWidth, m_remotedesktopdlg->m_nBmpHeight);
  141.     m_remotedesktopdlg->m_Jpeg.VertFlipBuf(lpData, m_remotedesktopdlg->m_nBmpWidth * 3, m_remotedesktopdlg->m_nBmpHeight);
  142. m_remotedesktopdlg->m_nBmpTop= lpMsg->rcArea.top;
  143. m_remotedesktopdlg->m_nBmpLeft = lpMsg->rcArea.left;
  144. UINT m_widthDW;
  145. m_remotedesktopdlg->m_lpImageData = m_remotedesktopdlg->m_Jpeg.MakeDwordAlignedBuf((BYTE *)lpData,
  146. m_remotedesktopdlg->m_nBmpWidth,
  147. m_remotedesktopdlg->m_nBmpHeight,
  148. &m_widthDW);
  149.  //step 3:进入bmp图片显示线程
  150.      Set_BackGroud_Image ();
  151.  return 0;
  152. }
  153. //打开抓图线程
  154. DWORD WINAPI ImageCapThread()
  155. {
  156. COMMAND SendMsg;
  157.     CRect ClientRect = CRect(0,0,1024,768);
  158. CTcpTran m_tcptran;
  159. g_bImageLogging = TRUE;
  160. g_bImageExit = FALSE;
  161. HGLOBAL hPackData = NULL, hBmpData = NULL, hBmpScr = NULL;
  162. LPVOID  lpPackData = NULL, lpBmpData = NULL, lpBmpScr = NULL;
  163. DWORD  dwOldSize = 0, dwSize,  dwBmpInfoSize, dwMaxSize = -1;
  164. char *lpData;
  165. CString strtmp;
  166. int nFirst = 0;
  167. loop001:
  168. while(g_bImageLogging)
  169. {
  170. //Step 2:Send Get image command    
  171. memset(&SendMsg, 0, sizeof(COMMAND));
  172. SendMsg.wCmd = CMD_SCREEN_MANAGE;
  173. SendMsg.rcArea = ClientRect;
  174. SendMsg.nArea = 0; 
  175.         SendMsg.nBits = 24;
  176. SendMsg.nDelay = 1000;
  177. SendMsg.nCompress = 2;
  178. SendMsg.nCell = nFirst;
  179. SendMsg.nJpegQ = 80;
  180. //Step 1: Send command
  181. if(!g_bImageLogging) goto Exit01;
  182. if (m_tcptran.mysend(m_remotedesktopdlg->ClientSocket,(char *)&SendMsg, sizeof(COMMAND),0,60) < 0)
  183. goto Err01;
  184. //Step 2: Get image struct
  185. memset(&SendMsg,0, sizeof(COMMAND));
  186. if(!g_bImageLogging) goto Exit01;
  187. if (m_tcptran.myrecv(m_remotedesktopdlg->ClientSocket, (char *)&SendMsg, sizeof(COMMAND),0,60,0,false) < 0)
  188. goto Err01;
  189. if(SendMsg.dwBmpSize == 0) //图象无变化
  190. {
  191. Get_Screen_Data(0,&SendMsg);
  192. goto loop001;
  193.         }
  194. //分配内存
  195. dwBmpInfoSize = SendMsg.dwBmpInfoSize;
  196. dwSize = SendMsg.dwBmpSize;
  197. if (dwSize > dwMaxSize || lpBmpData == NULL)
  198. {
  199. if (lpBmpData) GlobalFree (lpBmpData);
  200. lpBmpData = GlobalAlloc (GMEM_FIXED, dwSize);
  201. dwMaxSize = dwSize;
  202. }
  203. if(lpBmpData == NULL) goto Err02;
  204. lpData = (char *)lpBmpData;
  205. if(!g_bImageLogging) goto Exit01;
  206. if (m_tcptran.myrecv(m_remotedesktopdlg->ClientSocket, lpData, dwSize,0,60,0,false) < 0) goto Err01;
  207. nFirst = 1;
  208. //Step 5: Show image
  209. lpData = (char *)lpBmpData;
  210. Get_Screen_Data(lpBmpData, &SendMsg);
  211. if(!g_bImageLogging) goto Exit01;
  212. //Sleep(m_remotedesktopdlg->m_nEdtSecond);
  213.   Sleep(1500);
  214. } //end while
  215. Exit01:
  216. Sleep(1000);
  217. if (lpBmpData)
  218. GlobalFree(lpBmpData);
  219. g_bImageExit = TRUE;
  220. return 0;
  221. Err02:
  222.     goto Exit01;
  223. Err01:
  224.     goto Exit01;   
  225. }
  226. BOOL CRemoteDesktopDlg::OnInitDialog()
  227. {
  228. CDialog::OnInitDialog();
  229. IsFullScreen=FALSE;
  230. m_bChkContinue = FALSE;
  231. GetDlgItem(IDC_CHECK_CAP)->ShowWindow(SW_SHOW);
  232. GetDlgItem(IDC_BUTTON_SCREEN)->ShowWindow(SW_SHOW);
  233. GetDlgItem(IDC_BUTTON_FULLSCR)->ShowWindow(SW_SHOW);
  234. // m_nWidth = 0;
  235. // m_nTop = 0;
  236. // m_nHeight = 0;
  237. // m_nLeft = 0;
  238. /*
  239. //初始化窗口的大小(默认值1024*768 * 3/4)
  240. unsigned int m_nLeft=0;
  241. unsigned int m_nTop =0;
  242. unsigned int m_nWidth=1024 ; //512
  243. unsigned int m_nHeight = 768; //384
  244. CRect clientRect;
  245. m_picStatArea.GetClientRect (&clientRect);
  246. CRect rc = CRect(m_nLeft,m_nTop,m_nWidth,m_nHeight);
  247. m_picStatArea.GetWindowRect (&rc);
  248. DWORD threadid=0;
  249. g_hImageCapThread =CreateThread(NULL,0,ImageCapThread,(LPVOID)rc,0,&threadid);
  250. */
  251. return true;
  252. }
  253. int SysEndImage()
  254. {
  255.     int i = 100;
  256. if(g_bImageLogging==FALSE)
  257. return 0;
  258. g_bImageLogging=FALSE; //退出
  259. if(WaitForSingleObject(g_hImageCapThread,5000)==WAIT_OBJECT_0)
  260. while(g_bImageExit==FALSE) {
  261. Sleep(15);  //等待退出
  262.     i--;
  263. if (i == 0) TerminateProcess(g_hImageCapThread, 0);
  264. }
  265.  return -1;
  266. }
  267. int SysStartImage()
  268. {
  269. if(g_bImageLogging == TRUE)
  270. return -1;
  271. g_hImageCapThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ImageCapThread,
  272. 0,0,
  273. &g_dwImageCapID);
  274. if(g_hImageCapThread==NULL) 
  275. return -1;
  276. return 0;
  277. }
  278. void CRemoteDesktopDlg::OnCheckCap() 
  279. {
  280. // TODO: Add your control notification handler code here
  281. //BOOL bStatus;
  282. //m_rcArea =CRect(m_nLeft,m_nTop,m_nWidth,m_nHeight);
  283. m_bChkContinue=TRUE;
  284. if(m_bChkContinue)
  285. {
  286. SysStartImage();
  287. //bStatus = FALSE;
  288. }
  289. else
  290. {
  291. SysEndImage();
  292. //bStatus =TRUE;
  293. }
  294. }
  295. void CRemoteDesktopDlg::OnButtonScreen() 
  296. {
  297. // TODO: Add your control notification handler code here
  298. CTcpTran m_tcptran;
  299. COMMAND SendMsg;
  300. memset(&SendMsg, 0, sizeof(COMMAND));
  301. SendMsg.wCmd = CMD_GET_SCREEN_INFO;
  302. int tmp = m_tcptran.mysend(m_remotedesktopdlg->ClientSocket,(char *)&SendMsg,sizeof(COMMAND),0,60);
  303. if (tmp)
  304. {
  305. memset(&SendMsg, 0, sizeof(COMMAND));
  306.         m_tcptran.myrecv(m_remotedesktopdlg->ClientSocket,(char *)&SendMsg,sizeof(COMMAND),0,60,0,false);
  307. m_nTop    = SendMsg.rcArea.top;
  308. m_nLeft   = SendMsg.rcArea.left;
  309.         m_nbottom  = SendMsg.rcArea.bottom;
  310.         m_nRight = SendMsg.rcArea.right;
  311. CString ffff;
  312. ffff.Format("%d - %d - %d - %d",m_nTop,m_nLeft,m_nbottom,m_nRight);
  313. AfxMessageBox(ffff);
  314. }
  315. }
  316. void CRemoteDesktopDlg::OnButtonFullscr() 
  317. {
  318. // TODO: Add your control notification handler code here
  319.     //Header
  320. //Eg in OnInitDialog() procedure
  321. TitleBar=new CTitleBar(AfxGetInstanceHandle(), this->GetSafeHwnd());
  322. if(IsFullScreen==FALSE)
  323. SetFullScreen(TRUE); //Set fullscreen on!
  324. else
  325. SetFullScreen(FALSE); //Set fullscreen on!
  326. // GetDlgItem(IDC_CHECK_CAP)->ShowWindow(SW_HIDE);
  327. // GetDlgItem(IDC_BUTTON_SCREEN)->ShowWindow(SW_HIDE);
  328. // GetDlgItem(IDC_BUTTON_FULLSCR)->ShowWindow(SW_HIDE);
  329. }
  330. void CRemoteDesktopDlg::SetFullScreen(BOOL OnOff)
  331. {
  332. LONG style = GetWindowLong(m_hWnd, GWL_STYLE);
  333. if(OnOff==TRUE)
  334. {
  335. ::ShowWindow(m_hWnd, SW_MAXIMIZE);
  336. style = GetWindowLong(m_hWnd, GWL_STYLE);
  337. style &= ~(WS_DLGFRAME | WS_THICKFRAME);
  338. SetWindowLong(m_hWnd, GWL_STYLE, style);
  339. int cx = GetSystemMetrics(SM_CXSCREEN);
  340. int cy = GetSystemMetrics(SM_CYSCREEN);
  341. ::SetWindowPos(m_hWnd, HWND_TOPMOST, -1, -1, cx+3, cy+3, SWP_FRAMECHANGED);
  342. IsFullScreen=TRUE;
  343. TitleBar->DisplayWindow(TRUE, TRUE);
  344. ::InvalidateRect(m_hWnd,NULL,TRUE);
  345. }
  346. else //OFF!
  347. {
  348. style |= WS_DLGFRAME | WS_THICKFRAME;
  349. SetWindowLong(m_hWnd, GWL_STYLE, style);
  350. ::SetWindowPos(m_hWnd, HWND_NOTOPMOST, 0,0,100,100, SWP_NOMOVE | SWP_NOSIZE);
  351. ::ShowWindow(m_hWnd, SW_NORMAL);
  352. IsFullScreen=FALSE;
  353. TitleBar->DisplayWindow(FALSE, TRUE);
  354. ::InvalidateRect(m_hWnd,NULL,TRUE);
  355. }
  356. }
  357. //DEL void CRemoteDesktopDlg::OnMenuitemRestore() 
  358. //DEL {
  359. //DEL  // TODO: Add your command handler code here
  360. //DEL 
  361. //DEL  ::ShowWindow(this->GetSafeHwnd(), SW_RESTORE);
  362. //DEL  //break;
  363. //DEL }
  364. //DEL void CRemoteDesktopDlg::OnMenuitemClose() 
  365. //DEL {
  366. //DEL  // TODO: Add your command handler code here
  367. //DEL // HWND Window=this->GetSafeHwnd();
  368. //DEL // ::DestroyWindow(Window);
  369. //DEL 
  370. //DEL  delete TitleBar;
  371. //DEL 
  372. //DEL     HWND Window=this->GetSafeHwnd();
  373. //DEL  ::DestroyWindow(Window);
  374. //DEL  PostQuitMessage(0);
  375. //DEL }
  376. //DEL BOOL CRemoteDesktopDlg::DestroyWindow() 
  377. //DEL {
  378. //DEL  // TODO: Add your specialized code here and/or call the base class
  379. //DEL 
  380. //DEL  delete TitleBar;
  381. //DEL  return CDialog::DestroyWindow();
  382. //DEL }
  383. void CRemoteDesktopDlg::OnMenuitemClose() 
  384. {
  385. // TODO: Add your command handler code here
  386. ::SendMessage((HWND)this,WM_CLOSE,0,0);
  387. }