ServerDlg.cpp
上传用户:maryhy001
上传日期:2007-05-02
资源大小:2317k
文件大小:8k
源码类别:

网格计算

开发平台:

Visual C++

  1. // ServerDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "Server.h"
  5. #include "ServerDlg.h"
  6. #include "netaddress.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. // CAboutDlg dialog used for App About
  13. class CAboutDlg : public CDialog
  14. {
  15. public:
  16. CAboutDlg();
  17. // Dialog Data
  18. //{{AFX_DATA(CAboutDlg)
  19. enum { IDD = IDD_ABOUTBOX };
  20. //}}AFX_DATA
  21. // ClassWizard generated virtual function overrides
  22. //{{AFX_VIRTUAL(CAboutDlg)
  23. protected:
  24. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  25. //}}AFX_VIRTUAL
  26. // Implementation
  27. protected:
  28. //{{AFX_MSG(CAboutDlg)
  29. //}}AFX_MSG
  30. DECLARE_MESSAGE_MAP()
  31. };
  32. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  33. {
  34. //{{AFX_DATA_INIT(CAboutDlg)
  35. //}}AFX_DATA_INIT
  36. }
  37. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  38. {
  39. CDialog::DoDataExchange(pDX);
  40. //{{AFX_DATA_MAP(CAboutDlg)
  41. //}}AFX_DATA_MAP
  42. }
  43. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  44. //{{AFX_MSG_MAP(CAboutDlg)
  45. // No message handlers
  46. //}}AFX_MSG_MAP
  47. END_MESSAGE_MAP()
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CServerDlg dialog
  50. CServerDlg::CServerDlg(CWnd* pParent /*=NULL*/)
  51. : CDialog(CServerDlg::IDD, pParent)
  52. {
  53. //{{AFX_DATA_INIT(CServerDlg)
  54. m_slocalip = _T("");
  55. m_nlistenport = 0;
  56. //}}AFX_DATA_INIT
  57. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  58. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  59. }
  60. void CServerDlg::DoDataExchange(CDataExchange* pDX)
  61. {
  62. CDialog::DoDataExchange(pDX);
  63. //{{AFX_DATA_MAP(CServerDlg)
  64. DDX_Control(pDX, ID_CLOSEDIRSVR, m_btnCloseSvr);
  65. DDX_Control(pDX, ID_OPENDIRSVR, m_btnOpenSvr);
  66. DDX_Text(pDX, IDC_EDTLOCALIP, m_slocalip);
  67. DDV_MaxChars(pDX, m_slocalip, 32);
  68. DDX_Text(pDX, IDC_EDTSVRPORT, m_nlistenport);
  69. DDV_MinMaxUInt(pDX, m_nlistenport, 5555, 10000);
  70. //}}AFX_DATA_MAP
  71. }
  72. BEGIN_MESSAGE_MAP(CServerDlg, CDialog)
  73. //{{AFX_MSG_MAP(CServerDlg)
  74. ON_WM_SYSCOMMAND()
  75. ON_WM_PAINT()
  76. ON_WM_QUERYDRAGICON()
  77. ON_WM_DESTROY()
  78. ON_BN_CLICKED(ID_OPENDIRSVR, OnOpendirsvr)
  79. ON_BN_CLICKED(ID_CLOSEDIRSVR, OnClosedirsvr)
  80. ON_BN_CLICKED(IDC_LOCALSETTING, OnLocalsetting)
  81. ON_WM_TIMER()
  82. ON_COMMAND(IDM_TISHOWWND, OnTishowwnd)
  83. ON_COMMAND(IDM_TIEXIT, OnTiexit)
  84. ON_COMMAND(IDM_TIABOUT, OnTiabout)
  85. ON_COMMAND(IDM_TISTARTSVR, OnTistartsvr)
  86. ON_COMMAND(IDM_TICLOSESVR, OnTiclosesvr)
  87. //}}AFX_MSG_MAP
  88. ON_MESSAGE(UWM_TRAYICON, OnTrayNotification)
  89. END_MESSAGE_MAP()
  90. /////////////////////////////////////////////////////////////////////////////
  91. // CServerDlg message handlers
  92. BOOL CServerDlg::OnInitDialog()
  93. {
  94. CDialog::OnInitDialog();
  95. // Add "About..." menu item to system menu.
  96. // IDM_ABOUTBOX must be in the system command range.
  97. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  98. ASSERT(IDM_ABOUTBOX < 0xF000);
  99. CMenu* pSysMenu = GetSystemMenu(FALSE);
  100. if (pSysMenu != NULL)
  101. {
  102. CString strAboutMenu;
  103. strAboutMenu.LoadString(IDS_ABOUTBOX);
  104. if (!strAboutMenu.IsEmpty())
  105. {
  106. pSysMenu->AppendMenu(MF_SEPARATOR);
  107. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  108. }
  109. }
  110. // Set the icon for this dialog.  The framework does this automatically
  111. //  when the application's main window is not a dialog
  112. SetIcon(m_hIcon, TRUE); // Set big icon
  113. SetIcon(m_hIcon, FALSE); // Set small icon
  114. if(!this->m_otrayicon.Create(this->GetSafeHwnd(), GLOBAL_TRAYICON_ID, 
  115. AfxGetApp()->LoadIcon(IDI_SVRSTOP), "trfServer服务器", UWM_TRAYICON))
  116. {
  117. return FALSE;
  118. }
  119. this->m_otrayicon.SetMenu(IDR_TRAYICON);
  120. EnableMenuItem(this->m_otrayicon.GetPopMenu(), IDM_TISTARTSVR, FALSE | MF_BYCOMMAND);
  121. EnableMenuItem(this->m_otrayicon.GetPopMenu(), IDM_TICLOSESVR, TRUE  | MF_BYCOMMAND);
  122. m_btnOpenSvr.EnableWindow(TRUE);
  123. m_btnCloseSvr.EnableWindow(FALSE);
  124. this->m_slocalip.Format("%s / %s", 
  125. CXNetAddress::GetLocalHostName().data(),
  126. CXNetAddress::GetLocalHostIp().data());
  127. this->m_nlistenport = SVR_DIRPORT;
  128. theApp.m_syslog.Print(LL_INFO, "CServerDlg::OnInitDialog : 本机标识[%s].n", this->m_slocalip.GetBuffer(0));
  129. UpdateData(FALSE);
  130. this->SetTimer(100, 60*1000, NULL);
  131. this->SetTimer(110, 500, NULL);
  132. this->SetTimer(111, 3000, NULL);
  133. return TRUE;  // return TRUE  unless you set the focus to a control
  134. }
  135. void CServerDlg::OnSysCommand(UINT nID, LPARAM lParam)
  136. {
  137. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  138. {
  139. CAboutDlg dlgAbout;
  140. dlgAbout.DoModal();
  141. }
  142. else
  143. {
  144. CDialog::OnSysCommand(nID, lParam);
  145. }
  146. }
  147. // If you add a minimize button to your dialog, you will need the code below
  148. //  to draw the icon.  For MFC applications using the document/view model,
  149. //  this is automatically done for you by the framework.
  150. void CServerDlg::OnPaint() 
  151. {
  152. if (IsIconic())
  153. {
  154. CPaintDC dc(this); // device context for painting
  155. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  156. // Center icon in client rectangle
  157. int cxIcon = GetSystemMetrics(SM_CXICON);
  158. int cyIcon = GetSystemMetrics(SM_CYICON);
  159. CRect rect;
  160. GetClientRect(&rect);
  161. int x = (rect.Width() - cxIcon + 1) / 2;
  162. int y = (rect.Height() - cyIcon + 1) / 2;
  163. // Draw the icon
  164. dc.DrawIcon(x, y, m_hIcon);
  165. }
  166. else
  167. {
  168. CDialog::OnPaint();
  169. }
  170. }
  171. // The system calls this to obtain the cursor to display while the user drags
  172. //  the minimized window.
  173. HCURSOR CServerDlg::OnQueryDragIcon()
  174. {
  175. return (HCURSOR) m_hIcon;
  176. }
  177. void CServerDlg::OnDestroy() 
  178. {
  179. m_service.Stop();
  180. CDialog::OnDestroy();
  181. }
  182. void CServerDlg::OnOpendirsvr() 
  183. {
  184. UpdateData(TRUE);
  185. if(m_service.Start(this->m_nlistenport))
  186. {
  187. m_btnOpenSvr.EnableWindow(FALSE);
  188. m_btnCloseSvr.EnableWindow(TRUE);
  189. CWnd *pwnd = this->GetDlgItem(IDC_EDTSVRPORT);
  190. pwnd->EnableWindow(FALSE);
  191. this->m_otrayicon.ModifyIcon(IDR_MAINFRAME);
  192. EnableMenuItem(this->m_otrayicon.GetPopMenu(), IDM_TISTARTSVR, TRUE | MF_BYCOMMAND);
  193. EnableMenuItem(this->m_otrayicon.GetPopMenu(), IDM_TICLOSESVR, FALSE| MF_BYCOMMAND);
  194. }
  195. }
  196. void CServerDlg::OnClosedirsvr() 
  197. {
  198. m_service.Stop();
  199. {
  200. m_btnOpenSvr.EnableWindow(TRUE);
  201. m_btnCloseSvr.EnableWindow(FALSE);
  202. CWnd *pwnd = this->GetDlgItem(IDC_EDTSVRPORT);
  203. pwnd->EnableWindow(TRUE);
  204. this->m_otrayicon.ModifyIcon(IDI_SVRSTOP);
  205. EnableMenuItem(this->m_otrayicon.GetPopMenu(), IDM_TISTARTSVR, FALSE| MF_BYCOMMAND);
  206. EnableMenuItem(this->m_otrayicon.GetPopMenu(), IDM_TICLOSESVR, TRUE | MF_BYCOMMAND);
  207. }
  208. }
  209. void CServerDlg::OnLocalsetting() 
  210. {
  211. this->m_otrayicon.SetActive(TRUE);
  212. }
  213. void CServerDlg::OnTimer(UINT nIDEvent) 
  214. {
  215. if(100 == nIDEvent)
  216. {
  217. string strtip = CXNetAddress::GetLocalHostName() + " / ";
  218. strtip = strtip + CXNetAddress::GetLocalHostIp();
  219. this->m_slocalip = strtip.data();
  220. this->m_otrayicon.ModifyTipInfo(this->m_slocalip.GetBuffer(0));
  221. UpdateData(FALSE);
  222. }
  223. else if(110 == nIDEvent)
  224. {
  225. this->OnLocalsetting();
  226. this->OnOpendirsvr();
  227. this->KillTimer(110);
  228. }
  229. else if(111 == nIDEvent)
  230. {
  231. this->m_otrayicon.Refresh();
  232. }
  233. CDialog::OnTimer(nIDEvent);
  234. }
  235. LRESULT CServerDlg::OnTrayNotification(WPARAM wparam, LPARAM lparam)
  236. {
  237. if ( wparam == GLOBAL_TRAYICON_ID )
  238. {
  239. if ( lparam == WM_RBUTTONUP )
  240. {
  241. m_otrayicon.PopupMenu();
  242. }
  243. else
  244. {
  245. if ( lparam == WM_LBUTTONDBLCLK )
  246. {
  247. if ( ::IsWindowVisible(m_otrayicon.GetOwnerWnd()) )
  248. {
  249. m_otrayicon.SetActive();
  250. ::ShowWindow(m_otrayicon.GetOwnerWnd(), SW_HIDE);
  251. }
  252. else
  253. {
  254. m_otrayicon.SetActive(FALSE);
  255. ::ShowWindow(m_otrayicon.GetOwnerWnd(), SW_SHOW);
  256. }
  257. }
  258. }
  259. }
  260. return 1;
  261. }
  262. void CServerDlg::OnTishowwnd() 
  263. {
  264. this->m_otrayicon.SetActive(FALSE);
  265. }
  266. void CServerDlg::OnTiexit() 
  267. {
  268. this->OnClosedirsvr();
  269. PostMessage(WM_CLOSE, 0, 0);
  270. }
  271. void CServerDlg::OnTiabout() 
  272. {
  273. CAboutDlg about;
  274. about.DoModal();
  275. }
  276. void CServerDlg::OnTistartsvr() 
  277. {
  278. this->OnOpendirsvr();
  279. }
  280. void CServerDlg::OnTiclosesvr() 
  281. {
  282. this->OnClosedirsvr();
  283. }