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

模拟服务器

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include <stdio.h>
  3. #include "../def/dbmgr.h"
  4. BOOL jRegSetKey(LPCSTR pSubKeyName, LPCSTR pValueName, DWORD dwFlags, LPBYTE pValue, DWORD nValueSize);
  5. BOOL jRegGetKey(LPCSTR pSubKeyName, LPCSTR pValueName, LPBYTE pValue);
  6. BOOL CALLBACK ConfigDlgFunc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
  7. BOOL CALLBACK ServerListProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
  8. LPARAM OnGateCommSockMsg(WPARAM wParam, LPARAM lParam);
  9. BOOL InitServerThreadForMsg();
  10. extern CWHList< GAMESERVERINFO * > g_xGameServerList;
  11. extern CWHList< GATESERVERINFO * > g_xGateServerList;
  12. extern char g_szServerList[1024];
  13. extern HINSTANCE g_hInst;
  14. extern HWND g_hMainWnd;
  15. extern HWND g_hLogMsgWnd;
  16. extern HWND g_hToolBar;
  17. extern HWND g_hStatusBar;
  18. extern unsigned long g_hThreadForMsg;
  19. SOCKET g_gcSock = INVALID_SOCKET;
  20. SOCKADDR_IN g_gcAddr;
  21. BOOL g_fTerminated = FALSE;
  22. void SwitchMenuItem(BOOL fFlag)
  23. {
  24. HMENU hMainMenu = GetMenu(g_hMainWnd);
  25. HMENU hMenu = GetSubMenu(hMainMenu, 0);
  26. if (fFlag)
  27. {
  28. EnableMenuItem(hMenu, IDM_STARTSERVICE, MF_GRAYED|MF_BYCOMMAND);
  29. EnableMenuItem(hMenu, IDM_STOPSERVICE, MF_ENABLED|MF_BYCOMMAND);
  30. SendMessage(g_hToolBar, TB_SETSTATE, (WPARAM)IDM_STARTSERVICE, (LPARAM)MAKELONG(TBSTATE_INDETERMINATE, 0));
  31. SendMessage(g_hToolBar, TB_SETSTATE, (WPARAM)IDM_STOPSERVICE, (LPARAM)MAKELONG(TBSTATE_ENABLED, 0));
  32. InsertLogMsg(IDS_STARTSERVICE);
  33. }
  34. else
  35. {
  36. EnableMenuItem(hMenu, IDM_STARTSERVICE, MF_ENABLED|MF_BYCOMMAND);
  37. EnableMenuItem(hMenu, IDM_STOPSERVICE, MF_GRAYED|MF_BYCOMMAND);
  38. SendMessage(g_hToolBar, TB_SETSTATE, (WPARAM)IDM_STARTSERVICE, (LPARAM)MAKELONG(TBSTATE_ENABLED, 0));
  39. SendMessage(g_hToolBar, TB_SETSTATE, (WPARAM)IDM_STOPSERVICE, (LPARAM)MAKELONG(TBSTATE_INDETERMINATE, 0));
  40. InsertLogMsg(IDS_STOPSERVICE);
  41. }
  42. }
  43. void CreateConfigProperties()
  44. {
  45. TCHAR szText[64];
  46. PROPSHEETPAGE psp[1];
  47. PROPSHEETHEADER psh;
  48. LoadString((HINSTANCE)g_hInst, IDS_TAB_LABEL1, szText, sizeof(szText));
  49. psp[0].dwSize = sizeof(PROPSHEETPAGE);
  50. psp[0].dwFlags = 0; //PSP_USETITLE;
  51. psp[0].hInstance = g_hInst;
  52. psp[0].pszTemplate = MAKEINTRESOURCE(IDD_CONFIGDLG_SERVERLIST);
  53. psp[0].pszIcon = NULL;
  54. psp[0].pfnDlgProc = ServerListProc;
  55. psp[0].pszTitle = szText;
  56. psp[0].lParam = 0;
  57. psh.dwSize = sizeof(PROPSHEETHEADER);
  58. psh.dwFlags = PSH_PROPSHEETPAGE | PSH_NOAPPLYNOW;
  59. psh.hwndParent = g_hMainWnd;
  60. psh.hInstance = g_hInst;
  61. psh.pszIcon = NULL;
  62. psh.pszCaption = (LPTSTR)_T("Configuration");
  63. psh.nPages = sizeof(psp) / sizeof(PROPSHEETPAGE);
  64. psh.ppsp = (LPCPROPSHEETPAGE)&psp;
  65. PropertySheet(&psh);
  66. }
  67. UINT WINAPI LoadAccountRecords(LPVOID lpParameter)
  68. {
  69. InsertLogMsg(IDS_LOADACCOUNTRECORDS);
  70. CRecordset *pRec = GetDBManager()->CreateRecordset();
  71. pRec->Execute( "UPDATE TBL_ACCOUNT SET FLD_CERTIFICATION=0 WHERE FLD_CERTIFICATION >= 30" );
  72. GetDBManager()->DestroyRecordset( pRec );
  73. // ----------------------------------------------------------------------------------------
  74. GAMESERVERINFO *pServerInfo;
  75. pRec = GetDBManager()->CreateRecordset();
  76. if ( pRec->Execute( "SELECT * FROM TBL_SERVERINFO" ) )
  77. {
  78. while ( pRec->Fetch() )
  79. {
  80. pServerInfo = new GAMESERVERINFO;
  81. if ( !pServerInfo )
  82. break;
  83. pServerInfo->index = atoi( pRec->Get( "FLD_SERVERIDX" ) );
  84. strcpy( pServerInfo->name, pRec->Get( "FLD_SERVERNAME" ) );
  85. strcpy( pServerInfo->ip,   pRec->Get( "FLD_SERVERIP" ) );
  86. pServerInfo->connCnt = 0;
  87. g_xGameServerList.AddNewNode( pServerInfo );
  88. }
  89. }
  90. GetDBManager()->DestroyRecordset( pRec );
  91. char szTmp[64];
  92. for ( PLISTNODE pNode = g_xGameServerList.GetHead(); pNode; pNode = g_xGameServerList.GetNext( pNode ) )
  93. {
  94. pServerInfo = g_xGameServerList.GetData( pNode );
  95. sprintf( szTmp, "%d,%s,", pServerInfo->index, pServerInfo->name );
  96. strcat( g_szServerList, szTmp );
  97. }
  98. // ----------------------------------------------------------------------------------------
  99. InitServerThreadForMsg();
  100. if (InitServerSocket(g_gcSock, &g_gcAddr, _IDM_GATECOMMSOCK_MSG, 5500, 1))
  101. SwitchMenuItem(TRUE);
  102. return 0L;
  103. }
  104. void SetFontColor()
  105. {
  106. CHOOSECOLOR color;
  107. static COLORREF acrCustClr[16]; 
  108. memset( &color, 0, sizeof( color ) );
  109. color.hwndOwner = g_hMainWnd;
  110. color.lStructSize = sizeof( color );
  111. color.Flags = CC_FULLOPEN | CC_RGBINIT;
  112. color.rgbResult = ListView_GetTextColor( g_hLogMsgWnd );
  113. color.lpCustColors = acrCustClr;
  114. if ( !ChooseColor( &color ) )
  115. return;
  116. ListView_SetTextColor( g_hLogMsgWnd, color.rgbResult );
  117. InvalidateRect( g_hLogMsgWnd, NULL, TRUE );
  118. }
  119. void SetBackColor()
  120. {
  121. CHOOSECOLOR color;
  122. static COLORREF acrCustClr[16]; 
  123. memset( &color, 0, sizeof( color ) );
  124. color.hwndOwner = g_hMainWnd;
  125. color.lStructSize = sizeof( color );
  126. color.Flags = CC_FULLOPEN | CC_RGBINIT;
  127. color.rgbResult = ListView_GetBkColor( g_hLogMsgWnd );
  128. color.lpCustColors = acrCustClr;
  129. if ( !ChooseColor( &color ) )
  130. return;
  131. ListView_SetTextBkColor( g_hLogMsgWnd, color.rgbResult );
  132. ListView_SetBkColor( g_hLogMsgWnd, color.rgbResult );
  133. InvalidateRect( g_hLogMsgWnd, NULL, TRUE );
  134. }
  135. void OnCommand(WPARAM wParam, LPARAM lParam)
  136. {
  137. switch (LOWORD(wParam))
  138. {
  139. case IDM_STARTSERVICE:
  140. {
  141. g_fTerminated = FALSE;
  142. UINT dwThreadIDForMsg = 0;
  143. unsigned long hThreadForMsg = 0;
  144. hThreadForMsg = _beginthreadex(NULL, 0, LoadAccountRecords, NULL, 0, &dwThreadIDForMsg);
  145. return;
  146. }
  147. case IDM_STOPSERVICE:
  148. {
  149. ClearSocket(g_gcSock);
  150. SwitchMenuItem(FALSE);
  151. return;
  152. }
  153. case IDM_FONTCOLOR:
  154. SetFontColor();
  155. return;
  156. case IDM_BACKCOLOR:
  157. SetBackColor();
  158. return;
  159. case IDM_CONFIG:
  160. {
  161. CreateConfigProperties();
  162. return;
  163. }
  164. }
  165. }
  166. // **************************************************************************************
  167. //
  168. //
  169. //
  170. // **************************************************************************************
  171. LPARAM APIENTRY MainWndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  172. {
  173. switch (nMsg)
  174. {
  175. case WM_COMMAND:
  176. OnCommand(wParam, lParam);
  177. break;
  178. case WM_ERASEBKGND:
  179. return 0; // ORZ: 濒糊芭府绰 巴 力芭
  180. case WM_SIZE:
  181. {
  182. if (g_hToolBar && g_hMainWnd && g_hStatusBar) 
  183. {
  184. RECT rcToolBar, rcMain, rcStatusBar;
  185. GetWindowRect(g_hToolBar, &rcToolBar);
  186. GetClientRect(g_hMainWnd, &rcMain);
  187. GetWindowRect(g_hStatusBar, &rcStatusBar);
  188. MoveWindow(g_hToolBar, 0, 0, LOWORD(lParam), (rcToolBar.bottom - rcToolBar.top), TRUE);
  189. MoveWindow(g_hStatusBar, 0, rcMain.bottom - (rcStatusBar.bottom - rcStatusBar.top), 
  190. LOWORD(lParam), (rcStatusBar.bottom - rcStatusBar.top), TRUE);
  191. MoveWindow(g_hLogMsgWnd, 0, (rcToolBar.bottom - rcToolBar.top) - 2, (rcMain.right - rcMain.left), 
  192. (rcMain.bottom - rcMain.top) - (rcToolBar.bottom - rcToolBar.top) - (rcStatusBar.bottom - rcStatusBar.top) + 2, 
  193. TRUE);
  194. int nStatusPartsWidths[_NUMOFMAX_STATUS_PARTS];
  195. int nCnt = 0;
  196. for (int i = _NUMOFMAX_STATUS_PARTS - 1; i >= 0 ; i--)
  197. nStatusPartsWidths[nCnt++] = (rcStatusBar.right - rcStatusBar.left) - (90 * i);
  198. SendMessage(g_hStatusBar, SB_SETPARTS, _NUMOFMAX_STATUS_PARTS, (LPARAM)nStatusPartsWidths);
  199. }
  200. break;
  201. }
  202. case WM_CLOSE:
  203. {
  204. TCHAR szMsg[128];
  205. TCHAR szTitle[128];
  206. LoadString(g_hInst, IDS_PROGRAM_QUIT, szMsg, sizeof(szMsg));
  207. LoadString(g_hInst, IDS_PROGRAM_TITLE, szTitle, sizeof(szTitle));
  208. if (MessageBox(g_hMainWnd, szMsg, szTitle, MB_ICONINFORMATION|MB_YESNO) == IDYES)
  209. {
  210. // ClearServerSocket();
  211. // DisconnectFromServer();
  212. OnCommand(IDM_STOPSERVICE, 0L);
  213. CoUninitialize();
  214. WSACleanup();
  215. PostQuitMessage(0);
  216. }
  217. return 0L;
  218. }
  219. }
  220. return (DefWindowProc(hWnd, nMsg, wParam, lParam));
  221. }