MyProgBarDlg.cpp
上传用户:szcysw
上传日期:2013-03-11
资源大小:6752k
文件大小:6k
源码类别:

界面编程

开发平台:

Visual C++

  1. // MyProgBarDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "MyProgBar.h"
  5. #include "MyProgBarDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  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 void OnTimer(UINT nIDEvent);
  30. virtual BOOL OnInitDialog();
  31. //}}AFX_MSG
  32. DECLARE_MESSAGE_MAP()
  33. };
  34. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  35. {
  36. //{{AFX_DATA_INIT(CAboutDlg)
  37. //}}AFX_DATA_INIT
  38. }
  39. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  40. {
  41. CDialog::DoDataExchange(pDX);
  42. //{{AFX_DATA_MAP(CAboutDlg)
  43. //}}AFX_DATA_MAP
  44. }
  45. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  46. //{{AFX_MSG_MAP(CAboutDlg)
  47. ON_WM_TIMER()
  48. //}}AFX_MSG_MAP
  49. END_MESSAGE_MAP()
  50. /////////////////////////////////////////////////////////////////////////////
  51. // CMyProgBarDlg dialog
  52. CMyProgBarDlg::CMyProgBarDlg(CWnd* pParent /*=NULL*/)
  53. : CDialog(CMyProgBarDlg::IDD, pParent)
  54. {
  55. //{{AFX_DATA_INIT(CMyProgBarDlg)
  56. // NOTE: the ClassWizard will add member initialization here
  57. //}}AFX_DATA_INIT
  58. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  59. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  60. }
  61. void CMyProgBarDlg::DoDataExchange(CDataExchange* pDX)
  62. {
  63. CDialog::DoDataExchange(pDX);
  64. //{{AFX_DATA_MAP(CMyProgBarDlg)
  65. // NOTE: the ClassWizard will add DDX and DDV calls here
  66. //}}AFX_DATA_MAP
  67. }
  68. BEGIN_MESSAGE_MAP(CMyProgBarDlg, CDialog)
  69. //{{AFX_MSG_MAP(CMyProgBarDlg)
  70. ON_WM_SYSCOMMAND()
  71. ON_WM_PAINT()
  72. ON_WM_QUERYDRAGICON()
  73. ON_WM_TIMER()
  74. //}}AFX_MSG_MAP
  75. END_MESSAGE_MAP()
  76. /////////////////////////////////////////////////////////////////////////////
  77. // CMyProgBarDlg message handlers
  78. BOOL CMyProgBarDlg::OnInitDialog()
  79. {
  80. CDialog::OnInitDialog();
  81. // Add "About..." menu item to system menu.
  82. // IDM_ABOUTBOX must be in the system command range.
  83. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  84. ASSERT(IDM_ABOUTBOX < 0xF000);
  85. CMenu* pSysMenu = GetSystemMenu(FALSE);
  86. if (pSysMenu != NULL)
  87. {
  88. CString strAboutMenu;
  89. strAboutMenu.LoadString(IDS_ABOUTBOX);
  90. if (!strAboutMenu.IsEmpty())
  91. {
  92. pSysMenu->AppendMenu(MF_SEPARATOR);
  93. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  94. }
  95. }
  96. // Set the icon for this dialog.  The framework does this automatically
  97. //  when the application's main window is not a dialog
  98. SetIcon(m_hIcon, TRUE); // Set big icon
  99. SetIcon(m_hIcon, FALSE); // Set small icon
  100. // TODO: Add extra initialization here
  101. //创建自定义进度条
  102. MyProgBar.Create(WS_CHILD|WS_VISIBLE,CRect(10,20,220,42),this,ID_MYPROGRESSBAR);
  103. //设置增量
  104. MyProgBar.SetStep(1);
  105. //设置进度条范围
  106. MyProgBar.SetRange(0,100);
  107. //设置进度条当前进度
  108. MyProgBar.SetPos(1);
  109. //初始化进度
  110. percent=0;
  111. this->SetTimer(ID_MYPROGRESSBAR,100,NULL);
  112. return TRUE;  // return TRUE  unless you set the focus to a control
  113. }
  114. void CMyProgBarDlg::OnSysCommand(UINT nID, LPARAM lParam)
  115. {
  116. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  117. {
  118. CAboutDlg dlgAbout;
  119. dlgAbout.DoModal();
  120. }
  121. else
  122. {
  123. CDialog::OnSysCommand(nID, lParam);
  124. }
  125. }
  126. // If you add a minimize button to your dialog, you will need the code below
  127. //  to draw the icon.  For MFC applications using the document/view model,
  128. //  this is automatically done for you by the framework.
  129. void CMyProgBarDlg::OnPaint() 
  130. {
  131. if (IsIconic())
  132. {
  133. CPaintDC dc(this); // device context for painting
  134. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  135. // Center icon in client rectangle
  136. int cxIcon = GetSystemMetrics(SM_CXICON);
  137. int cyIcon = GetSystemMetrics(SM_CYICON);
  138. CRect rect;
  139. GetClientRect(&rect);
  140. int x = (rect.Width() - cxIcon + 1) / 2;
  141. int y = (rect.Height() - cyIcon + 1) / 2;
  142. // Draw the icon
  143. dc.DrawIcon(x, y, m_hIcon);
  144. }
  145. else
  146. {
  147. CDialog::OnPaint();
  148. }
  149. }
  150. // The system calls this to obtain the cursor to display while the user drags
  151. //  the minimized window.
  152. HCURSOR CMyProgBarDlg::OnQueryDragIcon()
  153. {
  154. return (HCURSOR) m_hIcon;
  155. }
  156. void CAboutDlg::OnTimer(UINT nIDEvent) 
  157. {
  158. }
  159. void CMyProgBarDlg::OnTimer(UINT nIDEvent) 
  160. {
  161. percent++;
  162. if(percent>100)
  163. percent=0;
  164. //设置进度
  165. MyProgBar.SetPos(percent);
  166. //根据percent的奇偶性设置动画效果
  167. if(percent%2)
  168. MyProgBar.SetBmp(IDB_OPEN);
  169. else
  170. MyProgBar.SetBmp(IDB_CLOSE);
  171. CString title;
  172. title.Format("%d%%",percent);
  173. //动态设置进度条标题
  174. MyProgBar.SetBarCaption(title);
  175. CDialog::OnTimer(nIDEvent);
  176. CDialog::OnTimer(nIDEvent);
  177. CDialog::OnTimer(nIDEvent);
  178. }
  179. BOOL CAboutDlg::OnInitDialog() 
  180. {
  181. CDialog::OnInitDialog();
  182. //隐藏对话框 
  183. ShowWindow(SW_HIDE); 
  184. CRect dlgRect; 
  185. GetClientRect(&dlgRect); 
  186. CPoint centerPoint; 
  187. centerPoint.x=dlgRect.Width()/2; 
  188. centerPoint.y=dlgRect.Height()/2; 
  189. //得到对话框的中点坐标 
  190. CRgn testrgn; 
  191. this->ShowWindow(SW_HIDE);
  192. //得到边框宽度
  193. int m=GetSystemMetrics(SM_CYSIZEFRAME);
  194. //以下代码实现对话框的动态弹出 
  195. for (int i=10;i<dlgRect.Width()/2+2*m;i+=1) 
  196. //由内向外建立不同大小的区域
  197. testrgn.CreateRectRgn(centerPoint.x-i,
  198. centerPoint.y-i,centerPoint.x+i,centerPoint.y+i); 
  199. //设置窗口的区域
  200. SetWindowRgn((HRGN) testrgn,TRUE); 
  201. //居中显示窗口
  202. ShowWindow(SW_SHOW); 
  203. CenterWindow(); 
  204. testrgn.DeleteObject(); 
  205. //稍做延时,以呈现动态效果
  206. Sleep(5);
  207. return TRUE; 
  208. }