POPCheckerDlg.cpp
上传用户:huaqiang
上传日期:2007-01-03
资源大小:19k
文件大小:7k
源码类别:

Email客户端

开发平台:

Visual C++

  1. // POPCheckerDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "POPChecker.h"
  5. #include "POPCheckerDlg.h"
  6. //#include "Pop.h"
  7. #include "TrayIcon.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. #define WM_ICON_NOTIFY  WM_USER+10
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CAboutDlg dialog used for App About
  16. class CAboutDlg : public CDialog
  17. {
  18. public:
  19. CAboutDlg();
  20. // Dialog Data
  21. //{{AFX_DATA(CAboutDlg)
  22. enum { IDD = IDD_ABOUTBOX };
  23. //}}AFX_DATA
  24. // ClassWizard generated virtual function overrides
  25. //{{AFX_VIRTUAL(CAboutDlg)
  26. protected:
  27. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  28. //}}AFX_VIRTUAL
  29. // Implementation
  30. protected:
  31. //{{AFX_MSG(CAboutDlg)
  32. //}}AFX_MSG
  33. DECLARE_MESSAGE_MAP()
  34. };
  35. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  36. {
  37. //{{AFX_DATA_INIT(CAboutDlg)
  38. //}}AFX_DATA_INIT
  39. }
  40. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  41. {
  42. CDialog::DoDataExchange(pDX);
  43. //{{AFX_DATA_MAP(CAboutDlg)
  44. //}}AFX_DATA_MAP
  45. }
  46. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  47. //{{AFX_MSG_MAP(CAboutDlg)
  48. // No message handlers
  49. //}}AFX_MSG_MAP
  50. END_MESSAGE_MAP()
  51. /////////////////////////////////////////////////////////////////////////////
  52. // CPOPCheckerDlg dialog
  53. CPOPCheckerDlg::CPOPCheckerDlg(CWnd* pParent /*=NULL*/)
  54. : CDialog(CPOPCheckerDlg::IDD, pParent)
  55. {
  56. //{{AFX_DATA_INIT(CPOPCheckerDlg)
  57. m_Duration = 0;
  58. m_NoMail = 0;
  59. m_Password = _T("");
  60. m_POPServer = _T("");
  61. m_UserName = _T("");
  62. //}}AFX_DATA_INIT
  63. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  64. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  65. }
  66. void CPOPCheckerDlg::DoDataExchange(CDataExchange* pDX)
  67. {
  68. CDialog::DoDataExchange(pDX);
  69. //{{AFX_DATA_MAP(CPOPCheckerDlg)
  70. DDX_Text(pDX, IDC_DURATION, m_Duration);
  71. DDX_Text(pDX, IDC_NO_MAIL, m_NoMail);
  72. DDX_Text(pDX, IDC_PASSWORD, m_Password);
  73. DDX_Text(pDX, IDC_POPSERVER, m_POPServer);
  74. DDX_Text(pDX, IDC_USERNAME, m_UserName);
  75. //}}AFX_DATA_MAP
  76. }
  77. BEGIN_MESSAGE_MAP(CPOPCheckerDlg, CDialog)
  78. //{{AFX_MSG_MAP(CPOPCheckerDlg)
  79. ON_WM_SYSCOMMAND()
  80. ON_WM_PAINT()
  81. ON_WM_QUERYDRAGICON()
  82. ON_BN_CLICKED(ID_START, OnStart)
  83. ON_WM_TIMER()
  84. ON_COMMAND(ID_CONFIGURATION, OnConfiguration)
  85. ON_MESSAGE(WM_ICON_NOTIFY, OnTrayNotification)
  86. ON_COMMAND(ID_EXIT, OnExit)
  87. //}}AFX_MSG_MAP
  88. END_MESSAGE_MAP()
  89. /////////////////////////////////////////////////////////////////////////////
  90. // CPOPCheckerDlg message handlers
  91. BOOL CPOPCheckerDlg::OnInitDialog()
  92. {
  93. CDialog::OnInitDialog();
  94. // Add "About..." menu item to system menu.
  95. // IDM_ABOUTBOX must be in the system command range.
  96. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  97. ASSERT(IDM_ABOUTBOX < 0xF000);
  98. CMenu* pSysMenu = GetSystemMenu(FALSE);
  99. if (pSysMenu != NULL)
  100. {
  101. CString strAboutMenu;
  102. strAboutMenu.LoadString(IDS_ABOUTBOX);
  103. if (!strAboutMenu.IsEmpty())
  104. {
  105. pSysMenu->AppendMenu(MF_SEPARATOR);
  106. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  107. }
  108. }
  109. // Set the icon for this dialog.  The framework does this automatically
  110. //  when the application's main window is not a dialog
  111. SetIcon(m_hIcon, TRUE); // Set big icon
  112. SetIcon(m_hIcon, FALSE); // Set small icon
  113. // TODO: Add extra initialization here
  114. m_Error = 0;
  115. return TRUE;  // return TRUE  unless you set the focus to a control
  116. }
  117. void CPOPCheckerDlg::OnSysCommand(UINT nID, LPARAM lParam)
  118. {
  119. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  120. {
  121. CAboutDlg dlgAbout;
  122. dlgAbout.DoModal();
  123. }
  124. else
  125. {
  126. CDialog::OnSysCommand(nID, lParam);
  127. }
  128. }
  129. // If you add a minimize button to your dialog, you will need the code below
  130. //  to draw the icon.  For MFC applications using the document/view model,
  131. //  this is automatically done for you by the framework.
  132. void CPOPCheckerDlg::OnPaint() 
  133. {
  134. if (IsIconic())
  135. {
  136. CPaintDC dc(this); // device context for painting
  137. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  138. // Center icon in client rectangle
  139. int cxIcon = GetSystemMetrics(SM_CXICON);
  140. int cyIcon = GetSystemMetrics(SM_CYICON);
  141. CRect rect;
  142. GetClientRect(&rect);
  143. int x = (rect.Width() - cxIcon + 1) / 2;
  144. int y = (rect.Height() - cyIcon + 1) / 2;
  145. // Draw the icon
  146. dc.DrawIcon(x, y, m_hIcon);
  147. }
  148. else
  149. {
  150. CDialog::OnPaint();
  151. }
  152. }
  153. // The system calls this to obtain the cursor to display while the user drags
  154. //  the minimized window.
  155. HCURSOR CPOPCheckerDlg::OnQueryDragIcon()
  156. {
  157. return (HCURSOR) m_hIcon;
  158. }
  159. void CPOPCheckerDlg::OnStart() 
  160. {
  161. m_Error = 0;
  162. UpdateData();
  163. if(m_UserName.IsEmpty()!=0)
  164. MessageBox("User Name is not Entered ....");
  165. else  
  166. if(m_Password.IsEmpty()!=0)
  167. MessageBox("Password is not Entered ....");
  168. else
  169. if(m_POPServer.IsEmpty()!=0)
  170. MessageBox("POPServer is not Entered ....");
  171. else
  172. {
  173. HICON hIcon;
  174. hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  175. m_TrayIcon.Create(this, WM_ICON_NOTIFY, "Mail Checker", 
  176.   hIcon, IDR_POPUP_MENU);
  177. ShowWindow(SW_HIDE);
  178. SetTimer(1, m_Duration*1000, NULL);
  179. }
  180. }
  181. void CPOPCheckerDlg::OnTimer(UINT nIDEvent) 
  182. {
  183. CPop PopServer;
  184. switch(nIDEvent)
  185. {
  186. case 0:
  187. KillTimer(0);
  188. Beep(800*1,500); // produce Beep sound 
  189. Beep(800*2,500); // produce Beep sound 
  190. SetTimer(0, m_Duration*1000, NULL);
  191. break;
  192. case 1:
  193. KillTimer(1);
  194. if(PopServer.Connect(m_POPServer,m_UserName,m_Password)==FALSE)
  195. {
  196. Beep(800*1,500); // produce Beep sound 
  197. Beep(800*2,500); // produce Beep sound 
  198. SetTimer(0, m_Duration*1000, NULL);
  199. m_TrayIcon.SetTooltipText("Mail Checker: POP server cannot be connected ...");
  200. m_Error = 1;
  201. }
  202. else
  203. if(PopServer.Statistics()==FALSE)
  204. {
  205. Beep(32767,1000); // produce Beep sound 
  206. SetTimer(0, m_Duration*1000, NULL);
  207. m_Error = 2;
  208. }
  209. else
  210. {
  211. m_NoMail = PopServer.GetNumberOfMails();
  212. UpdateData(FALSE);
  213. CString temp;
  214. temp.Format("Mail Checker: You have Mail = %d ",m_NoMail);
  215. m_TrayIcon.SetTooltipText((LPCTSTR)temp);
  216. SetTimer(1, m_Duration*1000, NULL);
  217. }
  218. break;
  219. }
  220. }
  221. LRESULT CPOPCheckerDlg::OnTrayNotification(WPARAM wParam, LPARAM lParam)
  222. {
  223.     // Delegate all the work back to the default implementation in CTrayIcon.
  224.     return m_TrayIcon.OnTrayNotification(wParam, lParam);
  225. }
  226. void CPOPCheckerDlg::OnConfiguration() 
  227. {
  228. ShowWindow(SW_SHOWNORMAL);
  229. m_TrayIcon.RemoveIcon();
  230. if(m_Error>0)
  231. {
  232. KillTimer(0);
  233. switch (m_Error)
  234. {
  235. case 1: MessageBox("POP Server cannot be connected ...");break;
  236. case 2: MessageBox("Mail information cannot be retrived ...");break;
  237. }
  238. }
  239. else
  240. KillTimer(1);
  241. }
  242. void CPOPCheckerDlg::OnExit() 
  243. {
  244. KillTimer(0);
  245. m_TrayIcon.RemoveIcon();
  246. CDialog::OnCancel();
  247. }