WPF实现键盘鼠标输入简易计算器
使用的开发工具为VS2010,简单操作如下:
首先创建项目,整体为单页面应用
在App.xaml中指明启动初始页面
q前台代码
MainWindow.xaml
"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"> "3*"> "2*"> "2*"> "2*"> "2*"> "2*"> "0" Grid.ColumnSpan="4" Height="Auto" Name="textBlockExpression" Margin="0,0,15,0" HorizontalAlignment="Right" FontSize="36" Width="Auto"/> "0" Grid.ColumnSpan="4" Height="60" Name="textBlockResult" Margin="0,0,15,0" HorizontalAlignment="Right" FontSize="36" Text="0" />
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 { ////// 计算器交互逻辑 /// public partial class MainWindow : Window { ////// 计算结果值 /// private double m_dResult = 0.0; ////// 运算符号标记 /// private string m_strOperator = ""; public MainWindow() { InitializeComponent (); } ////// 数字按钮鼠标点击事件方法 /// /// /// private void buttonNum_Click(object sender, RoutedEventArgs e) { if (m_strOperator == "=") defaultCalcluator (e); string strCurrentNum = ((Button)sender).Content.ToString (); inputNum (strCurrentNum); } ////// 键盘或鼠标输入数字方法 /// /// 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; } } } ////// 运算符号按钮鼠标点击事件方法 /// /// /// private void buttonMark_Click(object sender, RoutedEventArgs e) { string strCurrentMark = ((Button)sender).Content.ToString (); inputOperationMark (strCurrentMark); } ////// 键盘或鼠标输入运算符号方法 /// /// 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 (); } } } ////// 数值计算 /// /// 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; } } ////// 判断输入的计算表达式最后一位是运算符号 /// /// ///private bool isSufOperatorMark(string strMark) { string[] arrOperatorMark = { "+", "-", "×", "÷" }; string strCurrentExpression = textBlockExpression.Text; if (Array.IndexOf (arrOperatorMark, strMark) == -1) return false; return true; } /// /// “=”按钮点击事件方法 /// /// /// private void buttonEqual_Click(object sender, RoutedEventArgs e) { //防止打开计算器直接按等于 if (m_strOperator != "") { textBlockExpression.Text = textBlockExpression.Text + "="; OperationNum ("="); textBlockResult.Text = m_dResult.ToString (); } } ////// 退格按钮点击事件方法 /// /// /// 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 = ""; } } ////// 清空按钮点击事件方法 /// /// /// private void buttonClear_Click(object sender, RoutedEventArgs e) { defaultCalcluator (e); } ////// 计算器回归默认值 /// public void defaultCalcluator(RoutedEventArgs e) { textBlockExpression.Text = ""; textBlockResult.Text = "0"; m_strOperator = ""; m_dResult = 0.0; } ////// 键盘按键输入事件方法 /// /// /// 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