计算器和画图板的设计

目录:

  1. 计算器设计
  2. 画图板设计
  3. 整体总结和分析

前言:
本次应课程要求要用C++设计计算器和画图程序,我会使用Microsoft Visual Studio 2010来完成该窗体应用程序,如果有写的不好的地方希望大家批评指正。

1.计算器设计

结构:

  1. 界面设计
  2. 函数模块设计
  3. 整体功能实现

首先,我们可以先创建一个一个基于CLR的窗体应用程序。这里我就从零开始讲解,这样大家可以更好的明白,同时自己也可以加深记忆。

我们打开Visual Studio 2010,先打开项目,创建一个CLR窗体应用程序,输入程序名称,选择保存路径,点击确定。如图:

打开项目后,将会出现以下的界面:

我们选择CLR中的窗体用用程序,这里一定要注意,千万别选错,不然就得不到想要的结果了。我这里创建了一个SpecialCalculator 名称,大家也可以创建别的名称,但最好是英文名,这样对编程更有利,也可以帮助大家学习英语。(ง •_•)ง
上面都创建好了,我们就出现如下的界面了:

接下来我们来实现整体界面设计,构建一个真正的计算器!!!

1.界面设计

我们都知道,要实现计算器,最基本的就是要有一个计算器的输入框和数字符号按钮,我们用鼠标点击数字、符号和公式,然后再点击=符号,输入框就可以计算出正确的答案。这样我们就实现了一个简单的计算器。

上面提到了输入框,按钮等等,以上我们可以称为组件,我们无非就是在窗体上添加自己想要的组件,然后给每个组件设置监听器,让他们去做不同的事情罢了。
这里我们就要打开关于组件的视图:工具箱||属性

所谓工具箱,就是存放工具的地方。里面包括所有我们要用到的组件,包括按钮,文本框,画图板,菜单栏,单选框等等。我们点击某个组件就可以在界面上进行绘制。如果我们要精确调整,这就涉及到组件的属性,属性自然说组件的特色,比如按钮的大小可以设置,按钮的背景颜色,按钮显示的文字的颜色,按钮的字体,视图的滚条设置等等,大家可以慢慢摸索。

接下来我们都知道组件的使用和属性了,我们可以简单设计一下界面。用到的组件有按钮、如下:

这里省略很多步骤,但这些步骤都一些简单的步骤,比如自己调整按钮的布局,在属性设置编辑框的显示文本,编辑框的滚条设计,还有就是按钮的前景色和背景色的设置。大家慢慢设计就可以制作一个美观的计算器,至于具体这么样大家可以有自己的想法。

2.函数模块设计

上面我们完成一个还算美观的界面,但是计算器是要用来计算的,而上面的按钮现在还只是摆设,我们因该要点击按钮时可以实现显示公式和算出结果。这里就涉及到对每个按钮添加相应的事件,我们可以称之为事件监听机制。
那么怎么实现事件监听呢?这里我们用鼠标双击我们需要添加监听器的组件,如下:

#pragma endregion//界面private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {}//编辑框private: System::Void textBox1_TextChanged(System::Object^  sender, System::EventArgs^  e) {}//标签private: System::Void label1_Click(System::Object^  sender, System::EventArgs^  e) {}//按钮0
private: System::Void button18_Click(System::Object^  sender, System::EventArgs^  e) {//这里我们让编辑框现实的字符串加上字符0this->textBox1->Text=this->textBox1->Text+L"0";}

接下来我们运行程序来看看点击O按钮的效果:

我们这里就完成字符的显示,接下类我们一气呵成,把其他的按钮都实现相应功能,完整的代码如下:

//------------------------------------------------------------------------------------------------------//按钮0
private: System::Void button18_Click(System::Object^  sender, System::EventArgs^  e) {this->textBox1->Text=this->textBox1->Text+L"0";}//按钮1
private: System::Void button17_Click(System::Object^  sender, System::EventArgs^  e) {this->textBox1->Text=this->textBox1->Text+L"1";}//按钮2
private: System::Void button12_Click(System::Object^  sender, System::EventArgs^  e) {this->textBox1->Text=this->textBox1->Text+L"2";}//按钮3
private: System::Void button9_Click(System::Object^  sender, System::EventArgs^  e) {this->textBox1->Text=this->textBox1->Text+L"3";}//按钮4
private: System::Void button8_Click(System::Object^  sender, System::EventArgs^  e) {this->textBox1->Text=this->textBox1->Text+L"4";}//按钮5
private: System::Void button13_Click(System::Object^  sender, System::EventArgs^  e) {this->textBox1->Text=this->textBox1->Text+L"5";}//按钮6
private: System::Void button16_Click(System::Object^  sender, System::EventArgs^  e) {this->textBox1->Text=this->textBox1->Text+L"6";}//按钮7
private: System::Void button15_Click(System::Object^  sender, System::EventArgs^  e) {this->textBox1->Text=this->textBox1->Text+L"7";}//按钮8
private: System::Void button14_Click(System::Object^  sender, System::EventArgs^  e) {this->textBox1->Text=this->textBox1->Text+L"8";}//按钮9
private: System::Void button7_Click(System::Object^  sender, System::EventArgs^  e) {this->textBox1->Text=this->textBox1->Text+L"9";}//按钮.
private: System::Void button11_Click(System::Object^  sender, System::EventArgs^  e) {this->textBox1->Text=this->textBox1->Text+L".";}//按钮/
private: System::Void button4_Click(System::Object^  sender, System::EventArgs^  e) {this->textBox1->Text=this->textBox1->Text+L"/";}//按钮+
private: System::Void button6_Click(System::Object^  sender, System::EventArgs^  e) {this->textBox1->Text=this->textBox1->Text+L"+";}//按钮-
private: System::Void button5_Click(System::Object^  sender, System::EventArgs^  e) {this->textBox1->Text=this->textBox1->Text+L"-";}//按钮*
private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) {this->textBox1->Text=this->textBox1->Text+L"*";}//按钮(
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {this->textBox1->Text=this->textBox1->Text+L"(";}//按钮)
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {this->textBox1->Text=this->textBox1->Text+L")";}//按钮Sin
private: System::Void button22_Click(System::Object^  sender, System::EventArgs^  e) {this->textBox1->Text=this->textBox1->Text+L"Sin";}//按钮Cos
private: System::Void button21_Click(System::Object^  sender, System::EventArgs^  e) {this->textBox1->Text=this->textBox1->Text+L"Cos";}//按钮Tan
private: System::Void button20_Click(System::Object^  sender, System::EventArgs^  e) {this->textBox1->Text=this->textBox1->Text+L"Tan";}//按钮In
private: System::Void button19_Click(System::Object^  sender, System::EventArgs^  e) {this->textBox1->Text=this->textBox1->Text+L"In";}
//------------------------------------------------------------------------------------------------------//按钮BackSpace(删除)
private: System::Void button27_Click(System::Object^  sender, System::EventArgs^  e) {String^ s=this->textBox1->Text;this->textBox1->Text=s->Remove(s->Length-1);}//按钮DelUp(清空)
private: System::Void button28_Click(System::Object^  sender, System::EventArgs^  e) {String^ s=this->textBox1->Text;this->textBox1->Text=s->Remove(0);}//按钮=输出-----------------------------------
private: System::Void button10_Click(System::Object^  sender, System::EventArgs^  e) {this->textBox1->Text=this->textBox1->Text+L"=";//调用计算函数(计算器的核心)}
};
}

