CalculatorDlg.cpp
上传用户:z_mail1980
上传日期:2007-06-01
资源大小:647k
文件大小:6k
源码类别:

SNMP编程

开发平台:

Visual C++

  1. // CalculatorDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "Calculator.h"
  5. #include "CalculatorDlg.h"
  6. #include "math.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. // Dialog Data
  19. //{{AFX_DATA(CAboutDlg)
  20. enum { IDD = IDD_ABOUTBOX };
  21. //}}AFX_DATA
  22. // ClassWizard generated virtual function overrides
  23. //{{AFX_VIRTUAL(CAboutDlg)
  24. protected:
  25. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  26. //}}AFX_VIRTUAL
  27. // Implementation
  28. protected:
  29. //{{AFX_MSG(CAboutDlg)
  30. //}}AFX_MSG
  31. DECLARE_MESSAGE_MAP()
  32. };
  33. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  34. {
  35. //{{AFX_DATA_INIT(CAboutDlg)
  36. //}}AFX_DATA_INIT
  37. }
  38. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  39. {
  40. CDialog::DoDataExchange(pDX);
  41. //{{AFX_DATA_MAP(CAboutDlg)
  42. //}}AFX_DATA_MAP
  43. }
  44. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  45. //{{AFX_MSG_MAP(CAboutDlg)
  46. // No message handlers
  47. //}}AFX_MSG_MAP
  48. END_MESSAGE_MAP()
  49. /////////////////////////////////////////////////////////////////////////////
  50. // CCalculatorDlg dialog
  51. CCalculatorDlg::CCalculatorDlg(CWnd* pParent /*=NULL*/)
  52. : CDialog(CCalculatorDlg::IDD, pParent)
  53. {
  54. //{{AFX_DATA_INIT(CCalculatorDlg)
  55. m_number = 0.0;
  56. //}}AFX_DATA_INIT
  57. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  58. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  59. m_NumberState=1;
  60. }
  61. void CCalculatorDlg::DoDataExchange(CDataExchange* pDX)
  62. {
  63. CDialog::DoDataExchange(pDX);
  64. //{{AFX_DATA_MAP(CCalculatorDlg)
  65. DDX_Text(pDX, IDC_EDIT_PUTOUT, m_number);
  66. //}}AFX_DATA_MAP
  67. }
  68. BEGIN_MESSAGE_MAP(CCalculatorDlg, CDialog)
  69. //{{AFX_MSG_MAP(CCalculatorDlg)
  70. ON_WM_SYSCOMMAND()
  71. ON_WM_PAINT()
  72. ON_WM_QUERYDRAGICON()
  73. ON_COMMAND_RANGE(IDC_NUMBER1,IDC_NUMBER0,OnNumberKey)
  74. ON_COMMAND_RANGE(IDC_MINUS,IDC_RESULT,OnOperationKey)
  75. //}}AFX_MSG_MAP
  76. END_MESSAGE_MAP()
  77. /////////////////////////////////////////////////////////////////////////////
  78. // CCalculatorDlg message handlers
  79. BOOL CCalculatorDlg::OnInitDialog()
  80. {
  81. CDialog::OnInitDialog();
  82. // Add "About..." menu item to system menu.
  83. // IDM_ABOUTBOX must be in the system command range.
  84. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  85. ASSERT(IDM_ABOUTBOX < 0xF000);
  86. CMenu* pSysMenu = GetSystemMenu(FALSE);
  87. if (pSysMenu != NULL)
  88. {
  89. CString strAboutMenu;
  90. strAboutMenu.LoadString(IDS_ABOUTBOX);
  91. if (!strAboutMenu.IsEmpty())
  92. {
  93. pSysMenu->AppendMenu(MF_SEPARATOR);
  94. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  95. }
  96. }
  97. // Set the icon for this dialog.  The framework does this automatically
  98. //  when the application's main window is not a dialog
  99. SetIcon(m_hIcon, TRUE); // Set big icon
  100. SetIcon(m_hIcon, FALSE); // Set small icon
  101. // TODO: Add extra initialization here
  102. return TRUE;  // return TRUE  unless you set the focus to a control
  103. }
  104. void CCalculatorDlg::OnSysCommand(UINT nID, LPARAM lParam)
  105. {
  106. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  107. {
  108. CAboutDlg dlgAbout;
  109. dlgAbout.DoModal();
  110. }
  111. else
  112. {
  113. CDialog::OnSysCommand(nID, lParam);
  114. }
  115. }
  116. // If you add a minimize button to your dialog, you will need the code below
  117. //  to draw the icon.  For MFC applications using the document/view model,
  118. //  this is automatically done for you by the framework.
  119. void CCalculatorDlg::OnPaint() 
  120. {
  121. if (IsIconic())
  122. {
  123. CPaintDC dc(this); // device context for painting
  124. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  125. // Center icon in client rectangle
  126. int cxIcon = GetSystemMetrics(SM_CXICON);
  127. int cyIcon = GetSystemMetrics(SM_CYICON);
  128. CRect rect;
  129. GetClientRect(&rect);
  130. int x = (rect.Width() - cxIcon + 1) / 2;
  131. int y = (rect.Height() - cyIcon + 1) / 2;
  132. // Draw the icon
  133. dc.DrawIcon(x, y, m_hIcon);
  134. }
  135. else
  136. {
  137. CDialog::OnPaint();
  138. }
  139. }
  140. // The system calls this to obtain the cursor to display while the user drags
  141. //  the minimized window.
  142. HCURSOR CCalculatorDlg::OnQueryDragIcon()
  143. {
  144. return (HCURSOR) m_hIcon;
  145. }
  146. void CCalculatorDlg::OnNumberKey(UINT nID)
  147. {
  148. int n=0;
  149. switch(nID)
  150. {
  151. case IDC_NUMBER1:
  152. n=1;
  153. break;
  154. case IDC_NUMBER2:
  155. n=2;
  156. break;
  157. case IDC_NUMBER3:
  158. n=3;
  159. break;
  160. case IDC_NUMBER4:
  161. n=4;
  162. break;
  163. case IDC_NUMBER5:
  164. n=5;
  165. break;
  166. case IDC_NUMBER6:
  167. n=6;
  168. break;
  169. case IDC_NUMBER7:
  170. n=7;
  171. break;
  172. case IDC_NUMBER8:
  173. n=8;
  174. break;
  175. case IDC_NUMBER9:
  176. n=9;
  177. break;
  178. case IDC_NUMBER0:
  179. n=0;
  180. break;
  181. }
  182. if(m_NumberState==1)
  183. {
  184. m_number=m_number*10+n;
  185. m_number1=m_number;
  186. UpdateData(FALSE);
  187. }
  188. else
  189. {
  190. m_number=m_number*10+n;
  191. m_number2=m_number;
  192. UpdateData(FALSE);
  193. }
  194. }
  195. void CCalculatorDlg::OnOperationKey(UINT nID)
  196. {
  197. switch(nID)
  198. {
  199. case IDC_MINUS:
  200. m_number=-m_number;
  201. if(m_NumberState==1)
  202. m_number1=m_number;
  203. else
  204. m_number2=m_number;
  205. UpdateData(FALSE);
  206. break;
  207. case IDC_BACK:
  208. m_number=(int)m_number/10;
  209. if(m_NumberState==1)
  210. m_number1=m_number;
  211. else
  212. m_number2=m_number;
  213. UpdateData(FALSE);
  214. break;
  215. case IDC_DIVIDE:
  216. m_OperationState=1;
  217. m_number=0;
  218. UpdateData(FALSE);
  219. m_NumberState=2;
  220. break;
  221. case IDC_MULTIPLY:
  222. m_OperationState=2;
  223. m_number=0;
  224. UpdateData(FALSE);
  225. m_NumberState=2;
  226. break;
  227. case IDC_ADD:
  228. m_OperationState=3;
  229. m_number=0;
  230. UpdateData(FALSE);
  231. m_NumberState=2;
  232. break;
  233. case IDC_SUBTRACT:
  234. m_OperationState=4;
  235. m_number=0;
  236. UpdateData(FALSE);
  237. m_NumberState=2;
  238. break;
  239. case IDC_CLEAR:
  240. m_number1=m_number2=m_number=0;
  241. m_number=0;
  242. UpdateData(FALSE);
  243. m_NumberState=1;
  244. break;
  245. case IDC_SQRT:
  246. m_number1=m_number=sqrt(m_number1);
  247. UpdateData(FALSE);
  248. break;
  249. case IDC_RECIPROCAL:
  250. m_number1=m_number=(double) 1/m_number1;
  251. UpdateData(FALSE);
  252. break;
  253. case IDC_RESULT:
  254. cal();
  255. break;
  256. }
  257. }
  258. void CCalculatorDlg::cal()
  259. {
  260. switch(m_OperationState)
  261. {
  262. case 1:
  263. m_number=(double)m_number1/m_number2;
  264. m_NumberState=1;
  265. UpdateData(FALSE);
  266. break;
  267. case 2:
  268. m_number=m_number1*m_number2;
  269. m_NumberState=1;
  270. UpdateData(FALSE);
  271. break;
  272. case 3:
  273. m_number=m_number1+m_number2;
  274. m_NumberState=1;
  275. UpdateData(FALSE);
  276. break;
  277. case 4:
  278. m_number=m_number1-m_number2;
  279. m_NumberState=1;
  280. UpdateData(FALSE);
  281. break;
  282. }
  283. m_OperationState=0;
  284. }