DrvSpaceDlg.cpp
上传用户:lqt88888
上传日期:2009-12-14
资源大小:905k
文件大小:6k
源码类别:

书籍源码

开发平台:

Visual C++

  1. // DrvSpaceDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "DrvSpace.h"
  5. #include "DrvSpaceDlg.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
  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. // CDrvSpaceDlg dialog
  50. CDrvSpaceDlg::CDrvSpaceDlg(CWnd* pParent /*=NULL*/)
  51. : CDialog(CDrvSpaceDlg::IDD, pParent)
  52. {
  53. //{{AFX_DATA_INIT(CDrvSpaceDlg)
  54. // NOTE: the ClassWizard will add member initialization here
  55. //}}AFX_DATA_INIT
  56. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  57. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  58. }
  59. void CDrvSpaceDlg::DoDataExchange(CDataExchange* pDX)
  60. {
  61. CDialog::DoDataExchange(pDX);
  62. //{{AFX_DATA_MAP(CDrvSpaceDlg)
  63. // NOTE: the ClassWizard will add DDX and DDV calls here
  64. //}}AFX_DATA_MAP
  65. }
  66. BEGIN_MESSAGE_MAP(CDrvSpaceDlg, CDialog)
  67. //{{AFX_MSG_MAP(CDrvSpaceDlg)
  68. ON_WM_SYSCOMMAND()
  69. ON_WM_PAINT()
  70. ON_WM_QUERYDRAGICON()
  71. ON_BN_CLICKED(IDC_GETDRVSPACEINFO, OnGetDrvSpaceInfo)
  72. //}}AFX_MSG_MAP
  73. END_MESSAGE_MAP()
  74. /////////////////////////////////////////////////////////////////////////////
  75. // CDrvSpaceDlg message handlers
  76. BOOL CDrvSpaceDlg::OnInitDialog()
  77. {
  78. CDialog::OnInitDialog();
  79. // Add "About..." menu item to system menu.
  80. // IDM_ABOUTBOX must be in the system command range.
  81. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  82. ASSERT(IDM_ABOUTBOX < 0xF000);
  83. CMenu* pSysMenu = GetSystemMenu(FALSE);
  84. if (pSysMenu != NULL)
  85. {
  86. CString strAboutMenu;
  87. strAboutMenu.LoadString(IDS_ABOUTBOX);
  88. if (!strAboutMenu.IsEmpty())
  89. {
  90. pSysMenu->AppendMenu(MF_SEPARATOR);
  91. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  92. }
  93. }
  94. // Set the icon for this dialog.  The framework does this automatically
  95. //  when the application's main window is not a dialog
  96. SetIcon(m_hIcon, TRUE); // Set big icon
  97. SetIcon(m_hIcon, FALSE); // Set small icon
  98. // TODO: Add extra initialization here
  99. //列出所有驱动器
  100. FindAllDrivers();
  101. CComboBox* Driver=(CComboBox*)GetDlgItem(IDC_DRIVER);
  102. Driver->SetCurSel(0);
  103. return TRUE;  // return TRUE  unless you set the focus to a control
  104. }
  105. void CDrvSpaceDlg::OnSysCommand(UINT nID, LPARAM lParam)
  106. {
  107. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  108. {
  109. CAboutDlg dlgAbout;
  110. dlgAbout.DoModal();
  111. }
  112. else
  113. {
  114. CDialog::OnSysCommand(nID, lParam);
  115. }
  116. }
  117. // If you add a minimize button to your dialog, you will need the code below
  118. //  to draw the icon.  For MFC applications using the document/view model,
  119. //  this is automatically done for you by the framework.
  120. void CDrvSpaceDlg::OnPaint() 
  121. {
  122. if (IsIconic())
  123. {
  124. CPaintDC dc(this); // device context for painting
  125. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  126. // Center icon in client rectangle
  127. int cxIcon = GetSystemMetrics(SM_CXICON);
  128. int cyIcon = GetSystemMetrics(SM_CYICON);
  129. CRect rect;
  130. GetClientRect(&rect);
  131. int x = (rect.Width() - cxIcon + 1) / 2;
  132. int y = (rect.Height() - cyIcon + 1) / 2;
  133. // Draw the icon
  134. dc.DrawIcon(x, y, m_hIcon);
  135. }
  136. else
  137. {
  138. CDialog::OnPaint();
  139. }
  140. }
  141. // The system calls this to obtain the cursor to display while the user drags
  142. //  the minimized window.
  143. HCURSOR CDrvSpaceDlg::OnQueryDragIcon()
  144. {
  145. return (HCURSOR) m_hIcon;
  146. }
  147. void CDrvSpaceDlg::OnGetDrvSpaceInfo() 
  148. {
  149. //更新列表框
  150. UpdateData(TRUE);
  151. //获得选择的驱动器
  152. CString Driver;
  153. CComboBox* pDriver = (CComboBox*)GetDlgItem(IDC_DRIVER);
  154. pDriver->GetWindowText(Driver);
  155. //获得磁盘空间信息
  156. ULARGE_INTEGER FreeAv,TotalBytes,FreeBytes;
  157. if(GetDiskFreeSpaceEx(Driver,&FreeAv,&TotalBytes,&FreeBytes))
  158. {
  159. //格式化信息,并显示出来
  160. CString strTotalBytes,strFreeBytes;
  161. strTotalBytes.Format("%u字节",TotalBytes.QuadPart);
  162. strFreeBytes.Format("%u字节",FreeBytes.QuadPart);
  163. CStatic* pTotalStatic = (CStatic*)GetDlgItem(IDC_TOTAL);
  164. CStatic* pFreeStatic = (CStatic*)GetDlgItem(IDC_FREE);
  165. pTotalStatic->SetWindowText(strTotalBytes);
  166. pFreeStatic->SetWindowText(strFreeBytes);
  167. }
  168. }
  169. void CDrvSpaceDlg::FindAllDrivers()
  170. {
  171. CComboBox* Driver=(CComboBox*)GetDlgItem(IDC_DRIVER);
  172. DWORD dwNumBytesForDriveStrings;//实际存储驱动器号的字符串长度
  173. HANDLE hHeap;
  174. LPSTR lp;
  175. CString strLogdrive;
  176. //获得实际存储驱动器号的字符串长度
  177. dwNumBytesForDriveStrings=GetLogicalDriveStrings(0,NULL)*sizeof(TCHAR);
  178. //如果字符串不为空,则表示有正常的驱动器存在
  179. if (dwNumBytesForDriveStrings!=0) {
  180. //分配字符串空间
  181. hHeap=GetProcessHeap();
  182. lp=(LPSTR)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,
  183. dwNumBytesForDriveStrings);
  184. //获得标明所有驱动器的字符串
  185. GetLogicalDriveStrings(HeapSize(hHeap,0,lp),lp);
  186. //将驱动器一个个放到下拉框中
  187. while (*lp!=0) {
  188. Driver->AddString(lp);
  189. lp=_tcschr(lp,0)+1;
  190. }
  191. }
  192. else 
  193. AfxMessageBox("Can't Use The Function GetLogicalDriveStrings!");
  194. }