Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

276 lines
5.9 KiB

  1. // leakydlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "leakyapp.h"
  5. #include "leakydlg.h"
  6. #ifdef _DEBUG
  7. #undef THIS_FILE
  8. static char BASED_CODE THIS_FILE[] = __FILE__;
  9. #endif
  10. /////////////////////////////////////////////////////////////////////////////
  11. // CAboutDlg dialog used for App About
  12. class CAboutDlg : public CDialog
  13. {
  14. public:
  15. CAboutDlg();
  16. // Dialog Data
  17. //{{AFX_DATA(CAboutDlg)
  18. enum { IDD = IDD_ABOUTBOX };
  19. //}}AFX_DATA
  20. // Implementation
  21. protected:
  22. virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
  23. //{{AFX_MSG(CAboutDlg)
  24. virtual BOOL OnInitDialog();
  25. //}}AFX_MSG
  26. DECLARE_MESSAGE_MAP()
  27. };
  28. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  29. {
  30. //{{AFX_DATA_INIT(CAboutDlg)
  31. //}}AFX_DATA_INIT
  32. }
  33. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  34. {
  35. CDialog::DoDataExchange(pDX);
  36. //{{AFX_DATA_MAP(CAboutDlg)
  37. //}}AFX_DATA_MAP
  38. }
  39. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  40. //{{AFX_MSG_MAP(CAboutDlg)
  41. // No message handlers
  42. //}}AFX_MSG_MAP
  43. END_MESSAGE_MAP()
  44. /////////////////////////////////////////////////////////////////////////////
  45. // CAboutDlg message handlers
  46. BOOL CAboutDlg::OnInitDialog()
  47. {
  48. CDialog::OnInitDialog();
  49. CenterWindow();
  50. // TODO: Add extra about dlg initialization here
  51. return TRUE; // return TRUE unless you set the focus to a control
  52. }
  53. /////////////////////////////////////////////////////////////////////////////
  54. // CLeakyappDlg dialog
  55. CLeakyappDlg::CLeakyappDlg(CWnd* pParent /*=NULL*/)
  56. : CDialog(CLeakyappDlg::IDD, pParent)
  57. {
  58. //{{AFX_DATA_INIT(CLeakyappDlg)
  59. // NOTE: the ClassWizard will add member initialization here
  60. //}}AFX_DATA_INIT
  61. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  62. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  63. m_mabListHead.pNext = NULL;
  64. m_bRunning = FALSE;
  65. }
  66. void CLeakyappDlg::DoDataExchange(CDataExchange* pDX)
  67. {
  68. CDialog::DoDataExchange(pDX);
  69. //{{AFX_DATA_MAP(CLeakyappDlg)
  70. // NOTE: the ClassWizard will add DDX and DDV calls here
  71. //}}AFX_DATA_MAP
  72. }
  73. BEGIN_MESSAGE_MAP(CLeakyappDlg, CDialog)
  74. //{{AFX_MSG_MAP(CLeakyappDlg)
  75. ON_WM_SYSCOMMAND()
  76. ON_WM_PAINT()
  77. ON_WM_QUERYDRAGICON()
  78. ON_BN_CLICKED(ID_FREE_MEMORY, OnFreeMemory)
  79. ON_BN_CLICKED(ID_START_STOP, OnStartStop)
  80. ON_WM_DESTROY()
  81. ON_WM_TIMER()
  82. //}}AFX_MSG_MAP
  83. END_MESSAGE_MAP()
  84. void CLeakyappDlg::SetMemUsageBar()
  85. {
  86. MEMORYSTATUS MemoryStatusData;
  87. LONGLONG llInUse;
  88. DWORD dwPercentUsed;
  89. GlobalMemoryStatus (&MemoryStatusData);
  90. llInUse = (LONGLONG)(MemoryStatusData.dwTotalPageFile - MemoryStatusData.dwAvailPageFile + 5 );
  91. llInUse *= 1000;
  92. llInUse /= MemoryStatusData.dwTotalPageFile;
  93. llInUse /= 10;
  94. dwPercentUsed = (DWORD)llInUse;
  95. SendDlgItemMessage (IDC_LEAK_PROGRESS, PBM_SETPOS, (WPARAM)dwPercentUsed);
  96. }
  97. /////////////////////////////////////////////////////////////////////////////
  98. // CLeakyappDlg message handlers
  99. BOOL CLeakyappDlg::OnInitDialog()
  100. {
  101. CDialog::OnInitDialog();
  102. CenterWindow();
  103. // Add "About..." menu item to system menu.
  104. // IDM_ABOUTBOX must be in the system command range.
  105. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  106. ASSERT(IDM_ABOUTBOX < 0xF000);
  107. CMenu* pSysMenu = GetSystemMenu(FALSE);
  108. CString strAboutMenu;
  109. strAboutMenu.LoadString(IDS_ABOUTBOX);
  110. if (!strAboutMenu.IsEmpty())
  111. {
  112. pSysMenu->AppendMenu(MF_SEPARATOR);
  113. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  114. }
  115. SendDlgItemMessage (IDC_LEAK_PROGRESS, PBM_SETRANGE, 0, MAKELONG(0, 100));
  116. SendDlgItemMessage (IDC_LEAK_PROGRESS, PBM_SETSTEP, 2, 0);
  117. SetMemUsageBar();
  118. return TRUE; // return TRUE unless you set the focus to a control
  119. }
  120. void CLeakyappDlg::OnSysCommand(UINT nID, LPARAM lParam)
  121. {
  122. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  123. {
  124. CAboutDlg dlgAbout;
  125. dlgAbout.DoModal();
  126. }
  127. else if ((nID & 0xFFF0) == SC_CLOSE)
  128. {
  129. OnOK();
  130. }
  131. else
  132. {
  133. CDialog::OnSysCommand(nID, lParam);
  134. }
  135. }
  136. // If you add a minimize button to your dialog, you will need the code below
  137. // to draw the icon. For MFC applications using the document/view model,
  138. // this is automatically done for you by the framework.
  139. void CLeakyappDlg::OnPaint()
  140. {
  141. if (IsIconic())
  142. {
  143. CPaintDC dc(this); // device context for painting
  144. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  145. // Center icon in client rectangle
  146. int cxIcon = GetSystemMetrics(SM_CXICON);
  147. int cyIcon = GetSystemMetrics(SM_CYICON);
  148. CRect rect;
  149. GetClientRect(&rect);
  150. int x = (rect.Width() - cxIcon + 1) / 2;
  151. int y = (rect.Height() - cyIcon + 1) / 2;
  152. // Draw the icon
  153. dc.DrawIcon(x, y, m_hIcon);
  154. }
  155. else
  156. {
  157. CDialog::OnPaint();
  158. }
  159. }
  160. // The system calls this to obtain the cursor to display while the user drags
  161. // the minimized window.
  162. HCURSOR CLeakyappDlg::OnQueryDragIcon()
  163. {
  164. return (HCURSOR) m_hIcon;
  165. }
  166. void CLeakyappDlg::OnFreeMemory()
  167. {
  168. PMEMORY_ALLOC_BLOCK pNextMab, pMab;
  169. pMab = m_mabListHead.pNext;
  170. while (pMab != NULL) {
  171. pNextMab = pMab->pNext;
  172. GlobalFree (pMab);
  173. pMab = pNextMab;
  174. }
  175. m_mabListHead.pNext = NULL;
  176. SetMemUsageBar();
  177. }
  178. void CLeakyappDlg::OnStartStop()
  179. {
  180. if (m_bRunning) {
  181. // then stop
  182. KillTimer (m_TimerId);
  183. m_bRunning = FALSE;
  184. SetDlgItemText (ID_START_STOP, TEXT("&Start Leaking"));
  185. } else {
  186. // not running, so start
  187. m_TimerId = SetTimer (LEAK_TIMER, TIME_INTERVAL, NULL);
  188. if (m_TimerId != 0) {
  189. m_bRunning = TRUE;
  190. SetDlgItemText (ID_START_STOP, TEXT("&Stop Leaking"));
  191. }
  192. }
  193. SetMemUsageBar();
  194. }
  195. void CLeakyappDlg::OnOK()
  196. {
  197. CDialog::OnOK();
  198. }
  199. void CLeakyappDlg::OnDestroy()
  200. {
  201. OnFreeMemory();
  202. CDialog::OnDestroy();
  203. }
  204. void CLeakyappDlg::OnTimer(UINT nIDEvent)
  205. {
  206. PMEMORY_ALLOC_BLOCK pMab, pNewMab;
  207. pNewMab = (PMEMORY_ALLOC_BLOCK)GlobalAlloc (GPTR, ALLOCATION_SIZE);
  208. if (pNewMab != NULL) {
  209. // save this pointer
  210. pNewMab->pNext = NULL;
  211. if (m_mabListHead.pNext == NULL) {
  212. // this is the first entry
  213. m_mabListHead.pNext = pNewMab;
  214. } else {
  215. // go to end of list
  216. pMab = m_mabListHead.pNext;
  217. while (pMab->pNext != NULL) pMab = pMab->pNext;
  218. pMab->pNext = pNewMab;
  219. }
  220. }
  221. SetMemUsageBar();
  222. CDialog::OnTimer(nIDEvent);
  223. }