接下来我们键入公式,这里就需要根据公式来计算并显示答案,需要计算器的核心模块,计算函数函数构造:

1.首先,创建一个头文件来放置计算函数来供定义文件调用。
在出现的对话框中选中头文件(.h),并填写文件名,点击确定及创建一个头文件:

接下来就是编写函数代码:

using namespace System;//调用命名空间
//声名函数
String^ eatspaces(String^ str);//1.去掉输入公式中空格的函数
double expr(String^ str);//2.计算器的主函数计算+-
double term(String^ str,int^ index);//计算乘除函数
double number(String^ str,int^ index);//计算每一个数值
String^ extract(String^,int^);//抽取括号字符串函数
String^ exhsm(String^ int^);//取得三角函数字符串的函数//定义函数-------------------------------------------------------------//1.去掉输入公式中空格的函数
String^ eatspace(String^ str){return str->Replace(L" ",L"");//注意第一个参数有空格,第二个没有
}
//这里我们可以简单了解一下Replace函数,这个大家可以去相应文档去查找,它有两个参数
//都是字符或字符串,这里显然是字符,意思是遍历传入的字符串并检查有无出现第一个数
//设置的字符,这个代表空字符,若出现空字符则用""代替,也就是去除。//2.计算器的主函数,最后计算最基本的+,-
double expr(String^ str){int^ index(0);//定义一个index指针变量,初始值为0double value(term(str,index));//声名value双精度变量,以term
//函数作为参数,value可以理解为实时计算的过程值while(*index<str->Length){//判断index值是否大于传入公式的长度switch(str[*index]){//switch判断语句,按顺序判断传入公示的每一个字符//+case'+':++(*index);value+=term(str,index);break;case'-':++(*index);value+=term(str,index);break;default:Console::WriteLine(L"There is an error!!!\n");}}return value;
}//定义先计算的乘除函数
double term(String^ str,int^ index){double value(number(str,index));while(*index<str->Length){if(L'*'==str[*index]){++(*index);value*=number(str,index);}else if(L"/"==str[*index]){++(*index);value/=number(str,index);}else{break;}}return value;
}//返回每个表达式数字的函数
double number(String^ str,int^ index){//初始化初值double value(0.0);double sign(1.0);double mtd(0.0);String^ s2;//遇到()为特殊情况需要截取括号的内容作为一个表达式重新计算if(L'('==str[*index]){++(*index);String^ substr=extract(str,index);return expr(substr);}if((*index<str->Length)&&str[*index]==L'-'){sign=-1.0;(*index)++;}//Sin,Cos,Tan函数s2=exhsm(str,index);if(s2->Length>0){if(s2=L"Sin"){mtd=number(str,index);mtd=Math::Sin(mtd);return mtd;}elseif(s2=L"Cos"){mtd=number(str,index);mtd=Math::Cos(mtd);return mtd;}elseif(s2=L"Tan"){mtd=number(str,index);mtd=Math::Tan(mtd);return mtd;}}
//小数点符号while((*index<str->Length)&&Char::IsDigit(str,*index)){value=10.0*value+Char::GetNumericValue(str[(*index)]);++(*index);}if((*index==str->Length||str[*index]!='.')){return sign*value;double factor(1.0);++(*index);while((*index<str->Length)&&Char::IsDigit(str,*index)){factor*=0.1;value=value+Char::GetNumericValue(str[(*index)])*factor;++(*index);}return sign*value;
}//取得括号中的表达式的函数
String^ extract(String^ str,int^ index){int numL(0);int bufindex(*index);while(*index<str->Length){switch(str[*index]){case')':if(0==numL){return str->Substring(bufindex,(*index)++ -bufindex);}else{numL--;break;}case'(':numL++;break;}++(*index);}return "0";
}//取得字符串判断是否为三角函数的函数
String^ exhsm(String^ str,int^ index){String^ s1="";while(*index<str->Length&&!Char::IsDigit(str,*index)&&str[*index]!=L'('&&str[*index]!=L'-'){s1=s1+str[*index];(*index)++;return s1;}
}
//注意:
//上式中的IsDigit是判断该字符是否为十进制数
//上式中的GetNumericValue是将指定的数字 Unicode 字符转换为双精度浮点数
//上式中的Substring是它等于此实例中从 startIndex 开始的长度为 length 子
//字符
//串,如果 startIndex 等于此实例的长度且 length 为零,则为 Empty 

