Using button controls in an application

本文介绍如何使用MFC创建一个简单的计算器程序,通过按钮输入两个数值并进行加减运算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Introduction

This tutorial could well be the simplest windows program you could ever write.  All you need to understand this tutorial is the basics of windows messaging.  This tutorial is about the CButton class, and how to get a simple button working.


This tutorial simply takes input for two numbers and depending on what button you click, does the math.  ie. if you enter 1 and 2, then click on 'Plus' the answer will be 3.  Pretty basic.

To create this program first we need to get the framework laid out.  Now 99.9% of the time, you will be using buttons via a dialog box so that is where we will start.  Open up the AppWizard and create a new project titled ButtonDemo.  Just create a basic dialog box without any document / view architecture.

Then click on the ResourceVew tab in the 'Workspace' window.  Proceed to edit the dialog box IDD_BUTTONDEMO_DLG.  It will already contain the Buttons 'Ok' and 'Cancel'.  Delete the 'TODO :' message and the 'Cancel' button so we can get to work.

Just drag n' drop the buttons needed, in this example 'Plus' and 'Minus'.  Then select and right click to edit their properties.  The MFC keeps track of these buttons by their unique ID, a macro located in the "Resource.h" file.  For code clarity, change the name of the ID to ID_BUTTON_ADD and modify the caption of the button.

Next, let's add the Edit boxes to house the values to be added or subtracted, do this in a very similar manner to the way you added the buttons.  Just drag and drop them in and then modify their ID.  We will need an Edit box for the left and right side of the equation as well as one to house the answer.  Just for clarity to the end user, check the 'disable' property of the last Edit box.  Since we won't directly be able to specify the answer.  We will also add a Static control for the equals sign.  Just drop one in and then change the caption.

Now that our buttons and are in place we need to wire them into our application.  We do this via the class wizard.  (Control + W)  Click on the Member Variables tab and proceed to add the variables m_nX, m_nY, and m_nAnswer.  All integers; these will hold the values inputted from the Edit controls. 

Then click on the Message Maps tab.  And add a new function for the object ID ID_BUTTON_ADD and  ID_BUTTON_SUBTRACT message BN_CLICK.  This create a function is called whenever that button is clicked once.  Denoted by OnButtonAdd and OnButtonSubtract.

Now that we have created our buttons this is all the code we need to write to get this program working.  It is fairly self explanatory: what we did was create two functions to be called whenever the respective buttons were pressed.  The UpdateData (BOOL) functions are used to manage the data in the Edit boxes of the dialog.  UpdataData (TRUE), 'updates' the member variables linked to the Edit boxes to whatever is the current value.  UpdataData (FALSE), updates the Edit box to whatever our variable is.

要注意的地方就是,要使用UpdataData(BOOL)这个函数来刷新Edit控件的内容。

  1. void CButtonDemoDlg::OnButtonAdd() 
  2. {
  3.     // TODO: Add your control notification handler code here
  4.     UpdateData (TRUE);
  5.     m_ans=m_nx+m_ny;
  6.     UpdateData (FALSE);
  7. }
  8. void CButtonDemoDlg::OnButtonSub() 
  9. {
  10.     // TODO: Add your control notification handler code here
  11.     UpdateData (TRUE);
  12.     m_ans=m_nx-m_ny;
  13.     UpdateData (FALSE);
  14. }

Controls 是 C# 中的一个类,它是 System.Windows.Forms 命名空间中的一部分。Controls 类提供了创建和管理 Windows 窗体应用程序中的控件的功能。控件是用户界面的可视化元素,如按钮、文本框、标签等。 通过使用 Controls 类,您可以在窗体上添加、删除和操作各种控件。您可以设置控件的属性,如位置、大小、颜色等。您还可以处理控件的事件,如单击按钮、输入文本等。 以下是使用 Controls 类创建按钮控件的示例代码: ``` using System; using System.Windows.Forms; public class MyForm : Form { public MyForm() { Button myButton = new Button(); myButton.Text = "Click me!"; myButton.Location = new Point(50, 50); myButton.Click += new EventHandler(MyButton_Click); Controls.Add(myButton); } private void MyButton_Click(object sender, EventArgs e) { MessageBox.Show("Button clicked!"); } } public class Program { [STAThread] public static void Main() { Application.Run(new MyForm()); } } ``` 在上面的代码中,我们创建了一个继承自 Form 的自定义窗体类 MyForm。在 MyForm 构造函数中,我们创建了一个按钮控件,并将其添加到窗体的 Controls 集合中。我们还订阅了按钮的 Click 事件,并在事件处理程序中显示一个消息框。 这只是 Controls 类的一小部分功能,它提供了许多其他方法和属性来管理和操作控件。您可以根据您的需求使用 Controls 类的各种功能来开发 Windows 窗体应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值