用小画家制作了,加,减,乘,除,0,1,2,3,4,5,6,7,8,9,=,(,)等,手绘的,48*48位图,保存为.bmp形式。
加载是这样的,好像抄写连连看程序时,抄过,直接上代码:
string temp = System.Environment.CurrentDirectory;
bmp[0] = (Bitmap)Bitmap.FromFile(temp + "\\加.bmp");
bmp[1] = (Bitmap)Bitmap.FromFile(temp + "\\减.bmp");
bmp[2] = (Bitmap)Bitmap.FromFile(temp + "\\乘.bmp");
bmp[3] = (Bitmap)Bitmap.FromFile(temp + "\\除.bmp");
bmp[4] = (Bitmap)Bitmap.FromFile(temp + "\\零.bmp");
bmp[5] = (Bitmap)Bitmap.FromFile(temp + "\\一.bmp");
bmp[6] = (Bitmap)Bitmap.FromFile(temp + "\\二.bmp");
bmp[7] = (Bitmap)Bitmap.FromFile(temp + "\\三.bmp");
for (int i = 0; i < 8; i++)
{
rcs[i] = new System.Drawing.Rectangle();
}
//矩形=new rectangle(左,顶,右,底);
rcs[0] = new Rectangle((int)mytoolbox.m_RoiBase1.rcWin.Left, (int)mytoolbox.m_RoiBase1.rcWin.Top, 50, 50);
rcs[1] = new Rectangle((int)mytoolbox.m_RoiBase1.m_Center.X, (int)mytoolbox.m_RoiBase1.rcWin.Top, 50, 50);
rcs[2] = new Rectangle((int)mytoolbox.m_RoiBase1.rcWin.Left, (int)mytoolbox.m_RoiBase1.m_Center.Y - 50, 50, 50);
rcs[3] = new Rectangle((int)mytoolbox.m_RoiBase1.m_Center.X, (int)mytoolbox.m_RoiBase1.m_Center.Y - 50, 50, 50);
rcs[4] = new Rectangle((int)mytoolbox.m_RoiBase1.rcWin.Left, (int)mytoolbox.m_RoiBase1.m_Center.Y, 50, 50);
rcs[5] = new Rectangle((int)mytoolbox.m_RoiBase1.m_Center.X, (int)mytoolbox.m_RoiBase1.m_Center.Y, 50, 50);
rcs[6] = new Rectangle((int)mytoolbox.m_RoiBase1.rcWin.Left, (int)mytoolbox.m_RoiBase1.m_Center.Y + 50, 50, 50);
rcs[7] = new Rectangle((int)mytoolbox.m_RoiBase1.m_Center.X, (int)mytoolbox.m_RoiBase1.m_Center.Y + 50, 50, 50);
然后放到想要的位置,在paint函数中,
Graphics g = e.Graphics;
Pen p = new Pen(Color.Green, 1); //mytolbox是我自制的工具箱,
mytoolbox.drawtoolbox(g, p);//你可以看到上面的8个矩形覆盖在8个位图上。便于鼠标点击定位。
//DrawImage(Image img ,float x,float y)
g.DrawImage(bmp[0], mytoolbox.m_RoiBase1.rcWin.Left, mytoolbox.m_RoiBase1.rcWin.Top);
g.DrawImage(bmp[1], mytoolbox.m_RoiBase1.m_Center.X, mytoolbox.m_RoiBase1.rcWin.Top);
g.DrawImage(bmp[2], mytoolbox.m_RoiBase1.rcWin.Left, mytoolbox.m_RoiBase1.m_Center.Y - 50);
g.DrawImage(bmp[3], mytoolbox.m_RoiBase1.m_Center.X, mytoolbox.m_RoiBase1.m_Center.Y - 50);
g.DrawImage(bmp[4], mytoolbox.m_RoiBase1.rcWin.Left, mytoolbox.m_RoiBase1.m_Center.Y );
g.DrawImage(bmp[5], mytoolbox.m_RoiBase1.m_Center.X, mytoolbox.m_RoiBase1.m_Center.Y );
g.DrawImage(bmp[6], mytoolbox.m_RoiBase1.rcWin.Left, mytoolbox.m_RoiBase1.m_Center.Y + 50);
g.DrawImage(bmp[7], mytoolbox.m_RoiBase1.m_Center.X, mytoolbox.m_RoiBase1.m_Center.Y + 50);
这就完了?说完了就完了,说没有就没有,一直惦记的vc++中的res资源加载也是这样的呢?马上添加一个工具button(分割),在C#中验证,以下是button拖到form后,自动产生的代码在InitializeComponent()中:
this.分割 = new System.Windows.Forms.Button();
// 分割
//
this.分割.Location = new System.Drawing.Point(1031, 577);
this.分割.Name = "分割";
this.分割.Size = new System.Drawing.Size(96, 52);
this.分割.TabIndex = 338;
this.分割.Text = "计算点分割线";
this.分割.UseVisualStyleBackColor = true;
this.分割.Click += new System.EventHandler(this.分割_Click);
InitializeComponent()外是: private System.Windows.Forms.Button 分割;
在form中有: private void 分割_Click(object sender, EventArgs e) {}
如果我们手动添加是怎样的呢?
先声明, private System.Windows.Forms.Button button1;
再初始化:
this.button1 = new System.Windows.Forms.Button();
// this.button1.Image = ((System.Drawing.Image)( Bitmap.FromFile("")));
this.button1.Image = ((System.Drawing.Image)(Bitmap.FromFile(temp + "\\加.bmp")));
this.button1.Location = new System.Drawing.Point(250, 152);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(48, 48);
this.button1.TabIndex = 26;
// this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
this.panel1.Controls.Add(this.button1);
看到没有,我们使用矩形框定位鼠标事件和位图加载完全可以替代按钮button1,因为响应函数我们也可以用委托的方式替代,
又一个微软的利器被我突破了,惦记好多年了,从c++时代起,就这样不经意的突破了,突破了。先突破C#,再突破C++,时间不会太久。总结这个写给父亲的语音计算器,另一个突破是,接触到了编译原理,这不是纸上谈兵,是实战。有机会,还是要补上编译原理这一课。
另外这个程序还可以扩展:
1,能否增加编程功能,像foxbase或basic一样?
2,语音绘图键盘能否响应实体键盘?
3,更进一步,变成语音识别计算器?
4,是否把连连看小游戏移植过来,使用绘图汉字,而非动物肖像,增加语音,这样就可以增加母亲识字。
2,4是举手之劳,1,3还得下功夫。
待续(慢慢来!...........)每天一点小改变☺