既然我们已经搭建好了计算器的主函数,接下来就是要去调用这些函数:
1.首先我们在类视图中去添加include代码,将头文件包含进来:

2.包含了头文件,我的窗体的类程序中就可以访问头文件中的函数了:

    //按钮=输出-----------------------------------
private: System::Void button10_Click(System::Object^  sender, System::EventArgs^  e) {//调用计算函数(计算器的核心)double result=expr(eatspaces(this->textBox1->Text));//调用计算函数this->textBox1->Text=this->textBox1->Text+L"="+result.ToString();//以字符串的形式输出}

通过上面的步骤,一个简单的计算器基本实现了。
示例:

3.整体功能实现

上面的计算器还有一些功能是不全的,比如指数函数对数函数没有实现,接下来我们拓展一下功能,实现对指数函数和对数函数的计算:

     if (s2== L"Sin"){mtd=number(str,index);mtd=Math::Sin(mtd);return mtd;}    if (s2== L"Cos"){mtd=number(str,index);mtd=Math::Cos(mtd);return mtd;}if (s2== L"Tan"){mtd=number(str,index);mtd=Math::Tan(mtd);return mtd;} //对数函数实现代码if(s2==L"In"){mtd=number(str,index);double x=mtd;double sum=0.0;int f=1.0;int flag=1;double y,y1,t;long i=1;if(x<0||x==0){mtd=0.0;}if(x>2){y=1/x;flag=-1;}else{y=x;y=y-1;y1=y;t=y;while((t>=0?t:-t)>=0.000001){sum+=t;y1=y1*y;i++;f=-1*f;t=f*y1/i;}mtd=flag*sum;}return mtd;}

上面的对数函数代码添加到三角函数这一块比较好,因为这里有一个识别前面的公式,放在这一块比较合适。上面的算法我这里就不用分析,就是用数学里的微积分来计算对数,大家可以自己推导一下。

接下来说一下指数函数的实现:

   //平方private: System::Void button24_Click(System::Object^  sender, System::EventArgs^  e) {this->textBox1->Text=this->textBox1->Text+L"^2"; }//立方private: System::Void button23_Click(System::Object^  sender, System::EventArgs^  e) {this->textBox1->Text=this->textBox1->Text+L"^3";}//指数private: System::Void button25_Click(System::Object^  sender, System::EventArgs^  e) {this->textBox1->Text=this->textBox1->Text+L"^";}

通过如上添加到的代码设置,我们看可以分析一下,指数运算是通过“^”符号来实现的,而且指数运算优先于加减,并且优于乘除,所以我们如果要在指数后面乘以或除以一个数时,我们因该加上括号:
下面展示如何实现指数运算:

      if(L'*'==str[*index]){++(*index);value *=number(str,index);}else if(L'/'==str[*index]){++(*index);value /=number(str,index);//指数运算}else if(L'^'==str[*index]){++(*index);value=Math::Pow(value,number(str,index));}elsebreak;}

由上面代码来看还是比较简单的,我们只要调用Math库里的指数函数就OK了。

现在基本的计算器功能已经实现了。如果大家还有什么问题或有什么想法的可以和我交流。

2.画图板设计

结构:

  1. 界面设计
  2. 画图函数实现
  3. 整体功能实现

这里我们首先创建一个窗体类项目,同上面创建计算器的一样,这里就不重复了。我这里命名为:SpecialGraphics

首先还是老套路,分析一下,我们要实现一个简单的画图程序,首先要有一些按钮,包含直线,圆等等,我们但我们点击直线,我们在窗体上按下松开鼠标可以画一条直线。

要实现上面的功能,我们需要按钮,画框这些组件,同时我们在画框添加鼠标监听器,实现事件的响应。

1.界面设计

1.首先,我们进行组件的添加和相关属性的设置:

下面我们要进行代码编写,实现对事件的响应:
这里我们整个的画图程序的代码完整的给出来,里面基本都有注释,大家可以参考一下:

#pragma once
namespace SpecialGraphics {using namespace System;using namespace System::ComponentModel;using namespace System::Collections;using namespace System::Windows::Forms;using namespace System::Data;using namespace System::Drawing;/// <summary>/// Form1 摘要/// </summary>public ref class Form1 : public System::Windows::Forms::Form{public:Form1(void){InitializeComponent();////TODO: 在此处添加构造函数代码//PBheight=this->pictureBox1->Height;//获取画板的高度PBwidth=this->pictureBox1->Width;//获取画板宽度myImage=gcnew Bitmap(PBwidth,PBheight);//设置画布g=Graphics::FromImage(myImage);//将位图作为画板backgroundColor=Color::Black;//背景颜色g->Clear(backgroundColor);//黑底画布pictureBox1->Image=myImage;//显示图像foreColor=Color::White;//画笔颜色penWeight=1.0;//画笔粗细pen=gcnew Pen(foreColor,penWeight);//初始化画笔line_startPoint=gcnew Point();//记录线段首点buttonString="直线";//默认直线brush = gcnew SolidBrush(backgroundColor);//初始化笔刷}protected:/// <summary>/// 清理所有正在使用的资源。/// </summary>~Form1(){if (components){delete components;}}private: System::Windows::Forms::Button^ button1;protected: private: System::Windows::Forms::Button^ button2;private: System::Windows::Forms::Button^ button3;private: System::Windows::Forms::PictureBox^  pictureBox1;private:/// <summary>/// 必需的设计器变量。/// </summary>System::ComponentModel::Container ^components;Image^ myImage;//定义一个位图Graphics^ g;//创建一个画图对象Pen^ pen;//画笔float penWeight;//画笔粗细static int PBheight,PBwidth;//记录画板长宽String^ buttonString;//按钮类型Point^ line_startPoint;//记录线段首点int x1,y1,x2,y2;//坐标Color backgroundColor,foreColor;//背景色和画笔颜色Brush^ brush;//笔刷,用于填充图形private: System::Windows::Forms::MenuStrip^  menuStrip1;private: System::Windows::Forms::ToolStripMenuItem^  系统ToolStripMenuItem;private: System::Windows::Forms::ToolStripMenuItem^  线性ToolStripMenuItem;private: System::Windows::Forms::ToolStripMenuItem^  颜色ToolStripMenuItem;private: System::Windows::Forms::ToolStripMenuItem^  编辑ToolStripMenuItem;private: System::Windows::Forms::ToolStripMenuItem^  插入图片ToolStripMenuItem;private: System::Windows::Forms::ToolStripMenuItem^  退出ToolStripMenuItem;private: System::Windows::Forms::ToolStripMenuItem^  细线ToolStripMenuItem;private: System::Windows::Forms::ToolStripMenuItem^  中线ToolStripMenuItem;private: System::Windows::Forms::ToolStripMenuItem^  粗线ToolStripMenuItem;private: System::Windows::Forms::ToolStripMenuItem^  背景颜色ToolStripMenuItem;private: System::Windows::Forms::ToolStripMenuItem^  画笔颜色ToolStripMenuItem;private: System::Windows::Forms::ToolStripMenuItem^  清屏ToolStripMenuItem;private: System::Windows::Forms::Button^  button4;private: System::Windows::Forms::Button^  button5;private: System::Windows::Forms::Button^  button6;private: System::Windows::Forms::Button^  button7;private: System::Windows::Forms::Button^  button8;private: System::Windows::Forms::Button^  button9;private: System::Windows::Forms::Button^  button10;private: System::Windows::Forms::ToolStripMenuItem^  橡皮擦ToolStripMenuItem;#pragma region Windows Form Designer generated code/// <summary>/// 设计器支持所需的方法 - 不要/// 使用代码编辑器修改此方法的内容。/// </summary>void InitializeComponent(void){this->button1 = (gcnew System::Windows::Forms::Button());this->button2 = (gcnew System::Windows::Forms::Button());this->button3 = (gcnew System::Windows::Forms::Button());this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());this->menuStrip1 = (gcnew System::Windows::Forms::MenuStrip());this->系统ToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());this->插入图片ToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());this->退出ToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());this->线性ToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());this->细线ToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());this->中线ToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());this->粗线ToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());this->颜色ToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());this->背景颜色ToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());this->画笔颜色ToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());this->编辑ToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());this->清屏ToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());this->橡皮擦ToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());this->button4 = (gcnew System::Windows::Forms::Button());this->button5 = (gcnew System::Windows::Forms::Button());this->button6 = (gcnew System::Windows::Forms::Button());this->button7 = (gcnew System::Windows::Forms::Button());this->button8 = (gcnew System::Windows::Forms::Button());this->button9 = (gcnew System::Windows::Forms::Button());this->button10 = (gcnew System::Windows::Forms::Button());(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1))->BeginInit();this->menuStrip1->SuspendLayout();this->SuspendLayout();// // button1// this->button1->Font = (gcnew System::Drawing::Font(L"宋体", 9, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(134)));this->button1->Location = System::Drawing::Point(0, 31);this->button1->Name = L"button1";this->button1->Size = System::Drawing::Size(75, 29);this->button1->TabIndex = 0;this->button1->Text = L"直线";this->button1->UseVisualStyleBackColor = true;this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);// // button2// this->button2->Font = (gcnew System::Drawing::Font(L"宋体", 9, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(134)));this->button2->Location = System::Drawing::Point(0, 66);this->button2->Name = L"button2";this->button2->Size = System::Drawing::Size(75, 29);this->button2->TabIndex = 1;this->button2->Text = L"圆";this->button2->UseVisualStyleBackColor = true;this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);// // button3// this->button3->Font = (gcnew System::Drawing::Font(L"宋体", 9, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(134)));this->button3->Location = System::Drawing::Point(0, 101);this->button3->Name = L"button3";this->button3->Size = System::Drawing::Size(75, 29);this->button3->TabIndex = 2;this->button3->Text = L"椭圆";this->button3->UseVisualStyleBackColor = true;this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);// // pictureBox1// this->pictureBox1->BackColor = System::Drawing::Color::Black;this->pictureBox1->Cursor = System::Windows::Forms::Cursors::Default;this->pictureBox1->Location = System::Drawing::Point(75, 31);this->pictureBox1->Name = L"pictureBox1";this->pictureBox1->Size = System::Drawing::Size(1107, 723);this->pictureBox1->TabIndex = 3;this->pictureBox1->TabStop = false;this->pictureBox1->MouseDown += gcnew System::Windows::Forms::MouseEventHandler(this, &Form1::pictureBox1_MouseDown);this->pictureBox1->MouseMove += gcnew System::Windows::Forms::MouseEventHandler(this, &Form1::pictureBox1_MouseMove);this->pictureBox1->MouseUp += gcnew System::Windows::Forms::MouseEventHandler(this, &Form1::pictureBox1_MouseUp);// // menuStrip1// this->menuStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(4) {this->系统ToolStripMenuItem, this->线性ToolStripMenuItem, this->颜色ToolStripMenuItem, this->编辑ToolStripMenuItem});this->menuStrip1->Location = System::Drawing::Point(0, 0);this->menuStrip1->Name = L"menuStrip1";this->menuStrip1->Size = System::Drawing::Size(1182, 28);this->menuStrip1->TabIndex = 4;this->menuStrip1->Text = L"menuStrip1";// // 系统ToolStripMenuItem// this->系统ToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(2) {this->插入图片ToolStripMenuItem, this->退出ToolStripMenuItem});this->系统ToolStripMenuItem->Name = L"系统ToolStripMenuItem";this->系统ToolStripMenuItem->Size = System::Drawing::Size(51, 24);this->系统ToolStripMenuItem->Text = L"系统";// // 插入图片ToolStripMenuItem// this->插入图片ToolStripMenuItem->Name = L"插入图片ToolStripMenuItem";this->插入图片ToolStripMenuItem->Size = System::Drawing::Size(138, 24);this->插入图片ToolStripMenuItem->Text = L"插入图片";// // 退出ToolStripMenuItem// this->退出ToolStripMenuItem->Name = L"退出ToolStripMenuItem";this->退出ToolStripMenuItem->Size = System::Drawing::Size(152, 24);this->退出ToolStripMenuItem->Text = L"退出";this->退出ToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::退出ToolStripMenuItem_Click);// // 线性ToolStripMenuItem// this->线性ToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(3) {this->细线ToolStripMenuItem, this->中线ToolStripMenuItem, this->粗线ToolStripMenuItem});this->线性ToolStripMenuItem->Name = L"线性ToolStripMenuItem";this->线性ToolStripMenuItem->Size = System::Drawing::Size(51, 24);this->线性ToolStripMenuItem->Text = L"线性";// // 细线ToolStripMenuItem// this->细线ToolStripMenuItem->Name = L"细线ToolStripMenuItem";this->细线ToolStripMenuItem->Size = System::Drawing::Size(108, 24);this->细线ToolStripMenuItem->Text = L"细线";this->细线ToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::细线ToolStripMenuItem_Click);// // 中线ToolStripMenuItem// this->中线ToolStripMenuItem->Name = L"中线ToolStripMenuItem";this->中线ToolStripMenuItem->Size = System::Drawing::Size(108, 24);this->中线ToolStripMenuItem->Text = L"中线";this->中线ToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::中线ToolStripMenuItem_Click);// // 粗线ToolStripMenuItem// this->粗线ToolStripMenuItem->Name = L"粗线ToolStripMenuItem";this->粗线ToolStripMenuItem->Size = System::Drawing::Size(108, 24);this->粗线ToolStripMenuItem->Text = L"粗线";this->粗线ToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::粗线ToolStripMenuItem_Click);// // 颜色ToolStripMenuItem// this->颜色ToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(2) {this->背景颜色ToolStripMenuItem, this->画笔颜色ToolStripMenuItem});this->颜色ToolStripMenuItem->Name = L"颜色ToolStripMenuItem";this->颜色ToolStripMenuItem->Size = System::Drawing::Size(51, 24);this->颜色ToolStripMenuItem->Text = L"颜色";// // 背景颜色ToolStripMenuItem// this->背景颜色ToolStripMenuItem->Name = L"背景颜色ToolStripMenuItem";this->背景颜色ToolStripMenuItem->Size = System::Drawing::Size(138, 24);this->背景颜色ToolStripMenuItem->Text = L"背景颜色";this->背景颜色ToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::背景颜色ToolStripMenuItem_Click);// // 画笔颜色ToolStripMenuItem// this->画笔颜色ToolStripMenuItem->Name = L"画笔颜色ToolStripMenuItem";this->画笔颜色ToolStripMenuItem->Size = System::Drawing::Size(138, 24);this->画笔颜色ToolStripMenuItem->Text = L"画笔颜色";this->画笔颜色ToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::画笔颜色ToolStripMenuItem_Click);// // 编辑ToolStripMenuItem// this->编辑ToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(2) {this->清屏ToolStripMenuItem, this->橡皮擦ToolStripMenuItem});this->编辑ToolStripMenuItem->Name = L"编辑ToolStripMenuItem";this->编辑ToolStripMenuItem->Size = System::Drawing::Size(51, 24);this->编辑ToolStripMenuItem->Text = L"编辑";// // 清屏ToolStripMenuItem// this->清屏ToolStripMenuItem->Name = L"清屏ToolStripMenuItem";this->清屏ToolStripMenuItem->Size = System::Drawing::Size(152, 24);this->清屏ToolStripMenuItem->Text = L"清屏";this->清屏ToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::清屏ToolStripMenuItem_Click);// // 橡皮擦ToolStripMenuItem// this->橡皮擦ToolStripMenuItem->Name = L"橡皮擦ToolStripMenuItem";this->橡皮擦ToolStripMenuItem->Size = System::Drawing::Size(152, 24);this->橡皮擦ToolStripMenuItem->Text = L"橡皮擦";this->橡皮擦ToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::橡皮擦ToolStripMenuItem_Click);// // button4// this->button4->Font = (gcnew System::Drawing::Font(L"宋体", 9, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(134)));this->button4->Location = System::Drawing::Point(0, 164);this->button4->Name = L"button4";this->button4->Size = System::Drawing::Size(75, 29);this->button4->TabIndex = 5;this->button4->Text = L"细线";this->button4->UseVisualStyleBackColor = true;this->button4->Click += gcnew System::EventHandler(this, &Form1::button4_Click);// // button5// this->button5->Font = (gcnew System::Drawing::Font(L"宋体", 9, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(134)));this->button5->Location = System::Drawing::Point(0, 199);this->button5->Name = L"button5";this->button5->Size = System::Drawing::Size(75, 29);this->button5->TabIndex = 6;this->button5->Text = L"中线";this->button5->UseVisualStyleBackColor = true;this->button5->Click += gcnew System::EventHandler(this, &Form1::button5_Click);// // button6// this->button6->Font = (gcnew System::Drawing::Font(L"宋体", 9, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(134)));this->button6->Location = System::Drawing::Point(0, 234);this->button6->Name = L"button6";this->button6->Size = System::Drawing::Size(75, 29);this->button6->TabIndex = 7;this->button6->Text = L"粗线";this->button6->UseVisualStyleBackColor = true;this->button6->Click += gcnew System::EventHandler(this, &Form1::button6_Click);// // button7// this->button7->Font = (gcnew System::Drawing::Font(L"宋体", 9, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(134)));this->button7->Location = System::Drawing::Point(0, 279);this->button7->Name = L"button7";this->button7->Size = System::Drawing::Size(75, 29);this->button7->TabIndex = 8;this->button7->Text = L"前景色";this->button7->UseVisualStyleBackColor = true;this->button7->Click += gcnew System::EventHandler(this, &Form1::button7_Click);// // button8// this->button8->Font = (gcnew System::Drawing::Font(L"宋体", 9, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(134)));this->button8->Location = System::Drawing::Point(0, 314);this->button8->Name = L"button8";this->button8->Size = System::Drawing::Size(75, 29);this->button8->TabIndex = 9;this->button8->Text = L"背景色";this->button8->UseVisualStyleBackColor = true;this->button8->Click += gcnew System::EventHandler(this, &Form1::button8_Click);// // button9// this->button9->Font = (gcnew System::Drawing::Font(L"宋体", 9, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(134)));this->button9->Location = System::Drawing::Point(0, 443);this->button9->Name = L"button9";this->button9->Size = System::Drawing::Size(75, 29);this->button9->TabIndex = 10;this->button9->Text = L"清屏";this->button9->UseVisualStyleBackColor = true;this->button9->Click += gcnew System::EventHandler(this, &Form1::button9_Click);// // button10// this->button10->Font = (gcnew System::Drawing::Font(L"宋体", 9, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(134)));this->button10->Location = System::Drawing::Point(0, 478);this->button10->Name = L"button10";this->button10->Size = System::Drawing::Size(75, 29);this->button10->TabIndex = 11;this->button10->Text = L"橡皮擦";this->button10->UseVisualStyleBackColor = true;this->button10->Click += gcnew System::EventHandler(this, &Form1::button10_Click);// // Form1// this->AutoScaleDimensions = System::Drawing::SizeF(8, 15);this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;this->BackColor = System::Drawing::SystemColors::ControlDark;this->ClientSize = System::Drawing::Size(1182, 753);this->Controls->Add(this->button10);this->Controls->Add(this->button9);this->Controls->Add(this->button8);this->Controls->Add(this->button7);this->Controls->Add(this->button6);this->Controls->Add(this->button5);this->Controls->Add(this->button4);this->Controls->Add(this->pictureBox1);this->Controls->Add(this->button3);this->Controls->Add(this->button2);this->Controls->Add(this->button1);this->Controls->Add(this->menuStrip1);this->MainMenuStrip = this->menuStrip1;this->Name = L"Form1";this->Text = L"多功能画图板---抽象派画家";this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1))->EndInit();this->menuStrip1->ResumeLayout(false);this->menuStrip1->PerformLayout();this->ResumeLayout(false);this->PerformLayout();}#pragma endregion//背景颜色private: System::Void 背景颜色ToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {ColorDialog^ colorDialog =gcnew ColorDialog();// Keeps the user from selecting a custom color.colorDialog->AllowFullOpen = false ;// Allows the user to get help. (The default is false.)帮助colorDialog->ShowHelp = true ;// Sets the initial color select to the current text color.//colorDialog->Color = textBox1.ForeColor ;//上面是一些默认设置可以不用管if (colorDialog->ShowDialog()==Windows::Forms::DialogResult::OK){//获取所选择的颜色backgroundColor= colorDialog->Color;g->Clear(backgroundColor);this->button8->BackColor=backgroundColor;pictureBox1->Image=myImage;}}//画笔颜色private: System::Void 画笔颜色ToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {          ColorDialog^ colorDialog =gcnew ColorDialog();// Keeps the user from selecting a custom color.colorDialog->AllowFullOpen = false ;// Allows the user to get help. (The default is false.)帮助colorDialog->ShowHelp = true ;// Sets the initial color select to the current text color.//colorDialog->Color = textBox1.ForeColor ;//上面是一些默认设置可以不用管if (colorDialog->ShowDialog()==Windows::Forms::DialogResult::OK){//获取所选择的颜色foreColor=colorDialog->Color;pen->Color=foreColor;this->button7->BackColor=foreColor;pictureBox1->Image=myImage;}           }//界面private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {}//直线private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {buttonString=L"直线";}//圆private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {buttonString=L"圆";}//椭圆private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) {buttonString=L"椭圆";}
//---------------------------------------------鼠标事件
//画板的鼠标按下事件
private: System::Void pictureBox1_MouseDown(System::Object^  sender,System::Windows::Forms::MouseEventArgs^  e) {//记录首点坐标line_startPoint->X=e->X;line_startPoint->Y=e->Y;x1=e->X;y1=e->Y;//判断字符串if(buttonString=="圆"){pen->Color=foreColor;g->DrawEllipse(pen,x1-25,y1-25,50,50);pictureBox1->Image=myImage;}elseif(buttonString=="椭圆"){pen->Color=foreColor;g->DrawEllipse(pen,x1-40,y1-25,80,50);pictureBox1->Image=myImage;}}//画板的鼠标松开事件
private: System::Void pictureBox1_MouseUp(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e) {//判断if(buttonString=="直线"){Point^ line_endPoint=gcnew Point();//记录尾点坐标line_endPoint->X=e->X;line_endPoint->Y=e->Y;pen->Color=foreColor;g->DrawLine(pen,*line_startPoint,*line_endPoint);pictureBox1->Image=myImage;}}
//画板的鼠标移动事件
private: System::Void pictureBox1_MouseMove(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e) {x1=e->X;y1=e->Y;//判断if(buttonString=="橡皮"){brush = gcnew SolidBrush(backgroundColor);//初始化笔刷g->FillRectangle(brush,x1,y1,15,15);pictureBox1->Image=myImage;}}
//清屏
private: System::Void button9_Click(System::Object^  sender, System::EventArgs^  e) {g->Clear(backgroundColor);pictureBox1->Image=myImage;}
//橡皮
private: System::Void button10_Click(System::Object^  sender, System::EventArgs^  e) {buttonString=L"橡皮";}
//细线
private: System::Void button4_Click(System::Object^  sender, System::EventArgs^  e) {penWeight=1.0;pen=gcnew Pen(foreColor,penWeight);//初始化画笔}
//中线
private: System::Void button5_Click(System::Object^  sender, System::EventArgs^  e) {penWeight=2.0;pen=gcnew Pen(foreColor,penWeight);//初始化画笔}
//粗线
private: System::Void button6_Click(System::Object^  sender, System::EventArgs^  e) {penWeight=3.0;pen=gcnew Pen(foreColor,penWeight);//初始化画笔}//前景色
private: System::Void button7_Click(System::Object^  sender, System::EventArgs^  e) {ColorDialog^ colorDialog =gcnew ColorDialog();// Keeps the user from selecting a custom color.colorDialog->AllowFullOpen = false ;// Allows the user to get help. (The default is false.)帮助colorDialog->ShowHelp = true ;// Sets the initial color select to the current text color.//colorDialog->Color = textBox1.ForeColor ;//上面是一些默认设置可以不用管if (colorDialog->ShowDialog()==Windows::Forms::DialogResult::OK){//获取所选择的颜色foreColor=colorDialog->Color;pen->Color=foreColor;this->button7->BackColor=foreColor;pictureBox1->Image=myImage;} }
//背景色
private: System::Void button8_Click(System::Object^  sender, System::EventArgs^  e) {ColorDialog^ colorDialog =gcnew ColorDialog();// Keeps the user from selecting a custom color.colorDialog->AllowFullOpen = false ;// Allows the user to get help. (The default is false.)帮助colorDialog->ShowHelp = true ;// Sets the initial color select to the current text color.//colorDialog->Color = textBox1.ForeColor ;//上面是一些默认设置可以不用管if (colorDialog->ShowDialog()==Windows::Forms::DialogResult::OK){//获取所选择的颜色backgroundColor= colorDialog->Color;g->Clear(backgroundColor);this->button8->BackColor=backgroundColor;pictureBox1->Image=myImage;}}
//菜单栏设置
//细线
private: System::Void 细线ToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {penWeight=1.0;pen=gcnew Pen(foreColor,penWeight);//初始化画笔}
//中线
private: System::Void 中线ToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {penWeight=2.0;pen=gcnew Pen(foreColor,penWeight);//初始化画笔}
//粗线
private: System::Void 粗线ToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {penWeight=3.0;pen=gcnew Pen(foreColor,penWeight);//初始化画笔}
//清屏
private: System::Void 清屏ToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {g->Clear(backgroundColor);pictureBox1->Image=myImage;}
//橡皮
private: System::Void 橡皮擦ToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {buttonString=L"橡皮";}
//退出
private: System::Void 退出ToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {this->Visible=false;}
};
}

注意:
1.在上述画图程序中定义全局变量时,应在Form1类中进行,而且不能初始化,否则会报错:

 private://Form1类的位置/// <summary>/// 必需的设计器变量。/// </summary>System::ComponentModel::Container ^components;Image^ myImage;//定义一个位图Graphics^ g;//创建一个画图对象Pen^ pen;//画笔float penWeight;//画笔粗细static int PBheight,PBwidth;//记录画板长宽String^ buttonString;//按钮类型Point^ line_startPoint;//记录线段首点int x1,y1,x2,y2;//坐标Color backgroundColor,foreColor;//背景色和画笔颜色Brush^ brush;//笔刷,用于填充图形

2.在Form1构造函数可以进行初始化:

  Form1(void){//构造函数模块InitializeComponent();////TODO: 在此处添加构造函数代码//PBheight=this->pictureBox1->Height;//获取画板的高度PBwidth=this->pictureBox1->Width;//获取画板宽度myImage=gcnew Bitmap(PBwidth,PBheight);//设置画布g=Graphics::FromImage(myImage);//将位图作为画板backgroundColor=Color::Black;//背景颜色g->Clear(backgroundColor);//黑底画布pictureBox1->Image=myImage;//显示图像foreColor=Color::White;//画笔颜色penWeight=1.0;//画笔粗细pen=gcnew Pen(foreColor,penWeight);//初始化画笔line_startPoint=gcnew Point();//记录线段首点buttonString="直线";//默认直线brush = gcnew SolidBrush(backgroundColor);//初始化笔刷}
2.画图函数实现

画图函数这一块其实不是特别难,大家看看Visual C++的文档基本就能够搞定,主要就是要在画图板上添加一些鼠标监听器,鼠标按下、松开、移动等等,以自己的需求而定,上面我也有相应的代码操作。
鼠标监听器文档:

3.整体功能实现

上面基本实现了画图,调整画笔、背景颜色,清屏、橡皮擦等功能。我们其实还可以添加一些别的功能,我的想法是实现插入图片,画一些迭代图形等,大家可以创建一个别的图形。

3.整体总结和分析

上面的两个小程序总体的实现难度不大,关键大家要懂得模仿吧!
建议不要复制粘贴,要自己一行一敲,这样可以加深理解,拿来主义很重要!同时要多去看看文档里的代码的类和成员,以及构造函数,类里面代码我们要抓住核心。

最后,大家如果有什么问题可以和我讨论呀!!!

【Microsoft Visual Studio 2010完成CLR窗体应用程序】计算器和画图板的设计相关推荐

  1. Microsoft Visual Studio 2010

    VS 2010的全称是Microsoft Visual Studio 2010,它是由微软公司所推出的一款开发环境,它不仅仅可以支持C#.C++ .VB等许多种开发语言,而且还可以用于创建Window ...

  2. Microsoft Visual Studio 2010 Service Pack 1

    官方页面 http://www.microsoft.com/downloads/zh-cn/details.aspx?FamilyID=75568aa6-8107-475d-948a-ef22627e ...

  3. Microsoft Visual Studio 2010 破解下载!

    1.Microsoft Visual Studio 2010下载 专业版(Professional)http://download.microsoft.com/download/4/0/E/40EFE ...

  4. Download Microsoft Visual Studio 2010 Ultimate Trial - ISO from Official Microsoft Download Center

    Download Microsoft Visual Studio 2010 Ultimate Trial - ISO from Official Microsoft Download Center M ...

  5. C语言的环境搭建(Microsoft Visual Studio 2010的介绍与安装)

    C语言的环境搭建 一.常用的环境 : 1.Microsoft Visual C++ (简称Visual C++.MSVC.VC++或VC)微软公司的C++开发工具,具有集成开发环境,可提供编辑C语言, ...

  6. Microsoft Visual Studio 2010(VS2010)秘钥

    Microsoft Visual Studio 2010(VS2010)正式版 CDKEY / SN: YCFHQ-9DWCY-DKV88-T2TMH-G7BHP 企业版.旗舰版

  7. Microsoft Visual Studio 2010 Service Pack 1 离线下载版

    家里网太慢了,终于找到离线安装的链接了.如下是iso和直接下载安装的版本(稍后奉上): Microsoft Visual Studio 2010 Service Pack 1 官方离线下载版(ISO) ...

  8. Microsoft Visual Studio 2010安装包、创建工程(一)

    对于每一个从事电子/计算机行业的员工,C语言都是一门必修课,那么什么C语言呢?C语言是一门计算机语言,所谓计算机语言就是人和计算机交流的语言.C语言是计算机语言之一,它还有C++.JAVA.Pytho ...

  9. Win7 64位安装VS2010旗舰版出现错误...Microsoft Visual Studio 2010 64bitPrerequisites (x64)

    本文是为了备忘在Win7 64位系统中安装Visual Studio 2010旗舰版出现错误并多次得不到解决,最终寻得解决方法. 电脑装的是Win7 64位旗舰版系统,安装Visual Studio ...

最新文章

  1. 英文版Windows XP操作系统的中文支持设置
  2. 查看linux上面是否有安装redis,redis启动
  3. matlab simulink 四分之一1/4车辆垂向振动模型 轮毂电机
  4. 获取dbgrid的行索引
  5. 剑指offer之青蛙跳台阶问题
  6. string 找出所有数字 index_发现规律,解决整数转罗马数字
  7. 【JS 逆向百例】网洛者反爬练习平台第五题:控制台反调试
  8. 跟我一起学习ASP.NET 4.5 MVC4.0(一)
  9. Ulua_toLua_基本案例(六)_LuaCoroutine2
  10. 优秀的用户体验设计,从讲好一个故事开始
  11. 汇编 LED驱动 烧写bin文件到SD卡
  12. 显示隐藏文件夹,Mac显示.m2文件夹
  13. 11 风险管理 人人都是项目经理系列(第11/13篇)
  14. 【学习资源】光学、物理类、电子学实验合集
  15. 第二章 Spring MVC入门 —— 跟开涛学SpringMVC
  16. 计算机网络中数据包的分布,计算机网络习题
  17. 【关于Endnotes】
  18. adb wifi 连接设备
  19. 给百度地图每个市添加颜色
  20. Squid 代理服务之日志分析 --- sarg 软件的安装及应用

热门文章

  1. MySQL及Navicat的安装和使用
  2. Linux编程入门四进程
  3. SAP中SD交货与WM下架功能的集成应用
  4. qq飞车前瞻版服务器维护,QQ飞车手游前瞻版
  5. C语言,从联合看字节序
  6. 自建免费的代理ip池
  7. RASA智能聊天机器人,理论与技术
  8. fluid 如何获取特定层的参数
  9. 最新手机号码检验正则表达式
  10. C#调用迅雷ThunderAgentLib.dll批量添加下载任务