MainWndProc.cpp
上传用户:szopptop
上传日期:2013-04-23
资源大小:1047k
文件大小:7k
源码类别:

模拟服务器

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. LPARAM OnServerSockMsg(WPARAM wParam, LPARAM lParam);
  3. LPARAM OnClientSockMsg(WPARAM wParam, LPARAM lParam);
  4. BOOL jRegSetKey(LPCTSTR pSubKeyName, LPCTSTR pValueName, DWORD dwFlags, LPBYTE pValue, DWORD nValueSize);
  5. BOOL jRegGetKey(LPCTSTR pSubKeyName, LPCTSTR pValueName, LPBYTE pValue);
  6. BOOL CALLBACK ConfigDlgFunc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
  7. VOID WINAPI OnTimerProc(HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime);
  8. extern HINSTANCE g_hInst;
  9. extern HWND g_hMainWnd;
  10. extern HWND g_hLogMsgWnd;
  11. extern HWND g_hToolBar;
  12. extern HWND g_hStatusBar;
  13. extern HANDLE g_hMsgThread;
  14. #ifndef _SOCKET_ASYNC_IO
  15. extern HANDLE g_hIOCP;
  16. extern HANDLE g_hAcceptThread;
  17. #endif
  18. SOCKET g_ssock = INVALID_SOCKET;
  19. SOCKADDR_IN g_saddr;
  20. SOCKET g_csock = INVALID_SOCKET;
  21. SOCKADDR_IN g_caddr;
  22. BOOL g_fTerminated = FALSE;
  23. void SetFontColor()
  24. {
  25. CHOOSECOLOR color;
  26. static COLORREF acrCustClr[16]; 
  27. memset( &color, 0, sizeof( color ) );
  28. color.hwndOwner = g_hMainWnd;
  29. color.lStructSize = sizeof( color );
  30. color.Flags = CC_FULLOPEN | CC_RGBINIT;
  31. color.rgbResult = ListView_GetTextColor( g_hLogMsgWnd );
  32. color.lpCustColors = acrCustClr;
  33. if ( !ChooseColor( &color ) )
  34. return;
  35. ListView_SetTextColor( g_hLogMsgWnd, color.rgbResult );
  36. InvalidateRect( g_hLogMsgWnd, NULL, TRUE );
  37. }
  38. void SetBackColor()
  39. {
  40. CHOOSECOLOR color;
  41. static COLORREF acrCustClr[16]; 
  42. memset( &color, 0, sizeof( color ) );
  43. color.hwndOwner = g_hMainWnd;
  44. color.lStructSize = sizeof( color );
  45. color.Flags = CC_FULLOPEN | CC_RGBINIT;
  46. color.rgbResult = ListView_GetBkColor( g_hLogMsgWnd );
  47. color.lpCustColors = acrCustClr;
  48. if ( !ChooseColor( &color ) )
  49. return;
  50. ListView_SetTextBkColor( g_hLogMsgWnd, color.rgbResult );
  51. ListView_SetBkColor( g_hLogMsgWnd, color.rgbResult );
  52. InvalidateRect( g_hLogMsgWnd, NULL, TRUE );
  53. }
  54. void OnCommand(WPARAM wParam, LPARAM lParam)
  55. {
  56. switch (LOWORD(wParam))
  57. {
  58. case IDM_STARTSERVICE:
  59. {
  60. DWORD dwIP = 0;
  61. int nPort = 0;
  62. g_fTerminated = FALSE;
  63. if (!jRegGetKey(_SELGATE_SERVER_REGISTRY, _TEXT("LocalPort"), (LPBYTE)&nPort))
  64. nPort = 7100;
  65. InitServerSocket(g_ssock, &g_saddr, _IDM_SERVERSOCK_MSG, nPort, 2);
  66. jRegGetKey(_SELGATE_SERVER_REGISTRY, _TEXT("RemoteIP"), (LPBYTE)&dwIP);
  67. if (!jRegGetKey(_SELGATE_SERVER_REGISTRY, _TEXT("RemotePort"), (LPBYTE)&nPort))
  68. nPort = 5100;
  69. ConnectToServer(g_csock, &g_caddr, _IDM_CLIENTSOCK_MSG, NULL, dwIP, nPort, FD_CONNECT|FD_READ|FD_CLOSE);
  70. HMENU hMainMenu = GetMenu(g_hMainWnd);
  71. HMENU hMenu = GetSubMenu(hMainMenu, 0);
  72. EnableMenuItem(hMenu, IDM_STARTSERVICE, MF_GRAYED|MF_BYCOMMAND);
  73. EnableMenuItem(hMenu, IDM_STOPSERVICE, MF_ENABLED|MF_BYCOMMAND);
  74. SendMessage(g_hToolBar, TB_SETSTATE, (WPARAM)IDM_STARTSERVICE, (LPARAM)MAKELONG(TBSTATE_INDETERMINATE, 0));
  75. SendMessage(g_hToolBar, TB_SETSTATE, (WPARAM)IDM_STOPSERVICE, (LPARAM)MAKELONG(TBSTATE_ENABLED, 0));
  76. InsertLogMsg(IDS_STARTSERVICE);
  77. SendMessage(g_hStatusBar, SB_SETTEXT, MAKEWORD(0, 0), (LPARAM)_TEXT("Ready"));
  78. return;
  79. }
  80. case IDM_STOPSERVICE:
  81. {
  82. g_fTerminated = TRUE;
  83. /* if (g_hAcceptThread != INVALID_HANDLE_VALUE)
  84. {
  85. TerminateThread(g_hAcceptThread, 0);
  86. WaitForSingleObject(g_hAcceptThread, INFINITE);
  87. CloseHandle(g_hAcceptThread);
  88. g_hAcceptThread = INVALID_HANDLE_VALUE;
  89. }
  90. if (g_hMsgThread != INVALID_HANDLE_VALUE)
  91. {
  92. TerminateThread(g_hMsgThread, 0);
  93. WaitForSingleObject(g_hMsgThread, INFINITE);
  94. CloseHandle(g_hMsgThread);
  95. g_hMsgThread = INVALID_HANDLE_VALUE;
  96. }
  97. */
  98. ClearSocket(g_ssock);
  99. ClearSocket(g_csock);
  100. CloseHandle(g_hIOCP);
  101. HMENU hMainMenu = GetMenu(g_hMainWnd);
  102. HMENU hMenu = GetSubMenu(hMainMenu, 0);
  103. EnableMenuItem(hMenu, IDM_STARTSERVICE, MF_ENABLED|MF_BYCOMMAND);
  104. EnableMenuItem(hMenu, IDM_STOPSERVICE, MF_GRAYED|MF_BYCOMMAND);
  105. SendMessage(g_hToolBar, TB_SETSTATE, (WPARAM)IDM_STARTSERVICE, (LPARAM)MAKELONG(TBSTATE_ENABLED, 0));
  106. SendMessage(g_hToolBar, TB_SETSTATE, (WPARAM)IDM_STOPSERVICE, (LPARAM)MAKELONG(TBSTATE_INDETERMINATE, 0));
  107. InsertLogMsg(IDS_STOPSERVICE);
  108. SendMessage(g_hStatusBar, SB_SETTEXT, MAKEWORD(0, 0), (LPARAM)_TEXT("Not Ready"));
  109. return;
  110. }
  111. // ORZ:
  112. case IDM_FONTCOLOR:
  113. SetFontColor();
  114. return;
  115. case IDM_BACKCOLOR:
  116. SetBackColor();
  117. return;
  118. case IDM_CONFIG:
  119. {
  120. DialogBox(g_hInst, MAKEINTRESOURCE(IDD_CONFIGDLG), g_hMainWnd, (DLGPROC)ConfigDlgFunc);
  121. return;
  122. }
  123. }
  124. }
  125. // **************************************************************************************
  126. //
  127. //
  128. //
  129. // **************************************************************************************
  130. LPARAM APIENTRY MainWndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  131. {
  132. switch (nMsg)
  133. {
  134. #ifdef _SOCKET_ASYNC_IO
  135. case _IDM_SERVERSOCK_MSG:
  136. return OnServerSockMsg(wParam, lParam);
  137. #endif
  138. case _IDM_CLIENTSOCK_MSG:
  139. return OnClientSockMsg(wParam, lParam);
  140. case WM_COMMAND:
  141. OnCommand(wParam, lParam);
  142. break;
  143. case WM_ERASEBKGND:
  144. return 0; // ORZ:
  145. case WM_SIZE:
  146. {
  147. if (g_hToolBar && g_hMainWnd && g_hStatusBar) 
  148. {
  149. RECT rcToolBar, rcMain, rcStatusBar;
  150. GetWindowRect(g_hToolBar, &rcToolBar);
  151. GetClientRect(g_hMainWnd, &rcMain);
  152. GetWindowRect(g_hStatusBar, &rcStatusBar);
  153. MoveWindow(g_hToolBar, 0, 0, LOWORD(lParam), (rcToolBar.bottom - rcToolBar.top), TRUE);
  154. MoveWindow(g_hStatusBar, 0, rcMain.bottom - (rcStatusBar.bottom - rcStatusBar.top), 
  155. LOWORD(lParam), (rcStatusBar.bottom - rcStatusBar.top), TRUE);
  156. MoveWindow(g_hLogMsgWnd, 0, (rcToolBar.bottom - rcToolBar.top) - 2, (rcMain.right - rcMain.left), 
  157. (rcMain.bottom - rcMain.top) - (rcToolBar.bottom - rcToolBar.top) - (rcStatusBar.bottom - rcStatusBar.top) + 2, 
  158. TRUE);
  159. int nStatusPartsWidths[_NUMOFMAX_STATUS_PARTS];
  160. int nCnt = 0;
  161. for (int i = _NUMOFMAX_STATUS_PARTS - 1; i >= 0 ; i--)
  162. nStatusPartsWidths[nCnt++] = (rcStatusBar.right - rcStatusBar.left) - (90 * i);
  163. SendMessage(g_hStatusBar, SB_SETPARTS, _NUMOFMAX_STATUS_PARTS, (LPARAM)nStatusPartsWidths);
  164. }
  165. break;
  166. }
  167. case WM_CLOSE:
  168. {
  169. TCHAR szMsg[128];
  170. TCHAR szTitle[128];
  171. LoadString(g_hInst, IDS_PROGRAM_QUIT, szMsg, sizeof(szMsg));
  172. LoadString(g_hInst, IDS_PROGRAM_TITLE, szTitle, sizeof(szTitle));
  173. if (MessageBox(g_hMainWnd, szMsg, szTitle, MB_ICONINFORMATION|MB_YESNO) == IDYES)
  174. {
  175. if (SendMessage(g_hToolBar, TB_GETSTATE, (WPARAM)IDM_STARTSERVICE, (LPARAM)0L) == TBSTATE_INDETERMINATE)
  176. OnCommand(IDM_STOPSERVICE, 0L);
  177. WSACleanup();
  178. PostQuitMessage(0);
  179. }
  180. return 0L;
  181. }
  182. }
  183. return (DefWindowProc(hWnd, nMsg, wParam, lParam));
  184. }