HJHomeworkDlg.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // HJHomeworkDlg.cpp : 实现文件
  2. //
  3. #include "stdafx.h"
  4. #include "HJHomework.h"
  5. #include "HJHomeworkDlg.h"
  6. #include "afxdialogex.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #endif
  10. // CHJHomeworkDlg 对话框
  11. #define WM_TO_TRAY_MSG (WM_USER + 2)
  12. CHJHomeworkDlg::CHJHomeworkDlg(CWnd* pParent /*=NULL*/)
  13. : CDialogEx(CHJHomeworkDlg::IDD, pParent)
  14. {
  15. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  16. m_hFontMessage = CreateFont(-12, 0, 0, 0, FW_THIN,
  17. 0, 0, 0, DEFAULT_CHARSET, OUT_CHARACTER_PRECIS,
  18. CLIP_CHARACTER_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH |
  19. FF_DONTCARE, L"微软雅黑");
  20. m_bMonitor = FALSE;
  21. }
  22. void CHJHomeworkDlg::DoDataExchange(CDataExchange* pDX)
  23. {
  24. CDialogEx::DoDataExchange(pDX);
  25. }
  26. BEGIN_MESSAGE_MAP(CHJHomeworkDlg, CDialogEx)
  27. ON_WM_PAINT()
  28. ON_WM_QUERYDRAGICON()
  29. ON_WM_CLOSE()
  30. ON_WM_COPYDATA()
  31. END_MESSAGE_MAP()
  32. BOOL CHJHomeworkDlg::OnInitDialog()
  33. {
  34. CDialogEx::OnInitDialog();
  35. m_unZipManager.SetMainHwnd(GetSafeHwnd(), PirntLog);
  36. SetIcon(m_hIcon, TRUE);
  37. SetIcon(m_hIcon, FALSE);
  38. m_combo_folder.Create(this, IDC_COMBO_FOLDER, CPoint(20, 20), CSize(310, 36), L"请选择图片目录", NULL, COMBOBOX_FOLDER);
  39. m_combo_folder.ShowWindow(SW_SHOW);
  40. m_btn_monitor.Create(this, IDC_BUTTON_MONITOR, CPoint(350, 20), CSize(115, 36), BUTTON_TYPE_TEXT, L"开始监控");
  41. m_btn_monitor.ShowWindow(SW_SHOW);
  42. m_btn_hide.Create(this, IDC_BUTTON_HIDE, CPoint(474, 20), CSize(65, 36), BUTTON_TYPE_TEXT, L"隐藏", RGB(0x99, 0x99, 0x99));
  43. m_btn_monitor.ShowWindow(SW_SHOW);
  44. m_edit_message.Create(WS_CHILD | ES_MULTILINE | ES_AUTOVSCROLL | WS_VSCROLL | WS_VISIBLE | WS_BORDER, CRect(20, 70, 538, 330), this, 5003);
  45. m_edit_message.SetFont(CFont::FromHandle(m_hFontMessage));
  46. m_edit_message.SetReadOnly(TRUE);
  47. m_edit_message.HideCaret();
  48. m_edit_message.SetBackgroundColor(FALSE, RGB(234, 234, 234));
  49. RegisterNotify();
  50. //m_edit_message.SetWindowText(m_strInfo.c_str());
  51. InitSharePath();
  52. for (int i = 0; i < __argc; i++)
  53. {
  54. CString pszParam = __targv[i];
  55. if (pszParam.Find(L"autoMonitor")>-1)
  56. {
  57. OnMonitor();
  58. }
  59. }
  60. return TRUE;
  61. }
  62. BOOL CHJHomeworkDlg::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct)
  63. {
  64. if (pCopyDataStruct && pCopyDataStruct->lpData && pCopyDataStruct->cbData > 0)
  65. {
  66. if (m_unZipManager.OnCopyData(pWnd, pCopyDataStruct))
  67. {
  68. return true;
  69. }
  70. }
  71. return true;
  72. }
  73. void CHJHomeworkDlg::PirntLog(const wchar_t* pszLog)
  74. {
  75. CTime tm = CTime::GetCurrentTime();
  76. CString strTime = tm.Format(L"%Y-%m-%d %H:%M:%S");
  77. TCHAR szText[1024] = { 0 };
  78. _stprintf(szText, L"%s %s\r", strTime, pszLog);
  79. CHJHomeworkDlg* dlg = (CHJHomeworkDlg*)theApp.m_pMainWnd;
  80. dlg->m_edit_message.SetSel(-1, -1);
  81. dlg->m_edit_message.ReplaceSel(szText); //追加显示的内容
  82. }
  83. void CHJHomeworkDlg::InitSharePath()
  84. {
  85. WCHAR share_path[MAX_PATH] = { 0 };
  86. GetPrivateProfileString(_T("USER"), _T("share_path"), _T(""), share_path, MAX_PATH, m_unZipManager.m_strConfigPath.c_str());
  87. m_combo_folder.SetWindowText(share_path);
  88. }
  89. void CHJHomeworkDlg::OnPaint()
  90. {
  91. if (IsIconic())
  92. {
  93. CPaintDC dc(this); // 用于绘制的设备上下文
  94. SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
  95. // 使图标在工作区矩形中居中
  96. int cxIcon = GetSystemMetrics(SM_CXICON);
  97. int cyIcon = GetSystemMetrics(SM_CYICON);
  98. CRect rect;
  99. GetClientRect(&rect);
  100. int x = (rect.Width() - cxIcon + 1) / 2;
  101. int y = (rect.Height() - cyIcon + 1) / 2;
  102. // 绘制图标
  103. dc.DrawIcon(x, y, m_hIcon);
  104. }
  105. else
  106. {
  107. CDialogEx::OnPaint();
  108. }
  109. }
  110. //当用户拖动最小化窗口时系统调用此函数取得光标
  111. //显示。
  112. HCURSOR CHJHomeworkDlg::OnQueryDragIcon()
  113. {
  114. return static_cast<HCURSOR>(m_hIcon);
  115. }
  116. void CHJHomeworkDlg::OnMonitor()
  117. {
  118. CString strPath;
  119. m_combo_folder.GetWindowText(strPath);
  120. if (strPath.GetLength() == 0)
  121. {
  122. MessageBox(L"未选择共享文件夹", L"提示", MB_OK);
  123. return;
  124. }
  125. if (!PathIsDirectory(strPath))
  126. {
  127. MessageBox(L"共享文件夹不存在", L"提示", MB_OK);
  128. return;
  129. }
  130. if (m_unZipManager.m_strConfigPath != (wstring)strPath)
  131. {
  132. WritePrivateProfileString(_T("USER"), _T("share_path"), strPath, m_unZipManager.m_strConfigPath.c_str());
  133. }
  134. if (!m_bMonitor)
  135. {
  136. m_btn_monitor.SetWindowText(L"停止监控");
  137. m_bMonitor = TRUE;
  138. m_unZipManager.OnTaskStart();
  139. PirntLog(L"解压程序启动成功");
  140. }
  141. else
  142. {
  143. std::wstring strMessage = L"确定要停止监控吗?";
  144. if (IDOK == MessageBox(strMessage.c_str(), L"提示", MB_OKCANCEL))
  145. {
  146. m_btn_monitor.SetWindowText(L"开始监控");
  147. m_bMonitor = FALSE;
  148. PirntLog(L"停止监控");
  149. m_unZipManager.OnClose();
  150. }
  151. }
  152. }
  153. void CHJHomeworkDlg::OnClose()
  154. {
  155. std::wstring strMessage = L"确定要退出吗?";
  156. if (IDOK == MessageBox(strMessage.c_str(), L"提示", MB_OKCANCEL))
  157. {
  158. m_unZipManager.OnClose();
  159. ::Shell_NotifyIcon(NIM_DELETE, &m_notifyIcon);
  160. CDialogEx::OnOK();
  161. }
  162. }
  163. void CHJHomeworkDlg::OnHide()
  164. {
  165. this->ShowWindow(SW_HIDE);
  166. }
  167. void CHJHomeworkDlg::RegisterNotify()
  168. {
  169. m_notifyIcon.cbSize = sizeof NOTIFYICONDATA;
  170. m_notifyIcon.hWnd = this->m_hWnd;
  171. m_notifyIcon.uID = IDR_MAINFRAME;
  172. m_notifyIcon.hIcon = LoadIcon(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_MAINFRAME));
  173. _tcscpy(m_notifyIcon.szTip, L"初中作业监控程序");
  174. m_notifyIcon.uCallbackMessage = WM_TO_TRAY_MSG;
  175. m_notifyIcon.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP; //OK,下面就是托盘产生了.
  176. Shell_NotifyIcon(NIM_ADD, &m_notifyIcon);
  177. }
  178. LRESULT CHJHomeworkDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
  179. {
  180. // TODO: 在此添加专用代码和/或调用基类
  181. switch (message) //判断消息类型
  182. {
  183. case WM_BUTTON_CLICKED: // 读卷界面的按钮单击
  184. {
  185. if (wParam == IDC_BUTTON_MONITOR)
  186. {
  187. OnMonitor();
  188. }
  189. else if (wParam == IDC_BUTTON_HIDE)
  190. {
  191. OnHide();
  192. }
  193. }
  194. break;
  195. case WM_TO_TRAY_MSG:
  196. //如果是用户定义的消息
  197. if (lParam == WM_LBUTTONUP)
  198. {
  199. //鼠标双击时主窗口出现
  200. if (this->IsWindowVisible()) //判断窗口当前状态
  201. {
  202. this->ShowWindow(SW_HIDE); //隐藏窗口
  203. }
  204. else
  205. {
  206. this->ShowWindow(SW_SHOW); //显示窗口
  207. }
  208. }
  209. break;
  210. }
  211. return CDialogEx::WindowProc(message, wParam, lParam);
  212. }