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