CalView.cpp
Upload User: kelijie
Upload Date: 2007-01-01
Package Size: 123k
Code Size: 10k
Category:

Graph program

Development Platform:

Visual C++

  1. // CalView.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "ebdlib2.h"
  5. #include "CalView.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CCalculatorView
  13. IMPLEMENT_DYNCREATE(CCalculatorView, CView)
  14. CCalculatorView::CCalculatorView()
  15.    : CStandardView()
  16. {
  17. //{{AFX_DATA_INIT(CCalculatorView)
  18. m_iVar1 = 0;
  19. m_iVar2 = 0;
  20. //}}AFX_DATA_INIT
  21. m_strName = "Calculator Module";
  22. m_strMsg.Empty();
  23. IDD = ID_CALCULATOR;
  24. m_iOperandType = OPERAND_ADD;
  25. }
  26. CCalculatorView::~CCalculatorView()
  27. {
  28. }
  29. BEGIN_MESSAGE_MAP(CCalculatorView, CView)
  30. //{{AFX_MSG_MAP(CCalculatorView)
  31. ON_COMMAND(IDM_CALCULATOREXIT, OnCalculatorexit)
  32. ON_WM_CREATE()
  33. ON_BN_CLICKED(IDC_CALCULATE, OnCalculate)
  34. ON_COMMAND(IDM_ADD, OnAdd)
  35. ON_UPDATE_COMMAND_UI(IDM_ADD, OnUpdateAdd)
  36. ON_COMMAND(IDM_SUB, OnSub)
  37. ON_UPDATE_COMMAND_UI(IDM_SUB, OnUpdateSub)
  38. ON_COMMAND(IDM_TIME, OnTime)
  39. ON_UPDATE_COMMAND_UI(IDM_TIME, OnUpdateTime)
  40. ON_COMMAND(IDM_DVD, OnDvd)
  41. ON_UPDATE_COMMAND_UI(IDM_DVD, OnUpdateDvd)
  42. //}}AFX_MSG_MAP
  43. END_MESSAGE_MAP()
  44. /////////////////////////////////////////////////////////////////////////////
  45. // CCalculatorView drawing
  46. void CCalculatorView::OnDraw(CDC* pDC)
  47. {
  48. CCalculatorDoc* pDoc = (CCalculatorDoc*)GetDocument();
  49. // TODO: add draw code here
  50.     CRect rect;
  51. GetClientRect(&rect);
  52. pDC->FillRect(&rect, &CBrush(RGB(192, 192, 192)));
  53. }
  54. /////////////////////////////////////////////////////////////////////////////
  55. // CCalculatorView diagnostics
  56. #ifdef _DEBUG
  57. void CCalculatorView::AssertValid() const
  58. {
  59. CView::AssertValid();
  60. }
  61. void CCalculatorView::Dump(CDumpContext& dc) const
  62. {
  63. CView::Dump(dc);
  64. }
  65. #endif //_DEBUG
  66. void CCalculatorView::GetMsgFromDocument(CString msg)
  67. {
  68. m_editDocMsg->SetWindowText(msg);
  69. }
  70. /////////////////////////////////////////////////////////////////////////////
  71. // CCalculatorView message handlers
  72. void CCalculatorView::OnInitialUpdate() 
  73. {
  74. CView::OnInitialUpdate();
  75. // TODO: Add your specialized code here and/or call the base class
  76. DWORD dwStaticStyle = WS_CHILD|WS_VISIBLE|SS_LEFT;
  77. DWORD dwEditStyle = ES_LEFT | ES_AUTOHSCROLL | WS_TABSTOP | WS_CHILD | WS_VISIBLE
  78.                 | WS_BORDER | ES_NUMBER | WS_EX_CLIENTEDGE | WS_EX_STATICEDGE;
  79. DWORD dwEditStyleReadOnly = ES_AUTOHSCROLL |ES_READONLY | WS_CHILD| WS_VISIBLE 
  80.                         | WS_BORDER; 
  81. DWORD dwButtonStyle =  WS_CHILD | WS_VISIBLE | WS_TABSTOP |
  82.                    BS_DEFPUSHBUTTON; 
  83. m_Font = new CFont();
  84. m_Font->CreateFont(-8, 0, 0, 0, FW_REGULAR, FALSE, FALSE, FALSE, 
  85.                 DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, 
  86.                 CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, 
  87.                 DEFAULT_PITCH | FF_DONTCARE, "MS Sans Serif");
  88. operFont = new CFont();
  89. operFont->CreateFont(-16, 0, 0, 0, FW_REGULAR, FALSE, FALSE, FALSE, 
  90.                 DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, 
  91.                 CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, 
  92.                 DEFAULT_PITCH | FF_DONTCARE, "MS Sans Serif");
  93. CRect r;
  94. r.SetRect(10, 9, 110+10, 14+9);
  95. m_txtVar1 = new CStatic();
  96. m_txtVar1->Create("The First Number", dwStaticStyle, r, this, IDC_STATIC1);
  97.     m_txtVar1->SetFont(m_Font);
  98. r.SetRect(10, 129, 50+10, 14+129);
  99. m_txtOperand = new CStatic();
  100. m_txtOperand->Create("Operand", dwStaticStyle, r, this, IDC_STATIC2);
  101.     m_txtOperand->SetFont(m_Font);
  102. r.SetRect(10, 49, 110+10, 14+49);
  103. m_txtVar2 = new CStatic();
  104. m_txtVar2->Create("The Second Number", dwStaticStyle, r, this, IDC_STATIC3);
  105.     m_txtVar2->SetFont(m_Font);
  106. r.SetRect(10, 89, 50+10, 14+89);
  107. m_txtResult = new CStatic();
  108. m_txtResult->Create("Result", dwStaticStyle, r, this, IDC_STATIC4);
  109.     m_txtResult->SetFont(m_Font);
  110. r.SetRect(10, 235, 200+10, 14+235);
  111. m_txtDocMsg = new CStatic();
  112. m_txtDocMsg->Create("Message from main module", dwStaticStyle, r, this, IDC_STATIC5);
  113.     m_txtDocMsg->SetFont(m_Font);
  114. r.SetRect(130, 9, 210+130, 25+9);
  115. m_editVar1 = new CEdit();
  116. m_editVar1->Create(dwEditStyle, r, this, IDC_VAR1);
  117.     m_editVar1->SetFont(m_Font);
  118. r.SetRect(130, 129, 120+130, 25+129);
  119. m_editOperand = new CEdit();
  120. m_editOperand->Create(dwEditStyleReadOnly, r, this, IDC_METHOD);
  121.     m_editOperand->SetFont(operFont);
  122. r.SetRect(130, 49, 210+130, 25+49);
  123. m_editVar2 = new CEdit();
  124. m_editVar2->Create(dwEditStyle, r, this, IDC_VAR2);
  125.     m_editVar2->SetFont(m_Font);
  126. r.SetRect(130, 89, 210+130, 25+89);
  127. m_editResult = new CEdit();
  128. m_editResult->Create(dwEditStyleReadOnly, r, this, IDC_RESULT);
  129.     m_editResult->SetFont(m_Font);
  130. r.SetRect(5, 260, 530+5, 25+260);
  131. m_editDocMsg = new CEdit();
  132. m_editDocMsg->Create(dwEditStyleReadOnly, r, this, IDC_MSG);
  133.     m_editDocMsg->SetFont(m_Font);
  134. r.SetRect(220, 190, 220 + 100, 190 + 30);
  135. m_btnCalculation = new CButton();
  136. m_btnCalculation->Create("&Calculation", dwButtonStyle, r, this, IDC_CALCULATE);
  137.     m_btnCalculation->SetFont(m_Font);
  138.     CString  opeType;
  139. switch(m_iOperandType)
  140. {
  141.    case OPERAND_ADD:
  142.        opeType = "+";
  143.                break; 
  144.    case OPERAND_SUB:
  145.        opeType = "-";
  146.                break; 
  147.    case OPERAND_MUL:
  148.        opeType = "*";
  149.                break; 
  150.        case OPERAND_DVD:
  151.        opeType = "/";
  152.                break; 
  153. }
  154.     
  155. m_editOperand->SetWindowText(opeType); 
  156. }
  157. void CCalculatorView::OnCalculatorexit() 
  158. {
  159. // TODO: Add your command handler code here
  160. GetParent()->SendMessage(WM_CLOSE);
  161. }
  162. int CCalculatorView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  163. {
  164. if (CView::OnCreate(lpCreateStruct) == -1)
  165. return -1;
  166. // TODO: Add your specialized creation code here
  167. return 0;
  168. }
  169. void CCalculatorView::DoDataExchange(CDataExchange* pDX) 
  170. {
  171. // TODO: Add your specialized code here and/or call the base class
  172. CView::DoDataExchange(pDX);
  173. //{{AFX_DATA_MAP(CCalculatorView)
  174. DDX_Text(pDX, IDC_VAR1, m_iVar1);
  175. DDV_MinMaxInt(pDX, m_iVar1, -32768, 32768);
  176. DDX_Text(pDX, IDC_VAR2, m_iVar2);
  177. DDV_MinMaxInt(pDX, m_iVar2, -32768, 32768);
  178. //}}AFX_DATA_MAP
  179. }
  180. void CCalculatorView::OnCalculate() 
  181. {
  182. // TODO: Add your control notification handler code here
  183. UpdateData(TRUE);
  184. int result;
  185. CString opertype;
  186.    
  187. switch(m_iOperandType)
  188. {
  189.    case OPERAND_ADD:
  190.        result = m_iVar1 + m_iVar2;
  191.    opertype = " + ";
  192.                break; 
  193.    case OPERAND_SUB:
  194.        result = m_iVar1 - m_iVar2;
  195.    opertype = " - ";
  196.                break; 
  197.    case OPERAND_MUL:
  198.        result = m_iVar1 * m_iVar2;
  199.    opertype = " * ";
  200.                break; 
  201.        case OPERAND_DVD:
  202.        if(m_iVar2 == 0)
  203.                {
  204.    this->MessageBox("The divider is zero !", "Calculator", MB_OK);
  205.    return;
  206.    }
  207.    else
  208.            result = m_iVar1 / m_iVar2;
  209.    opertype = " / ";
  210.                break; 
  211. }
  212. char str[40];
  213. itoa(result, str, 10);
  214.     m_editResult->SetWindowText(str);
  215.     
  216.     CString msg;
  217. itoa(m_iVar1, str, 10);
  218.     msg = str;
  219.     msg += opertype;
  220. itoa(m_iVar2, str, 10);
  221.     msg += str;
  222.     msg += " = ";
  223. itoa(result, str, 10);
  224.     msg += str;;
  225.     GetDocument()->GetMsgFromView(IDD, m_iPin, msg);
  226. }
  227. void CCalculatorView::OnAdd() 
  228. {
  229. // TODO: Add your command handler code here
  230. m_iOperandType = OPERAND_ADD;
  231.     CString  opeType;
  232.     opeType = " + ";
  233. m_editOperand->SetWindowText(opeType); 
  234. }
  235. void CCalculatorView::OnUpdateAdd(CCmdUI* pCmdUI) 
  236. {
  237. // TODO: Add your command update UI handler code here
  238. pCmdUI->SetCheck(m_iOperandType == OPERAND_ADD);
  239. }
  240. void CCalculatorView::OnSub() 
  241. {
  242. // TODO: Add your command handler code here
  243. m_iOperandType = OPERAND_SUB;
  244.     CString  opeType;
  245.     opeType = " - ";
  246. m_editOperand->SetWindowText(opeType); 
  247. }
  248. void CCalculatorView::OnUpdateSub(CCmdUI* pCmdUI) 
  249. {
  250. // TODO: Add your command update UI handler code here
  251. pCmdUI->SetCheck(m_iOperandType == OPERAND_SUB);
  252. }
  253. void CCalculatorView::OnTime() 
  254. {
  255. // TODO: Add your command handler code here
  256. m_iOperandType = OPERAND_MUL;
  257.     CString  opeType;
  258.     opeType = " * ";
  259. m_editOperand->SetWindowText(opeType); 
  260. }
  261. void CCalculatorView::OnUpdateTime(CCmdUI* pCmdUI) 
  262. {
  263. // TODO: Add your command update UI handler code here
  264. pCmdUI->SetCheck(m_iOperandType == OPERAND_MUL);
  265. }
  266. void CCalculatorView::OnDvd() 
  267. {
  268. // TODO: Add your command handler code here
  269. m_iOperandType = OPERAND_DVD;
  270.     CString  opeType;
  271.     opeType = " / ";
  272. m_editOperand->SetWindowText(opeType); 
  273. }
  274. void CCalculatorView::OnUpdateDvd(CCmdUI* pCmdUI) 
  275. {
  276. // TODO: Add your command update UI handler code here
  277. pCmdUI->SetCheck(m_iOperandType == OPERAND_DVD);
  278. }
  279. void CCalculatorView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
  280. {
  281. // TODO: Add your specialized code here and/or call the base class
  282.     if(pHint == NULL)
  283. return;
  284. CString msg;
  285. WNDINFO* info = (WNDINFO *)pHint;
  286. if(info->m_iID == (int)IDD && info->m_iPin == m_iPin)
  287. {
  288.     if((int)lHint == UPDATE_ONE)
  289. {
  290.            msg = "This system information is broadvasted to me!";
  291.            CCalDlg dlg(this);
  292.    dlg.SetOperand(m_iOperandType);
  293.    if(dlg.DoModal())
  294.    {
  295.    dlg.GetOperand(&m_iOperandType);
  296.                CString  opeType;
  297.            switch(m_iOperandType)
  298.    {
  299.        case OPERAND_ADD:
  300.        opeType = " + ";
  301.    break;
  302.        case OPERAND_SUB:
  303.        opeType = " - ";
  304.    break;
  305.        case OPERAND_MUL:
  306.        opeType = " * ";
  307.    break;
  308.        case OPERAND_DVD:
  309.        opeType = " / ";
  310.    break;
  311.    }
  312.        m_editOperand->SetWindowText(opeType); 
  313.    }
  314. }
  315. else
  316.    msg = "This system information doesn't have any thing with me!";
  317. }
  318. else
  319.         msg = "This system information doesn't have any thing with me!";
  320.     
  321. m_editDocMsg->SetWindowText(msg);
  322. }