CProgressCtrlDlg.cpp
上传用户:cn05999
上传日期:2020-06-29
资源大小:84k
文件大小:9k
源码类别:

Static控件

开发平台:

Visual C++

  1. // CProgressCtrlDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "CProgressCtrl.h"
  5. #include "CProgressCtrlDlg.h"
  6. #include "ColourPicker.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CAboutDlg dialog used for App About
  14. class CAboutDlg : public CDialog
  15. {
  16. public:
  17. CAboutDlg();
  18. COLORREF m_clrText;
  19. // Dialog Data
  20. //{{AFX_DATA(CAboutDlg)
  21. enum { IDD = IDD_ABOUTBOX };
  22. CColourPicker m_TextColor;
  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. virtual void OnOK();
  33. //}}AFX_MSG
  34. DECLARE_MESSAGE_MAP()
  35. };
  36. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  37. {
  38. //{{AFX_DATA_INIT(CAboutDlg)
  39. //}}AFX_DATA_INIT
  40. }
  41. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  42. {
  43. CDialog::DoDataExchange(pDX);
  44. //{{AFX_DATA_MAP(CAboutDlg)
  45. DDX_Control(pDX, IDC_BUTTON, m_TextColor);
  46. //}}AFX_DATA_MAP
  47. // DDX_ColourPicker(pDX,IDC_BUTTON,m_clrText);
  48. }
  49. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  50. //{{AFX_MSG_MAP(CAboutDlg)
  51. //}}AFX_MSG_MAP
  52. END_MESSAGE_MAP()
  53. /////////////////////////////////////////////////////////////////////////////
  54. // CCProgressCtrlDlg dialog
  55. CCProgressCtrlDlg::CCProgressCtrlDlg(CWnd* pParent /*=NULL*/)
  56. : CDialog(CCProgressCtrlDlg::IDD, pParent)
  57. {
  58. //{{AFX_DATA_INIT(CCProgressCtrlDlg)
  59. m_bShowPercent = FALSE;
  60. m_range = 500;
  61. //}}AFX_DATA_INIT
  62. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  63. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  64. m_clrBk=RGB(0xE6,0xE6,0xFA);// _T("淡紫色") },
  65. m_clrEnd=RGB(0x00,0x93,0x00);// _T("绿色") },
  66. m_clrText=RGB(0x00,0x00,0x00);// _T("黑色") },
  67. m_clrStart=RGB(0xFF,0xFF,0x00);// _T("黄色") },
  68. m_bClr=TRUE;
  69. m_hIcons[0]=AfxGetApp()->LoadIcon(IDI_ICON1);
  70. m_hIcons[1]=AfxGetApp()->LoadIcon(IDI_ICON2);
  71. m_hIcons[2]=AfxGetApp()->LoadIcon(IDI_ICON3);
  72. m_hIcons[3]=AfxGetApp()->LoadIcon(IDI_ICON4);
  73. m_hIcons[4]=AfxGetApp()->LoadIcon(IDI_ICON5);
  74. m_hIcons[5]=AfxGetApp()->LoadIcon(IDI_ICON6);
  75. m_hIcons[6]=AfxGetApp()->LoadIcon(IDI_ICON7);
  76. m_hIcons[7]=AfxGetApp()->LoadIcon(IDI_ICON8);
  77. m_TrayIcon.m_bHidden=FALSE;
  78. }
  79. void CCProgressCtrlDlg::DoDataExchange(CDataExchange* pDX)
  80. {
  81. CDialog::DoDataExchange(pDX);
  82. //{{AFX_DATA_MAP(CCProgressCtrlDlg)
  83. DDX_Control(pDX, IDC_BUTTON_TEXT, m_cClrText);
  84. DDX_Control(pDX, IDC_BUTTON_BGD, m_cClrBkGnd);
  85. DDX_Control(pDX, IDC_BUTTON_END, m_cClrEnd);
  86. DDX_Control(pDX, IDC_BUTTON_START, m_cClrStart);
  87. DDX_Control(pDX, IDC_PROGRESS, m_cProgress);
  88. DDX_Check(pDX, IDC_CHECK1, m_bShowPercent);
  89. DDX_Text(pDX, IDC_EDIT1, m_range);
  90. //}}AFX_DATA_MAP
  91. DDX_ColourPicker(pDX,IDC_BUTTON_TEXT,m_clrText);
  92. DDX_ColourPicker(pDX,IDC_BUTTON_START,m_clrStart);
  93. DDX_ColourPicker(pDX,IDC_BUTTON_END,m_clrEnd);
  94. DDX_ColourPicker(pDX,IDC_BUTTON_BGD,m_clrBk);
  95. }
  96. BEGIN_MESSAGE_MAP(CCProgressCtrlDlg, CDialog)
  97. //{{AFX_MSG_MAP(CCProgressCtrlDlg)
  98. ON_WM_SYSCOMMAND()
  99. ON_WM_PAINT()
  100. ON_WM_QUERYDRAGICON()
  101. ON_BN_CLICKED(IDC_BUTTON_TEST, OnButtonTest)
  102. ON_WM_CREATE()
  103. ON_COMMAND(ID_HIDEWINDOW, OnHidewindow)
  104. ON_COMMAND(ID_SHOWWINDOW, OnShowwindow)
  105. ON_UPDATE_COMMAND_UI(ID_SHOWWINDOW, OnUpdateShowwindow)
  106. ON_UPDATE_COMMAND_UI(ID_HIDEWINDOW, OnUpdateHidewindow)
  107. ON_WM_TIMER()
  108. ON_WM_SIZE()
  109. //}}AFX_MSG_MAP
  110. ON_MESSAGE(WM_TRAYNOTIFY,OnTrayNotification)
  111. END_MESSAGE_MAP()
  112. /////////////////////////////////////////////////////////////////////////////
  113. // CCProgressCtrlDlg message handlers
  114. BOOL CCProgressCtrlDlg::OnInitDialog()
  115. {
  116. CDialog::OnInitDialog();
  117. // Add "About..." menu item to system menu.
  118. // IDM_ABOUTBOX must be in the system command range.
  119. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  120. ASSERT(IDM_ABOUTBOX < 0xF000);
  121. CMenu* pSysMenu = GetSystemMenu(FALSE);
  122. if (pSysMenu != NULL)
  123. {
  124. CString strAboutMenu;
  125. strAboutMenu.LoadString(IDS_ABOUTBOX);
  126. if (!strAboutMenu.IsEmpty())
  127. {
  128. pSysMenu->AppendMenu(MF_SEPARATOR);
  129. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  130. }
  131. }
  132. // Set the icon for this dialog.  The framework does this automatically
  133. //  when the application's main window is not a dialog
  134. SetIcon(m_hIcon, TRUE); // Set big icon
  135. SetIcon(m_hIcon, FALSE); // Set small icon
  136. // TODO: Add extra initialization here
  137. return TRUE;  // return TRUE  unless you set the focus to a control
  138. }
  139. void CCProgressCtrlDlg::OnSysCommand(UINT nID, LPARAM lParam)
  140. {
  141. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  142. {
  143. CAboutDlg dlgAbout;
  144. dlgAbout.DoModal();
  145. }
  146. else
  147. {
  148. CDialog::OnSysCommand(nID, lParam);
  149. }
  150. }
  151. // If you add a minimize button to your dialog, you will need the code below
  152. //  to draw the icon.  For MFC applications using the document/view model,
  153. //  this is automatically done for you by the framework.
  154. void CCProgressCtrlDlg::OnPaint() 
  155. {
  156. if (IsIconic())
  157. {
  158. CPaintDC dc(this); // device context for painting
  159. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  160. // Center icon in client rectangle
  161. int cxIcon = GetSystemMetrics(SM_CXICON);
  162. int cyIcon = GetSystemMetrics(SM_CYICON);
  163. CRect rect;
  164. GetClientRect(&rect);
  165. int x = (rect.Width() - cxIcon + 1) / 2;
  166. int y = (rect.Height() - cyIcon + 1) / 2;
  167. // Draw the icon
  168. dc.DrawIcon(x, y, m_hIcon);
  169. }
  170. else
  171. {
  172. CDialog::OnPaint();
  173. }
  174. }
  175. // The system calls this to obtain the cursor to display while the user drags
  176. //  the minimized window.
  177. HCURSOR CCProgressCtrlDlg::OnQueryDragIcon()
  178. {
  179. return (HCURSOR) m_hIcon;
  180. }
  181. void CAboutDlg::OnOK() 
  182. {
  183. // TODO: Add extra validation here
  184. /// m_bActive=TRUE;
  185. CDialog::OnOK();
  186. }
  187. /*void CCProgressCtrlDlg::Oncolour() 
  188. {
  189. // TODO: Add your control notification handler code here
  190. CRect rect;
  191. GetWindowRect(rect);
  192. new CColourPopup(CPoint(rect.left,rect.bottom),RGB(0xFF,0xE4,0xE1),this,10,_T("其它颜色…"));
  193. return ;
  194. }*/
  195. void CCProgressCtrlDlg::OnButtonTest() 
  196. {
  197. // TODO: Add your control notification handler code here
  198. UpdateData();
  199. m_cProgress.SetRange(0,m_range);
  200. m_cProgress.ShowPercent(m_bShowPercent);
  201. m_cProgress.SetStartColor(m_clrStart);
  202. m_cProgress.SetEndColor(m_clrEnd);
  203. m_cProgress.SetBkColor(m_clrBk);
  204. m_cProgress.SetTextColor(m_clrText);
  205. for(int i=0;i<=m_range;i++)
  206. {
  207. PeekAndPump();
  208. m_cProgress.SetPos(i);
  209. }
  210. }
  211. BOOL PeekAndPump()
  212. {
  213. static MSG msg;
  214. while(::PeekMessage(&msg,NULL,0,0,PM_NOREMOVE))
  215. {
  216. if(!AfxGetApp()->PumpMessage()){
  217. ::PostQuitMessage(0);
  218. return FALSE;
  219. }
  220. }
  221. return TRUE;
  222. }
  223. int CCProgressCtrlDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  224. {
  225. if (CDialog::OnCreate(lpCreateStruct) == -1)
  226. return -1;
  227. // TODO: Add your specialized creation code here
  228. if(!m_TrayIcon.CreateTray(this,IDR_TRAYPOPUP,_T("演示托盘(任务栏)应用程序"),m_hIcons,8,400,WM_TRAYNOTIFY))
  229. {
  230. AfxMessageBox(_T("Failed to create tray icon"),MB_OK|MB_ICONSTOP);
  231. return -1;
  232. }
  233. SetTimer(3,400,NULL);
  234. return 0;
  235. }
  236. LRESULT CCProgressCtrlDlg::OnTrayNotification(WPARAM wParam, LPARAM lParam)
  237. {
  238. return  m_TrayIcon.OnTrayNotification(wParam,lParam);
  239. }
  240. void CCProgressCtrlDlg::OnHidewindow() 
  241. {
  242. // TODO: Add your command handler code here
  243. ShowWindow(SW_SHOWMINIMIZED);
  244. ShowWindow(SW_HIDE);
  245. UpdateWindow();
  246. m_TrayIcon.m_bIsWindowNormal=false;
  247. m_TrayIcon.m_bHidden=TRUE;
  248. }
  249. void CCProgressCtrlDlg::OnShowwindow() 
  250. {
  251. // TODO: Add your command handler code here
  252. ShowWindow(SW_RESTORE);
  253. SetForegroundWindow();
  254. m_TrayIcon.m_bIsWindowNormal=true;
  255. m_TrayIcon.m_bHidden=FALSE;
  256. }
  257. void CCProgressCtrlDlg::OnUpdateShowwindow(CCmdUI* pCmdUI) 
  258. {
  259. // TODO: Add your command update UI handler code here
  260. if(m_TrayIcon.IsHidden())
  261. pCmdUI->SetCheck(TRUE);
  262. else
  263. pCmdUI->SetCheck(FALSE);
  264. }
  265. void CCProgressCtrlDlg::OnUpdateHidewindow(CCmdUI* pCmdUI) 
  266. {
  267. // TODO: Add your command update UI handler code here
  268. if(m_TrayIcon.IsShowing())
  269. pCmdUI->SetCheck(TRUE);
  270. else
  271. pCmdUI->SetCheck(FALSE);
  272. }
  273. void CCProgressCtrlDlg::OnTimer(UINT nIDEvent) 
  274. {
  275. // TODO: Add your message handler code here and/or call default
  276. m_TrayIcon.OnTimer1(nIDEvent);
  277. CDialog::OnTimer(nIDEvent);
  278. }
  279. void CCProgressCtrlDlg::OnSize(UINT nType, int cx, int cy) 
  280. {
  281. CDialog::OnSize(nType, cx, cy);
  282. // TODO: Add your message handler code here
  283. if(nType==SIZE_MINIMIZED)
  284. {
  285. OnHidewindow();
  286. }
  287. }
  288. /*void CCProgressCtrlDlg::OnAppExitWindow() 
  289. {
  290. // TODO: Add your command handler code here
  291. ShowWindow(SW_SHOWMINIMIZED);
  292. ShowWindow(SW_HIDE);
  293. UpdateWindow();
  294. m_TrayIcon.m_bIsWindowNormal=false;
  295. }*/