RCAClient.cpp
上传用户:hyz2004817
上传日期:2022-03-30
资源大小:226k
文件大小:19k
源码类别:

远程控制编程

开发平台:

Visual C++

  1. // RCAClient.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include "resource.h"
  5. typedef enum _ThreadWorkState
  6. {
  7. UNINIT =0,
  8. WAIT_CONNECT,
  9. WAIT_REQUEST,
  10. CONNECTING,
  11. REQUESTING,
  12. } THREAD_WORK_STATE;
  13. typedef struct _ThreadCommun {
  14. HANDLE hExitEvent;
  15. HANDLE hBeginConnectEvent;
  16. HANDLE hBeginRequest;
  17. HWND hwnd;
  18. UINT msg;
  19. } THREAD_COMMUN, *PTHREAD_COMMUN;
  20. HWND g_hWnd;
  21. HINSTANCE g_hInstance;
  22. HIMAGELIST g_hImageList;
  23. SOCKET g_hSocket1 = NULL;
  24. SOCKET g_hSocket2 = NULL;
  25. sockaddr_in g_addr;
  26. sockaddr_in g_addr2;
  27. HANDLE g_hThread1 = NULL;
  28. HANDLE g_hThread2 = NULL;
  29. HANDLE g_hEvent1;
  30. HANDLE g_hEvent2;
  31. HBITMAP g_hRemoteBitmap = NULL;
  32. THREAD_COMMUN g_tc;
  33. THREAD_WORK_STATE g_ThreadState = UNINIT;
  34. RCAGETIMAGEDATA g_gm;
  35. BOOL g_bMustExit = FALSE;
  36. DWORD g_dwServerIP = 0;
  37. u_short g_usPort = 0;
  38. INT g_hWidth;
  39. INT g_hHeight;
  40. // 主窗口的子窗口标识符
  41. #define ID_TOOLBAR 1 // 工具栏窗口
  42. #define WM_THREADWORKSTATE (WM_USER+100)
  43. #define CANNOT_WAIT 0
  44. #define CANNOT_CREATESOCKET 1
  45. #define CANNOT_CONNECT 2
  46. #define CANNOT_REQUEST 3
  47. #define RESPONSE_ERROR 4
  48. #define WORK_SUCCEEDED 5
  49. #define CONNECTED 6
  50. INT WINAPI AddToolbarString( HWND tb, char * string )
  51. {
  52. char tmp[256];
  53. lstrcpy( tmp, string );
  54. tmp[ lstrlen(string) +1 ] = 0;
  55. return (INT)SendMessage( tb, TB_ADDSTRING, 0, (LPARAM)(LPSTR)tmp );
  56. }
  57. /////////////////////////////////////////////////////
  58. ///
  59. // 获取远程计算机图象函数, 由 创建的一个工作线程运行
  60. //
  61. DWORD WINAPI ThreadFunc1( PVOID p )
  62. {
  63. DWORD dwret;
  64. RCAREQUESTHEADER rreqh;
  65. RCAGETIMAGEDATA gm;
  66. RECT rect;
  67. PTHREAD_COMMUN ptc = (PTHREAD_COMMUN)p;
  68. HANDLE hEventArray[2] = { NULL, ptc->hExitEvent };
  69. HANDLE hWaitConnectArray[2] = { ptc->hBeginConnectEvent, ptc->hExitEvent };
  70. HANDLE  hWaitRequestArray[3] = { ptc->hBeginRequest, ptc->hExitEvent, ptc->hBeginConnectEvent };
  71. InitRCARequestHead( &rreqh, 1, 0, 0X20, sizeof(rreqh) + sizeof(gm) );
  72. rreqh.wMajorVersion = 1;
  73. rreqh.wMinorVersion = 0;
  74. gm.x = 0;
  75. gm.y = 0;
  76. gm.w = 0;
  77. gm.h = 0;
  78. gm.zh = 0;
  79. gm.zw = 0;
  80. gm.iBitCount = 8;
  81. gm.dwFlagEx1 = 50; // 图象质量
  82. gm.dwImageFormat = IMAGE_FORMAT_JPEG;
  83. g_gm.dwImageFormat = IMAGE_FORMAT_JPEG;
  84. g_gm.iBitCount = 8;
  85. g_gm.dwFlagEx1 = 50;
  86. while(1)
  87. {
  88. // 线程进入等待, 准备连接服务器
  89. g_ThreadState = WAIT_CONNECT;
  90. dwret = WaitForMultipleObjects( 2, hWaitConnectArray, FALSE, INFINITE );
  91. g_ThreadState = CONNECTING;
  92. if( dwret == (WAIT_OBJECT_0 +1))
  93. {
  94. return 0;
  95. }
  96. if( dwret == WAIT_FAILED)
  97. {
  98. PostMessage( ptc->hwnd, ptc->msg, CANNOT_WAIT, 0 );
  99. g_ThreadState = UNINIT;
  100. return 0;
  101. }
  102. ///////////////////////////////////////////////////////////
  103. if( !CreateEventSelectSocket( &g_hSocket1, &(hEventArray[0]), 
  104. FD_CONNECT | FD_WRITE | FD_READ | FD_CLOSE ) )
  105. {
  106. PostMessage( ptc->hwnd, ptc->msg, CANNOT_CREATESOCKET, 0 );
  107. continue;
  108. }
  109. g_addr.sin_addr.S_un.S_addr = g_dwServerIP;
  110. g_addr.sin_family = AF_INET;
  111. g_addr.sin_port = g_usPort;
  112. if( TRUE != RCAConnect_EventSelectIO( g_hSocket1, hEventArray, 
  113. (sockaddr*)&g_addr, sizeof(sockaddr) ) )
  114. {
  115. CloseHandle( g_hEvent1 );
  116. closesocket( g_hSocket1 );
  117. PostMessage( ptc->hwnd, ptc->msg, CANNOT_CONNECT, 0 );
  118. continue;
  119. }
  120. PostMessage( ptc->hwnd, ptc->msg, CONNECTED , 0 );
  121. ////////////////////////////
  122. // 请求开始
  123. while(1)
  124. {
  125. g_ThreadState = WAIT_REQUEST;
  126. dwret = WaitForMultipleObjects( 3,  hWaitRequestArray, FALSE, INFINITE );
  127. g_ThreadState = REQUESTING;
  128. if( dwret != (WAIT_OBJECT_0 ) )
  129. {
  130. if( dwret == (WAIT_OBJECT_0 +1) )
  131. {
  132. break;
  133. } else if( dwret == (WAIT_OBJECT_0 +2 ))
  134. break; 
  135. } else if( dwret == WAIT_FAILED )
  136. {
  137. PostMessage(ptc->hwnd, ptc->msg, CANNOT_WAIT, 0 );
  138. break;
  139. }
  140. // never run to here
  141. }
  142. GetClientRect( ptc->hwnd, &rect );
  143. gm.zw = rect.right;
  144. gm.zh = rect.bottom;
  145. gm.dwFlagEx1 = g_gm.dwFlagEx1;
  146. gm.dwImageFormat = g_gm.dwImageFormat;
  147. gm.iBitCount = g_gm.iBitCount;
  148. // now dwret==WAIT_OBJECT, start request server
  149. if( TRUE != RCASendRequest_0X20_EventSelectIO( g_hSocket1, hEventArray, 
  150. &gm ) )
  151. {
  152. PostMessage(ptc->hwnd, ptc->msg, CANNOT_REQUEST, 0 );
  153. break;
  154. }
  155.  
  156. HBITMAP bm = RCARecvImageData( g_hSocket1, hEventArray, gm.dwImageFormat );
  157. if( bm == NULL )
  158. {
  159. PostMessage( ptc->hwnd, ptc->msg, CANNOT_REQUEST, 0 );
  160. break;
  161. }
  162. PostMessage( ptc->hwnd, ptc->msg, WORK_SUCCEEDED, (LPARAM)bm );
  163. }
  164. if( g_bMustExit )
  165. break;
  166. else
  167. {
  168. closesocket( g_hSocket1 );
  169. CloseHandle( hEventArray[0] );
  170. }
  171. }
  172. closesocket( g_hSocket1 );
  173. CloseHandle( hEventArray[0] );
  174. return 0;
  175. }
  176. DWORD WINAPI ThreadFunc2( PVOID p )
  177. {
  178. while(1)
  179. {
  180. SuspendThread( GetCurrentThread());
  181. }
  182. return 0;
  183. }
  184. VOID WINAPI  OnPaint( HWND hwnd, HDC hdc )
  185. {
  186. if( g_hRemoteBitmap )
  187. {
  188. DIBSECTION  ds;
  189. RECT rect;
  190. GetClientRect( hwnd, &rect );
  191. GetObject( g_hRemoteBitmap, sizeof(ds), (LPVOID)&ds );
  192. HDC hdcmem = CreateCompatibleDC(hdc);
  193. SelectObject( hdcmem, g_hRemoteBitmap );
  194. StretchBlt( hdc, 0, 0, rect.right, rect.bottom, 
  195. hdcmem, 0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight, SRCCOPY );
  196. SelectObject( hdcmem, (HBITMAP)NULL );
  197. DeleteDC( hdcmem );
  198. }
  199. }
  200. VOID WINAPI OnThreadWorkState( HWND hwnd, WPARAM w, LPARAM l )
  201. {
  202. switch( w )
  203. {
  204. case WORK_SUCCEEDED:
  205. if(g_hRemoteBitmap != NULL )
  206. DeleteObject( g_hRemoteBitmap );
  207. g_hRemoteBitmap = (HBITMAP)l;
  208. InvalidateRect( hwnd, NULL, FALSE );
  209. break;
  210. }
  211. return;
  212. }
  213. BOOL WINAPI InitWorkThread( HWND hwnd )
  214. {
  215. // 确保 WIN98 的支持
  216. DWORD tmp;
  217. g_tc.hBeginConnectEvent = CreateEvent( NULL, FALSE, FALSE, NULL );
  218. g_tc.hBeginRequest = CreateEvent( NULL, FALSE, FALSE, NULL );
  219. g_tc.hExitEvent = CreateEvent( NULL, FALSE, FALSE, NULL );
  220. g_tc.hwnd = hwnd;
  221. g_tc.msg = WM_THREADWORKSTATE;
  222. g_hRemoteBitmap = NULL;
  223. g_hThread1 = CreateThread( NULL, 0, ThreadFunc1, (LPVOID)&g_tc, 0, &tmp );
  224. if( g_hThread1 != NULL )
  225. return TRUE;
  226. return TRUE;
  227. }
  228. VOID WINAPI DestroyWorkThreadResource()
  229. {
  230. if( g_ThreadState == UNINIT )
  231. return;
  232. // 通知线程退出;
  233. SetEvent( g_tc.hExitEvent );
  234. if( g_hSocket2 != NULL )
  235. closesocket( g_hSocket2 );
  236. if( g_hThread1 )
  237. {
  238. if ( WAIT_TIMEOUT == WaitForSingleObject( g_hThread1, 3000 ) )
  239. TerminateThread( g_hThread1, 0);
  240. CloseHandle( g_hThread1 );
  241. g_hThread1 = NULL;
  242. }
  243. // 关闭用于通信的所有事件句柄
  244. CloseHandle( g_tc.hBeginConnectEvent );
  245. CloseHandle( g_tc.hBeginRequest );
  246. CloseHandle( g_tc.hExitEvent );
  247. if( g_hRemoteBitmap  )
  248. {
  249. DeleteObject( g_hRemoteBitmap );
  250. g_hRemoteBitmap = NULL;
  251. }
  252. }
  253. BOOL WINAPI OnCreate( HWND hwnd, PAINTSTRUCT * pps )
  254. {
  255. WSADATA data;
  256. if( SOCKET_ERROR == WSAStartup( MAKEWORD(2,2), &data ) )
  257. return FALSE;
  258. INITCOMMONCONTROLSEX icex;
  259. icex.dwSize = sizeof(icex);
  260. icex.dwICC = ICC_WIN95_CLASSES;
  261. if( !InitWorkThread( hwnd) )
  262. return FALSE;
  263. return TRUE;
  264. }
  265. VOID WINAPI OnDestroy()
  266. {
  267. WSACleanup();
  268. DestroyWorkThreadResource();
  269. }
  270. BOOL CALLBACK ConnectDialogProc( HWND dlg, UINT msg, WPARAM w, LPARAM l )
  271. {
  272. char str[256];
  273. int ret;
  274. switch( msg )
  275. {
  276. case WM_INITDIALOG:
  277. SetFocus( GetDlgItem( dlg, IDC_SERVERNAME ) );
  278. return TRUE;
  279. case WM_COMMAND:
  280. switch( w )
  281. {
  282. case IDOK:
  283. GetWindowText( GetDlgItem(dlg, IDC_SERVERNAME), str, 255 );
  284. g_dwServerIP = inet_addr( str );
  285. GetWindowText( GetDlgItem( dlg, IDC_PORT), str, 255 );
  286. g_usPort  = htons( atoi(str));
  287. if( g_ThreadState == WAIT_CONNECT )
  288. {
  289. g_addr2.sin_addr.S_un.S_addr = g_dwServerIP;
  290. g_addr2.sin_family = AF_INET;
  291. g_addr2.sin_port = g_usPort;
  292. g_hSocket2 = socket( AF_INET, SOCK_STREAM, 0 );
  293. if( g_hSocket2 == INVALID_SOCKET )
  294. {
  295. MessageBox( dlg, "资源不足, 无法创建套接字", "RCA Client Error" , MB_ICONERROR );
  296. g_hSocket2 = NULL;
  297. return TRUE;
  298. }
  299. if( g_ThreadState == WAIT_CONNECT )
  300. SetEvent( g_tc.hBeginConnectEvent );
  301. ret = connect( g_hSocket2, (sockaddr*)&g_addr2, sizeof(sockaddr) );
  302. if( ret == SOCKET_ERROR )
  303. {
  304. MessageBox( dlg, WSAERRORSTRING, "RCA Client Error", MB_ICONERROR );
  305. closesocket( g_hSocket2 );
  306. g_hSocket2 = NULL;
  307. return TRUE;
  308. }
  309. MSG msg;
  310. while(1)
  311. {
  312. GetMessage( &msg, NULL, NULL, NULL );
  313. if( (msg.hwnd == g_hWnd) && (msg.message==WM_THREADWORKSTATE))
  314. {
  315. if( msg.wParam != CONNECTED )
  316. {
  317. MessageBox( dlg, WSAERRORSTRING, "RCA Client Error", MB_ICONERROR );
  318. return FALSE;
  319. }
  320. {
  321. EndDialog( dlg, TRUE );
  322. return TRUE;
  323. }
  324. }
  325. else
  326. {
  327. TranslateMessage( &msg );
  328. DispatchMessage( &msg );
  329. }
  330. }
  331. if( g_ThreadState == WAIT_REQUEST )
  332. {
  333. EndDialog( dlg, TRUE );
  334. }
  335. return TRUE;
  336. break;
  337. case IDCANCEL:
  338. EndDialog( dlg, FALSE );
  339. return TRUE;
  340. default:
  341. break;
  342. }
  343. default:
  344. return FALSE;
  345. }
  346. return FALSE;
  347. }
  348. VOID WINAPI OnCommand( HWND hwnd, WPARAM w, LPARAM l )
  349. {
  350. // 筛选命令 ID
  351. switch( w )
  352. {
  353. case ID_EXIT:
  354. if ( g_ThreadState == CONNECTING)
  355. {
  356. if( IDOK == MessageBox( hwnd, "正在获取远程计算机屏幕信息, 是否要中断连接 ?"
  357. , "RCA Client", MB_ICONQUESTION | MB_OKCANCEL ) )
  358. DestroyWindow( hwnd );
  359. }
  360. else
  361. {
  362. g_bMustExit = TRUE;
  363. DestroyWindow( hwnd );
  364. }
  365. break;
  366. case ID_CONNECT:
  367. //if( false )
  368. if( FALSE == DialogBox( g_hInstance, MAKEINTRESOURCE(IDD_CONNECTDIALOG), 
  369. g_hWnd, ConnectDialogProc ) )
  370. {
  371. return;
  372. } else {
  373. // 设置 UI
  374. EnableMenuItem( GetMenu(hwnd), ID_DISCONNECT, MF_ENABLED);
  375. EnableMenuItem( GetMenu(hwnd), ID_CONNECT, MF_GRAYED | MF_DISABLED);
  376. EnableMenuItem( GetMenu(hwnd), ID_REFRUSH, MF_ENABLED );
  377. }
  378. break;
  379. case ID_IMAGE_FORMAT_BMP:
  380. CheckMenuItem( GetMenu(hwnd), ID_IMAGE_FORMAT_BMP , MF_CHECKED );
  381. CheckMenuItem( GetMenu(hwnd), ID_IMAGE_FORMAT_JPEG, MF_UNCHECKED );
  382. g_gm.dwImageFormat = IMAGE_FORMAT_BMP;
  383. return;
  384. case ID_IMAGE_FORMAT_JPEG:
  385. CheckMenuItem( GetMenu(hwnd), ID_IMAGE_FORMAT_JPEG , MF_CHECKED );
  386. CheckMenuItem( GetMenu(hwnd), ID_IMAGE_FORMAT_BMP, MF_UNCHECKED );
  387. g_gm.dwImageFormat = IMAGE_FORMAT_JPEG;
  388. return;
  389. case ID_BIT_8:
  390. CheckMenuItem( GetMenu(hwnd), ID_BIT_8, MF_CHECKED );
  391. CheckMenuItem( GetMenu(hwnd), ID_BIT_16, MF_UNCHECKED );
  392. CheckMenuItem( GetMenu(hwnd), ID_BIT_24, MF_UNCHECKED );
  393. g_gm.iBitCount = 8;
  394. return;
  395. case ID_BIT_16:
  396. CheckMenuItem( GetMenu(hwnd), ID_BIT_8, MF_UNCHECKED );
  397. CheckMenuItem( GetMenu(hwnd), ID_BIT_16, MF_CHECKED );
  398. CheckMenuItem( GetMenu(hwnd), ID_BIT_24, MF_UNCHECKED );
  399. g_gm.iBitCount = 16;
  400. return;
  401. case ID_BIT_24:
  402. CheckMenuItem( GetMenu(hwnd), ID_BIT_8, MF_UNCHECKED );
  403. CheckMenuItem( GetMenu(hwnd), ID_BIT_16, MF_UNCHECKED );
  404. CheckMenuItem( GetMenu(hwnd), ID_BIT_24, MF_CHECKED );
  405. g_gm.iBitCount = 24;
  406. return;
  407. case ID_JPEG_QUALITY_10:
  408. CheckMenuItem( GetMenu(hwnd), ID_JPEG_QUALITY_10, MF_CHECKED );
  409. CheckMenuItem( GetMenu(hwnd), ID_JPEG_QUALITY_30, MF_UNCHECKED );
  410. CheckMenuItem( GetMenu(hwnd), ID_JPEG_QUALITY_50, MF_UNCHECKED );
  411. CheckMenuItem( GetMenu(hwnd), ID_JPEG_QUALITY_70, MF_UNCHECKED );
  412. CheckMenuItem( GetMenu(hwnd), ID_JPEG_QUALITY_90, MF_UNCHECKED );
  413. g_gm.dwFlagEx1 = 10;
  414. return;
  415. case ID_JPEG_QUALITY_30:
  416. CheckMenuItem( GetMenu(hwnd), ID_JPEG_QUALITY_10, MF_UNCHECKED );
  417. CheckMenuItem( GetMenu(hwnd), ID_JPEG_QUALITY_30, MF_CHECKED );
  418. CheckMenuItem( GetMenu(hwnd), ID_JPEG_QUALITY_50, MF_UNCHECKED );
  419. CheckMenuItem( GetMenu(hwnd), ID_JPEG_QUALITY_70, MF_UNCHECKED );
  420. CheckMenuItem( GetMenu(hwnd), ID_JPEG_QUALITY_90, MF_UNCHECKED );
  421. g_gm.dwFlagEx1 = 30;
  422. return;
  423. case ID_JPEG_QUALITY_50:
  424. CheckMenuItem( GetMenu(hwnd), ID_JPEG_QUALITY_10, MF_UNCHECKED );
  425. CheckMenuItem( GetMenu(hwnd), ID_JPEG_QUALITY_30, MF_UNCHECKED );
  426. CheckMenuItem( GetMenu(hwnd), ID_JPEG_QUALITY_50, MF_CHECKED );
  427. CheckMenuItem( GetMenu(hwnd), ID_JPEG_QUALITY_70, MF_UNCHECKED );
  428. CheckMenuItem( GetMenu(hwnd), ID_JPEG_QUALITY_90, MF_UNCHECKED );
  429. g_gm.dwFlagEx1 = 50;
  430. return;
  431. case ID_JPEG_QUALITY_70:
  432. CheckMenuItem( GetMenu(hwnd), ID_JPEG_QUALITY_10, MF_UNCHECKED );
  433. CheckMenuItem( GetMenu(hwnd), ID_JPEG_QUALITY_30, MF_UNCHECKED );
  434. CheckMenuItem( GetMenu(hwnd), ID_JPEG_QUALITY_50, MF_UNCHECKED );
  435. CheckMenuItem( GetMenu(hwnd), ID_JPEG_QUALITY_70, MF_CHECKED );
  436. CheckMenuItem( GetMenu(hwnd), ID_JPEG_QUALITY_90, MF_UNCHECKED );
  437. g_gm.dwFlagEx1 = 70;
  438. return;
  439. case ID_JPEG_QUALITY_90:
  440. CheckMenuItem( GetMenu(hwnd), ID_JPEG_QUALITY_10, MF_UNCHECKED );
  441. CheckMenuItem( GetMenu(hwnd), ID_JPEG_QUALITY_30, MF_UNCHECKED );
  442. CheckMenuItem( GetMenu(hwnd), ID_JPEG_QUALITY_50, MF_UNCHECKED );
  443. CheckMenuItem( GetMenu(hwnd), ID_JPEG_QUALITY_70, MF_UNCHECKED );
  444. CheckMenuItem( GetMenu(hwnd), ID_JPEG_QUALITY_90, MF_CHECKED );
  445. g_gm.dwFlagEx1 = 90;
  446. return;
  447. case ID_REFRUSH:
  448. if( g_ThreadState == WAIT_REQUEST )
  449. SetEvent( g_tc.hBeginRequest );
  450. break;
  451. case ID_DISCONNECT:
  452. EnableMenuItem( GetMenu(hwnd), ID_CONNECT, MF_ENABLED );
  453. EnableMenuItem( GetMenu(hwnd), ID_DISCONNECT, MF_DISABLED | MF_GRAYED );
  454. EnableMenuItem( GetMenu(hwnd), ID_REFRUSH, MF_DISABLED | MF_GRAYED );
  455. if( g_ThreadState == WAIT_REQUEST )
  456. {
  457. SetEvent( g_tc.hBeginConnectEvent );
  458. }
  459. if( g_ThreadState == REQUESTING )
  460. {
  461. SetEvent( g_tc.hExitEvent );
  462. }
  463. closesocket( g_hSocket2 );
  464. g_hSocket2 = NULL;
  465. break;
  466. default:
  467. break;
  468. }
  469. return;
  470. }
  471. RCAMOUSEINPUT mi;
  472. RCAKEYBDINPUT ki;
  473. RCAREQUESTHEADER rreqh;
  474. LRESULT CALLBACK MainWindowProc( HWND hwnd, UINT msg, WPARAM w, LPARAM l )
  475. {
  476. PAINTSTRUCT ps;
  477. HDC hdc;
  478. RECT rect;
  479. INT ret;
  480. GetClientRect( hwnd, &rect );
  481. static UINT_PTR timer;
  482. switch( msg )
  483. {
  484. case WM_RBUTTONDOWN:
  485. if( g_hSocket2 )
  486. {
  487. mi.dwExtraInfo =0;
  488. mi.dwFlags = MOUSEEVENTF_RIGHTDOWN;
  489. //mi.dx = (lParam&0x0000ffff) * (65535 / 1024);
  490. //mi.dy = (lParam>>16) * (65535 / 768);
  491. mi.mouseData = 0;
  492. mi.time = 0;
  493. rreqh.dwRequestID = 0x40;
  494. rreqh.requestBytes = sizeof(RCAREQUESTHEADER) + sizeof(RCAMOUSEINPUT);
  495. ret = send(g_hSocket2, (char*)&rreqh, sizeof(rreqh), 0 );
  496. if( ret == SOCKET_ERROR )
  497. return FALSE;
  498. ret = send(g_hSocket2, (char*)&mi, sizeof(mi), 0 );
  499. if( ret == SOCKET_ERROR )
  500. return FALSE;
  501. }
  502. return TRUE;
  503. case WM_RBUTTONUP:
  504. if( g_hSocket2 )
  505. {
  506. mi.dwExtraInfo =0;
  507. mi.dwFlags = MOUSEEVENTF_RIGHTUP;
  508. //mi.dx = (lParam&0x0000ffff) * (65535 / 1024);
  509. //mi.dy = (lParam>>16) * (65535 / 768);
  510. mi.mouseData = 0;
  511. mi.time = 0;
  512. rreqh.dwRequestID = 0x40;
  513. rreqh.requestBytes = sizeof(RCAREQUESTHEADER) + sizeof(RCAMOUSEINPUT);
  514. ret = send(g_hSocket2, (char*)&rreqh, sizeof(rreqh), 0 );
  515. if( ret == SOCKET_ERROR )
  516. return FALSE;
  517. ret = send(g_hSocket2, (char*)&mi, sizeof(mi), 0 );
  518. if( ret == SOCKET_ERROR )
  519. return FALSE;
  520. }
  521. return TRUE;
  522. case WM_LBUTTONUP:
  523. if( g_hSocket2 )
  524. {
  525. mi.dwExtraInfo =0;
  526. mi.dwFlags = MOUSEEVENTF_LEFTUP;
  527. // mi.dx = (lParam&0x0000ffff) * (65535 / 1024);
  528. // mi.dy = (lParam>>16) * (65535 / 768);
  529. mi.mouseData = 0;
  530. mi.time = 0;
  531. rreqh.dwRequestID = 0x40;
  532. rreqh.requestBytes = sizeof(RCAREQUESTHEADER) + sizeof(RCAMOUSEINPUT);
  533. ret = send(g_hSocket2, (char*)&rreqh, sizeof(rreqh), 0 );
  534. if( ret == SOCKET_ERROR )
  535. return FALSE;
  536. ret = send(g_hSocket2, (char*)&mi, sizeof(mi), 0 );
  537. if( ret == SOCKET_ERROR )
  538. return FALSE;
  539. }
  540. return TRUE;
  541. case WM_LBUTTONDOWN:
  542. if( g_hSocket2 )
  543. {
  544. mi.dwExtraInfo =0;
  545. mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
  546. // mi.dx = (lParam&0x0000ffff) * (65535 / 1024);
  547. // mi.dy = (lParam>>16) * (65535 / 768);
  548. mi.mouseData = 0;
  549. mi.time = 0;
  550. rreqh.dwRequestID = 0x40;
  551. rreqh.requestBytes = sizeof(RCAREQUESTHEADER) + sizeof(RCAMOUSEINPUT);
  552. ret = send(g_hSocket2, (char*)&rreqh, sizeof(rreqh), 0 );
  553. if( ret == SOCKET_ERROR )
  554. return FALSE;
  555. ret = send(g_hSocket2, (char*)&mi, sizeof(mi), 0 );
  556. if( ret == SOCKET_ERROR )
  557. return FALSE;
  558. }
  559. return TRUE;
  560. case WM_MOUSEMOVE:
  561. if( g_hSocket2 )
  562. {
  563. mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
  564. mi.dx = (LONG)(l & 0x0000ffff) * 65535 / rect.right;
  565. mi.dy = (LONG)(l >> 16) * 65535 / rect.bottom;
  566. rreqh.dwRequestID = 0x40;
  567. rreqh.requestBytes = 64;
  568. ret = send(g_hSocket2, (char*)&rreqh, sizeof(rreqh), 0 );
  569. if( ret == SOCKET_ERROR )
  570. {
  571. MessageBox( hwnd, "网络连接出现问题或 RCA 服务器已经被关闭", "RCA Client Information", MB_ICONINFORMATION );
  572. PostMessage( hwnd, WM_COMMAND, ID_DISCONNECT, 0 );
  573. return FALSE;
  574. }
  575. ret = send(g_hSocket2, (char*)&mi, sizeof(mi), 0 );
  576. if( ret == SOCKET_ERROR )
  577. return FALSE;
  578. }
  579. break;
  580. case WM_KEYDOWN:
  581. if( (w==VK_F5) && (g_ThreadState==WAIT_REQUEST) )
  582. SetEvent( g_tc.hBeginRequest );
  583. if( (w==VK_RWIN) || (w==VK_LWIN) )
  584. break;
  585. break;
  586. case WM_THREADWORKSTATE:
  587. OnThreadWorkState( hwnd, w, l );
  588. return 0;
  589. case WM_COMMAND:
  590. OnCommand( hwnd, w, l );
  591. return 0;
  592. case WM_TIMER:
  593. if( g_ThreadState != WAIT_CONNECT )
  594. PostMessage(hwnd, WM_COMMAND, ID_DISCONNECT , NULL );
  595. DestroyWindow( hwnd );
  596. KillTimer( hwnd, timer );
  597. return TRUE;
  598. case WM_CREATE:
  599. mi.dwExtraInfo =0;
  600. mi.mouseData = 0;
  601. mi.time = 0;
  602. lstrcpy(rreqh.rcaID, "RCA" );
  603. rreqh.wMajorVersion =1;
  604. rreqh.wMinorVersion =0;
  605. timer = SetTimer(hwnd, 1, 10 * 60 * 1000, NULL );
  606. return OnCreate( hwnd, (PAINTSTRUCT*)l );
  607. g_gm.dwImageFormat = IMAGE_FORMAT_JPEG;
  608. g_gm.iBitCount = 8;
  609. g_gm.dwFlagEx1 = 50;
  610. break;
  611. case WM_SIZE:
  612. SendMessage( GetDlgItem( hwnd, ID_TOOLBAR ), TB_AUTOSIZE, 0, 0 );
  613. break;
  614. case WM_PAINT:
  615. hdc = BeginPaint( hwnd, &ps );
  616. OnPaint( hwnd, hdc );
  617. EndPaint( hwnd, &ps );
  618. break;
  619. case WM_DESTROY:
  620. g_bMustExit = TRUE;
  621. OnDestroy();
  622. PostQuitMessage(0);
  623. break;
  624. default:
  625. break;
  626. }
  627. return DefWindowProc( hwnd, msg, w, l );
  628. }
  629. INT WINAPI WinMain( HINSTANCE, HINSTANCE, PSTR, INT )
  630. //int main(int argc, char* argv[])
  631. {
  632. g_hInstance = GetModuleHandle( NULL );
  633. WNDCLASSEX wcex;
  634. wcex.cbClsExtra = 0;
  635. wcex.cbSize = sizeof(wcex);
  636. wcex.cbWndExtra = 0;
  637. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW +1);
  638. wcex.hCursor = LoadCursor( NULL, IDC_ARROW );
  639. wcex.hIcon = LoadIcon( g_hInstance, MAKEINTRESOURCE(IDI_CLIENTICON) );
  640. wcex.hIconSm = LoadIcon( g_hInstance, MAKEINTRESOURCE(IDI_CLIENTICON) );
  641. wcex.hInstance = g_hInstance;
  642. wcex.lpfnWndProc = MainWindowProc;
  643. wcex.lpszMenuName = NULL;
  644. wcex.lpszClassName = "RCAClient_Window";
  645. wcex.style = CS_VREDRAW | CS_HREDRAW;
  646. if( !RegisterClassEx( &wcex ) )
  647. return FALSE;
  648. g_hWnd = CreateWindowEx( 0, "RCAClient_Window", "RCA Client", WS_OVERLAPPEDWINDOW,
  649. CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 
  650. NULL, LoadMenu( g_hInstance, MAKEINTRESOURCE(IDR_MENU1) ), 
  651. g_hInstance, NULL );
  652. if( !g_hWnd )
  653. return 0;
  654. ShowWindow( g_hWnd, SW_SHOWNORMAL );
  655. UpdateWindow( g_hWnd );
  656. MSG  msg;
  657. while( GetMessage( &msg, NULL, 0, 0 ) )
  658. {
  659. TranslateMessage( &msg );
  660. DispatchMessage( &msg );
  661. }
  662. return 0;
  663. }