WPF实现键盘鼠标输入简易计算器

使用的开发工具为VS2010,简单操作如下:

首先创建项目,整体为单页面应用

在App.xaml中指明启动初始页面

<Application x:Class="Calcluator.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
         
    </Application.Resources>
</Application> 

q前台代码

MainWindow.xaml

<Window x:Class="Calculator.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="计算器" Height="600" Width="400" Icon="Images/calculatorIcon.png"
        KeyDown="Calcluator_KeyDown">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="3*"></RowDefinition>
            <RowDefinition Height="2*"></RowDefinition>
            <RowDefinition Height="2*"></RowDefinition>
            <RowDefinition Height="2*"></RowDefinition>
            <RowDefinition Height="2*"></RowDefinition>
            <RowDefinition Height="2*"></RowDefinition>
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
 
        <TextBlock Grid.Row="0" Grid.ColumnSpan="4" Height="Auto" Name="textBlockExpression" Margin="0,0,15,0" HorizontalAlignment="Right" FontSize="36" Width="Auto"/>
        <TextBlock Grid.Row="0" Grid.ColumnSpan="4" Height="60"  Name="textBlockResult" Margin="0,0,15,0" HorizontalAlignment="Right" FontSize="36" Text="0" />

        <Button Grid.Row="1" Grid.Column="0" Name="buttonClear" Content="C" FontSize="40" Click="buttonClear_Click"></Button>
        <Button Grid.Row="1" Grid.Column="1" Name="buttonDelete" Content="" FontSize="40" Click="buttonDelete_Click"></Button>
        <Button Grid.Row="1" Grid.Column="2" Name="buttonTemp" Content="" FontSize="40"></Button>
        <Button Grid.Row="1" Grid.Column="3" Name="buttonAdd" Content="+" Click="buttonMark_Click" FontSize="40"></Button>

        <Button Grid.Row="2" Grid.Column="0" Name="buttonNum1" Content="1" Click="buttonNum_Click" FontSize="40"></Button>
        <Button Grid.Row="2" Grid.Column="1" Name="buttonNum2" Content="2" Click="buttonNum_Click" FontSize="40"></Button>
        <Button Grid.Row="2" Grid.Column="2" Name="buttonNum3" Content="3" Click="buttonNum_Click" FontSize="40"></Button>
        <Button Grid.Row="2" Grid.Column="3" Name="buttonMinus" Content="-" Click="buttonMark_Click" FontSize="40"></Button>

        <Button Grid.Row="3" Grid.Column="0" Name="buttonNum4" Content="4" Click="buttonNum_Click" FontSize="40"></Button>
        <Button Grid.Row="3" Grid.Column="1" Name="buttonNum5" Content="5" Click="buttonNum_Click" FontSize="40"></Button>
        <Button Grid.Row="3" Grid.Column="2" Name="buttonNum6" Content="6" Click="buttonNum_Click" FontSize="40"></Button>
        <Button Grid.Row="3" Grid.Column="3" Name="buttonMultiply" Content="×" Click="buttonMark_Click" FontSize="40"></Button>

        <Button Grid.Row="4" Grid.Column="0" Name="buttonNum7" Content="7" Click="buttonNum_Click" FontSize="40"></Button>
        <Button Grid.Row="4" Grid.Column="1" Name="buttonNum8" Content="8" Click="buttonNum_Click" FontSize="40"></Button>
        <Button Grid.Row="4" Grid.Column="2" Name="buttonNum9" Content="9" Click="buttonNum_Click" FontSize="40"></Button>
        <Button Grid.Row="4" Grid.Column="3" Name="buttonDivide" Content="÷" Click="buttonMark_Click" FontSize="40"></Button>

        <Button Grid.Row="5" Grid.ColumnSpan="2" Name="buttonNum0" Content="0" Click="buttonNum_Click" FontSize="40"></Button>
        <Button Grid.Row="5" Grid.Column="2" Name="buttonPoint" Content="." Click="buttonNum_Click" FontSize="40"></Button>
        <Button Grid.Row="5" Grid.Column="3" Name="buttonEquals" Content="=" Click="buttonEqual_Click" FontSize="40" Background="#FFF88B26"></Button>
    </Grid>
