The Imitation Windows calculator written by MFC

Source: Internet
Author: User
Tags variables

First you should understand object-oriented message-driven Windows Messaging programming, of course, for writing this calculator, you don't have to know too much about Windows programming, you are important to write a simple dialog based application.

First, let's take a closer look at Mircosoft's calculator, which we find is a dialog-box-containing two-based main dialog box, a dialog box, a menu application. In other words, we should create a dialog based application and add a menu and a main dialog box for it (because the application has created a main dialog box and a dialog box)

Next, we should design the interface of the dialog box, which is a very simple thing, you just have to compare it with the Mircosoft calculator. Of course, you can also add other controls.

The first main dialog box is also the default main program interface, which is used to perform simple calculations (+ 、-、 *,/, open root, Countdown, squared, etc.)

The second is the procedure for performing scientific calculations. The user can use the Control typesetting tool in VC and tab to design a beautiful interface (usually, the program interface to a large extent will affect the popularity of the program and the user's emotions). Well, after the work is done, let's get down to business.

We first have to assign an ID number to all the controls (be sure to see the ID), such as the 0 control we use ID_0 to express. The other controls, in turn. We should add variables to the ID when we design it, and here we specify the CString variable for all controls except the edit control, and edit the control to CEdit. Then I add some of the necessary member variables (let's take a simple counter for example)

public:
char m_sign; //记录运算符+、-、*、/等
int m; //控制 编辑框中的字符
int n; //用于判断连续进行了几次运算
CString strItem; // 用于记录当前编辑框中的数据
CString m_string; //用于记录前n-1次的运算结果

Now we write the constructor:

m_hIcon = AfxGetApp()->LoadIcon (IDR_MAINFRAME);
m=n=0;
m_string="";
strItem="";

We then write a program for the number key:

(以0为例) if(!m) {m_Edit.SetWindowText("");m++;} //用于得到连续的输入
m_Edit.GetWindowText(strItem); //将当前字符保存在strItem中
CString str="0";输入数字
strItem+=str;//连续输入字符
m_Edit.SetWindowText(strItem); //显示连续的输入

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.