</Window>

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Calculator
{
    /// <summary>
    /// 计算器交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        /// <summary>
        /// 计算结果值
        /// </summary>
        private double m_dResult = 0.0;

        /// <summary>
        /// 运算符号标记
        /// </summary>
        private string m_strOperator = "";

        public MainWindow()
        {
            InitializeComponent ();
        }

        /// <summary>
        /// 数字按钮鼠标点击事件方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonNum_Click(object sender, RoutedEventArgs e)
        {
            if (m_strOperator == "=")
                defaultCalcluator (e);
            string strCurrentNum = ((Button)sender).Content.ToString ();
            inputNum (strCurrentNum);
        }

        /// <summary>
        /// 键盘或鼠标输入数字方法
        /// </summary>
        /// <param name="strInputNum"></param>
        private void inputNum(string strInputNum)
        {
            if (m_strOperator != "")
            {
                string strCurrentExpression = textBlockExpression.Text;
                string strSufExperssion = strCurrentExpression.Substring (strCurrentExpression.Length - 1, 1);
                //计算表达式最后一位不是符号
                if (isSufOperatorMark (strSufExperssion))
                {
                    textBlockResult.Text = strInputNum;
                }
                else
                {
                    textBlockResult.Text = textBlockResult.Text + strInputNum;
                }
                textBlockExpression.Text = textBlockExpression.Text + strInputNum;
            }
            else
            {
                //输入第一个数字为0情况判断
                if (textBlockResult.Text == "0" && strInputNum != ".")
                {
                    textBlockResult.Text = strInputNum;
                    textBlockExpression.Text = strInputNum;
                }
                else
                {
                    textBlockResult.Text = textBlockResult.Text + strInputNum;
                    textBlockExpression.Text = textBlockExpression.Text + strInputNum;
                }
            }
        }

        /// <summary>
        /// 运算符号按钮鼠标点击事件方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonMark_Click(object sender, RoutedEventArgs e)
        {
            string strCurrentMark = ((Button)sender).Content.ToString ();
            inputOperationMark (strCurrentMark);
        }

        /// <summary>
        /// 键盘或鼠标输入运算符号方法
        /// </summary>
        /// <param name="strInputMark"></param>
        private void inputOperationMark(string strInputMark)
        {
            string strCurrentExpression = textBlockExpression.Text;
            if (!string.IsNullOrEmpty (strCurrentExpression))
            {
                string strSufExperssion = strCurrentExpression.Substring (strCurrentExpression.Length - 1, 1);
                if (isSufOperatorMark (strSufExperssion))
                {
                    //防止重复输入运算符号
                    if (strInputMark != strSufExperssion)
                    {
                        //可以修改不同运算符号
                        m_strOperator = strInputMark;
                        textBlockExpression.Text = strCurrentExpression.Substring (0, strCurrentExpression.Length - 1) + strInputMark;
                    }
                }
                else
                {
                    textBlockExpression.Text = textBlockExpression.Text + strInputMark;
                    OperationNum (strInputMark);
                    textBlockResult.Text = m_dResult.ToString ();
                }
            }
        }

        /// <summary>
        /// 数值计算
        /// </summary>
        /// <param name="strMark"></param>
        private void OperationNum(string strMark)
        {
            switch (m_strOperator)
            {
                case "":
                    m_dResult = double.Parse (textBlockResult.Text);
                    m_strOperator = strMark;
                    break;
                case "+":
                    m_dResult = m_dResult + double.Parse (textBlockResult.Text);
                    m_strOperator = strMark;
                    break;
                case "-":
                    m_dResult = m_dResult - double.Parse (textBlockResult.Text);
                    m_strOperator = strMark;
                    break;
                case "×":
                    m_dResult = m_dResult * double.Parse (textBlockResult.Text);
                    m_strOperator = strMark;
                    break;
                case "÷":
                    if (textBlockResult.Text == "0")
                    {
                        MessageBox.Show ("除数不能为0!");
                        textBlockExpression.Text = "";
                        textBlockResult.Text = "0";
                        m_strOperator = "";
                        //m_dResult = 0.0;
                    }
                    else
                    {
                        m_dResult = m_dResult / double.Parse (textBlockResult.Text);
                        m_strOperator = strMark;
                    }
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 判断输入的计算表达式最后一位是运算符号
        /// </summary>
        /// <param name="strMark"></param>
        /// <returns></returns>
        private bool isSufOperatorMark(string strMark)
        {
            string[] arrOperatorMark = { "+", "-", "×", "÷" };
            string strCurrentExpression = textBlockExpression.Text;
            if (Array.IndexOf (arrOperatorMark, strMark) == -1)
                return false;
            return true;
        }

        /// <summary>
        /// “=”按钮点击事件方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonEqual_Click(object sender, RoutedEventArgs e)
        {
            //防止打开计算器直接按等于
            if (m_strOperator != "")
            {
                textBlockExpression.Text = textBlockExpression.Text + "=";
                OperationNum ("=");
                textBlockResult.Text = m_dResult.ToString ();
            }
        }

        /// <summary>
        /// 退格按钮点击事件方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonDelete_Click(object sender, RoutedEventArgs e)
        {
            string strCurrentResult = textBlockResult.Text;
            if (!string.IsNullOrEmpty (strCurrentResult))
                textBlockResult.Text = strCurrentResult.Substring (0, strCurrentResult.Length - 1);
            else
            {
                textBlockResult.Text = "0";
                //textBlockExpression.Text = "";
            }
        }

        /// <summary>
        /// 清空按钮点击事件方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonClear_Click(object sender, RoutedEventArgs e)
        {
            defaultCalcluator (e);
        }

        /// <summary>
        /// 计算器回归默认值
        /// </summary>
        public void defaultCalcluator(RoutedEventArgs e)
        {
            textBlockExpression.Text = "";
            textBlockResult.Text = "0";
            m_strOperator = "";
            m_dResult = 0.0;
        }

        /// <summary>
        /// 键盘按键输入事件方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Calcluator_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyStates == Keyboard.GetKeyStates (Key.OemPlus) && Keyboard.Modifiers == ModifierKeys.Shift)
            {
                inputOperationMark ("+");
            }
            if (e.KeyStates == Keyboard.GetKeyStates (Key.OemMinus))
            {
                inputOperationMark ("-");
            }
            if (e.KeyStates == Keyboard.GetKeyStates (Key.OemQuestion))
            {
                inputOperationMark ("÷");
            }
            if (e.Key == Key.Return)
            {
                //防止打开计算器直接按等于
                if (m_strOperator != "")
                {
                    textBlockExpression.Text = textBlockExpression.Text + "=";
                    OperationNum ("=");
                    textBlockResult.Text = m_dResult.ToString ();
                }
            }

            if (e.KeyStates == Keyboard.GetKeyStates (Key.D0))
            {
                inputNum ("0");
            }
            if (e.KeyStates == Keyboard.GetKeyStates (Key.D1))
            {
                inputNum ("1");
            }
            if (e.Key == Key.D2)
            {
                inputNum ("2");
            }
            if (e.KeyStates == Keyboard.GetKeyStates (Key.D3))
            {
                inputNum ("3");
            }
            if (e.KeyStates == Keyboard.GetKeyStates (Key.D4))
            {
                inputNum ("4");
            }
            if (e.KeyStates == Keyboard.GetKeyStates (Key.D5))
            {
                inputNum ("5");
            }
            if (e.KeyStates == Keyboard.GetKeyStates (Key.D6))
            {
                inputNum ("6");
            }
            if (e.KeyStates == Keyboard.GetKeyStates (Key.D7))
            {
                inputNum ("7");
            }
            if (e.KeyStates == Keyboard.GetKeyStates (Key.D8))
            {
                inputNum ("8");
            }
            if (e.KeyStates == Keyboard.GetKeyStates (Key.D9))
            {
                inputNum ("9");
            }
            //小数点
            if (e.KeyStates == Keyboard.GetKeyStates (Key.OemPeriod))
            {
                inputNum (".");
            }
            //键盘退格符
            if (e.KeyStates == Keyboard.GetKeyStates (Key.Back))
            {
                string strCurrentResult = textBlockResult.Text;
                if (!string.IsNullOrEmpty (strCurrentResult))
                    textBlockResult.Text = strCurrentResult.Substring (0, strCurrentResult.Length - 1);
                {
                    textBlockResult.Text = "0";
                    //textBlockExpression.Text = "";
                }
            }
            //键盘Del键全部清空
            if (e.KeyStates == Keyboard.GetKeyStates (Key.Delete))
            {
                textBlockExpression.Text = "";
                textBlockResult.Text = "0";
                m_strOperator = "";
                m_dResult = 0.0;
            }
        }
    }
}

------------恢复内容结束------------

原文地址:https://www.cnblogs.com/LiuFqiang/p/13429632.html