基于winform实现的仿windows画板绘图程序

Feelme

发布日期: 2019-03-20 16:11:39 浏览量: 531
评分:
star star star star star star star star star star_border
*转载请注明来自write-bug.com

1、设计目标和要求

  • 用面向对象技术实现一个类似于Windows画板的程序

  • 要求实现生成图形的位图文件和JPEG文件保存和打开功能

2、编译与运行说明

  • Windows操作系统

  • VS2008下编译

  • 装有.Net FrameWork 3.5及以上版本运行库

  • 运行时exe文件需与光标文件getcolor.cur、paint.cur、pen.cur在同一目录下,否则会报错

3、算法原理介绍

本程序图形界面采用.net framework 3.5中的winform制作而成,用bitmap类对象存储图像,用picturebox控件显示图像,用Graphics类对象修改图像,用鼠标事件响应鼠标操作,用绘图事件进行即时反馈(绘图交互)。

实现了:调整颜色、打开保存(支持bmp、gif、exif、jpg、png、tiff格式)、铅笔、画刷、线段、圆形、矩形、取色器、油漆桶、橡皮等功能。其中,线段、圆形、矩形带有画图即时反馈,圆形、矩形支持空心于实心两种模式;油漆桶采用种子填充算法;画刷、橡皮支持调整大小;铅笔、画刷、橡皮由于鼠标事件MouseMove的响应频率不足使得取到的点断断续续,采用将相邻的点用直线联接起来,实际测试效果良好;所有工具均用光标自定义或绘图事件的方式实现了,光标在绘图区内的变化。

4、系统设计和实现

  1. //Form1.h
  2. #pragma once
  3. namespace 关键计算机图形学课程设计{
  4. using namespace System;
  5. using namespace System::ComponentModel;
  6. using namespace System::Collections;
  7. using namespace System::Windows::Forms;
  8. using namespace System::Data;
  9. using namespace System::Drawing;
  10. public ref class Form1 : public System::Windows::Forms::Form
  11. {
  12. public:
  13. Form1(void);//构造函数
  14. protected:
  15. /// <summary>
  16. /// 清理所有正在使用的资源。
  17. /// </summary>
  18. ~Form1()//析构函数
  19. //以下是各控件、对象及用到变量声明
  20. private: System::Windows::Forms::ToolStripPanel^ BottomToolStripPanel;
  21. protected:
  22. private: System::Windows::Forms::ToolStripPanel^ TopToolStripPanel;
  23. private: System::Windows::Forms::ToolStripPanel^ RightToolStripPanel;
  24. private: System::Windows::Forms::ToolStripPanel^ LeftToolStripPanel;
  25. private: System::Windows::Forms::ToolStripContentPanel^ ContentPanel;
  26. private: System::Windows::Forms::MenuStrip^ menuStrip1;
  27. private: System::Windows::Forms::ToolStrip^ toolStrip1;
  28. private: System::Windows::Forms::ToolStripButton^ brush;
  29. private: System::Windows::Forms::ToolStripMenuItem^ 文件ToolStripMenuItem;
  30. private: System::Windows::Forms::ToolStripMenuItem^ 新建ToolStripMenuItem;
  31. private: System::Windows::Forms::ToolStripMenuItem^ 打开ToolStripMenuItem;
  32. private: System::Windows::Forms::ToolStripMenuItem^ 保存ToolStripMenuItem;
  33. private: System::Windows::Forms::PictureBox^ pictureBox1;
  34. private: System::Windows::Forms::ColorDialog^ colorDialog1;
  35. private: System::Windows::Forms::OpenFileDialog^ openFileDialog1;
  36. private: System::Windows::Forms::SaveFileDialog^ saveFileDialog1;
  37. private: System::Windows::Forms::ToolStrip^ toolStrip2;
  38. private: System::Windows::Forms::ToolStripButton^ circle;
  39. private: array<Bitmap^> ^b;
  40. private: Graphics ^gra;
  41. private: System::Windows::Forms::Panel^ panel1;
  42. private: System::Windows::Forms::TableLayoutPanel^ tableLayoutPanel1;
  43. private: System::Windows::Forms::PictureBox^ pictureBox2;
  44. private: Bitmap ^pre;
  45. private: Graphics ^gp;
  46. private: Pen ^p;
  47. private: Color ^c1,^c2;
  48. private: int fx,fy,tx,ty,no,f,t,size;
  49. static const int mo=100;
  50. bool ld,rd,cv;
  51. array<Int32> ^lx,^ly;
  52. private: System::Windows::Forms::ToolStripMenuItem^ 编辑ToolStripMenuItem;
  53. private: System::Windows::Forms::ToolStripMenuItem^ 撤销ToolStripMenuItem;
  54. private: System::Windows::Forms::ToolStripMenuItem^ 重复ToolStripMenuItem;
  55. private: System::Windows::Forms::ToolStripButton^ sqare;
  56. private: System::Windows::Forms::FlowLayoutPanel^ flowLayoutPanel1;
  57. private: System::Windows::Forms::PictureBox^ color2;
  58. private: System::Windows::Forms::PictureBox^ color1;
  59. private: System::Drawing::Printing::PrintDocument^ printDocument1;
  60. private: System::Windows::Forms::PictureBox^ pictureBox3;
  61. private: System::Windows::Forms::PictureBox^ pictureBox4;
  62. private: System::Windows::Forms::PictureBox^ pictureBox5;
  63. private: System::Windows::Forms::PictureBox^ pictureBox6;
  64. private: System::Windows::Forms::PictureBox^ pictureBox7;
  65. private: System::Windows::Forms::PictureBox^ pictureBox8;
  66. private: System::Windows::Forms::PictureBox^ pictureBox9;
  67. private: System::Windows::Forms::PictureBox^ pictureBox10;
  68. private: System::Windows::Forms::PictureBox^ pictureBox11;
  69. private: System::Windows::Forms::PictureBox^ pictureBox12;
  70. private: System::Windows::Forms::PictureBox^ pictureBox13;
  71. private: System::Windows::Forms::PictureBox^ pictureBox14;
  72. private: System::Windows::Forms::PictureBox^ pictureBox15;
  73. private: System::Windows::Forms::PictureBox^ pictureBox16;
  74. private: System::Windows::Forms::PictureBox^ pictureBox17;
  75. private: System::Windows::Forms::PictureBox^ pictureBox18;
  76. private: System::Windows::Forms::PictureBox^ pictureBox19;
  77. private: System::Windows::Forms::PictureBox^ pictureBox20;
  78. private: System::Windows::Forms::PictureBox^ pictureBox21;
  79. private: System::Windows::Forms::PictureBox^ pictureBox22;
  80. private: System::Windows::Forms::PictureBox^ pictureBox23;
  81. private: System::Windows::Forms::PictureBox^ pictureBox24;
  82. private: System::Windows::Forms::PictureBox^ pictureBox25;
  83. private: System::Windows::Forms::PictureBox^ pictureBox26;
  84. private: System::Windows::Forms::PictureBox^ pictureBox27;
  85. private: System::Windows::Forms::PictureBox^ pictureBox28;
  86. private: System::Windows::Forms::PictureBox^ pictureBox29;
  87. private: System::Windows::Forms::PictureBox^ pictureBox30;
  88. private: System::Windows::Forms::PictureBox^ pictureBox31;
  89. private: System::Windows::Forms::PictureBox^ pictureBox32;
  90. private: System::Windows::Forms::PictureBox^ pictureBox33;
  91. private: System::Windows::Forms::PictureBox^ pictureBox34;
  92. private: System::Windows::Forms::PictureBox^ pictureBox35;
  93. private: System::Windows::Forms::PictureBox^ pictureBox36;
  94. private: System::Windows::Forms::PictureBox^ pictureBox37;
  95. private: System::Windows::Forms::PictureBox^ pictureBox38;
  96. private: System::Windows::Forms::PictureBox^ pictureBox39;
  97. private: System::Windows::Forms::PictureBox^ pictureBox40;
  98. private: System::Windows::Forms::PictureBox^ pictureBox41;
  99. private: System::Windows::Forms::PictureBox^ pictureBox42;
  100. private: System::Windows::Forms::PictureBox^ pictureBox43;
  101. private: System::Windows::Forms::PictureBox^ pictureBox44;
  102. private: System::Windows::Forms::PictureBox^ pictureBox45;
  103. private: System::Windows::Forms::PictureBox^ pictureBox46;
  104. private: System::Windows::Forms::ToolStripButton^ pen;
  105. private: System::Windows::Forms::Panel^ panel2;
  106. private: System::Windows::Forms::Panel^ panel3;
  107. private: System::Windows::Forms::PictureBox^ pictureBox47;
  108. private: System::Windows::Forms::PictureBox^ pictureBox48;
  109. private: System::Windows::Forms::PictureBox^ pictureBox49;
  110. private: System::Windows::Forms::PictureBox^ pictureBox50;
  111. private: System::Windows::Forms::PictureBox^ pictureBox51;
  112. private: System::Windows::Forms::ToolStripMenuItem^ 清除ToolStripMenuItem;
  113. ToolStripButton ^mod1,^mod2;
  114. private: System::Windows::Forms::ToolStripButton^ fillin;
  115. private: System::Windows::Forms::ToolStripButton^ line;
  116. private: System::Windows::Forms::ToolStripButton^ erazer;
  117. private: System::Windows::Forms::ToolStripButton^ getcolor;
  118. private: System::Windows::Forms::ToolStripButton^ empty;
  119. private: System::Windows::Forms::ToolStripButton^ ssub;
  120. private: System::Windows::Forms::ToolStripButton^ filled;
  121. private: System::Windows::Forms::ToolStripButton^ sadd;
  122. private: System::Windows::Forms::ToolStripMenuItem^ 另存为ToolStripMenuItem;
  123. System::ComponentModel::Container ^components;
  124. void InitializeComponent(void)//各控件初始化
  125. private: System::Int32 add1(int x);
  126. private:System::Int32 sub1(int x);
  127. private:System::Void next();//重复的实现
  128. private:System::Void last();//撤销的实现
  129. private:System::Void update()//对pictureBox1上显示的图像进行更新
  130. private:System::Void newb()//创建新bitmap来保存修改(用来实现撤销与重复功能)
  131. private:System::Void newb(int x,int y)//在创建bitmap同时改变图象大小(可实现缩放)
  132. private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) //窗体加载事件各变量初始化
  133. private: System::Void pictureBox2_MouseUp(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)//用来响应拖动右下角小白块时缩放的鼠标操作
  134. private: System::Void 撤销ToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) //撤销菜单
  135. private: System::Void 重复ToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) //重复菜单
  136. private: System::Void setcolor(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e) //用来设置前景色或后景色
  137. private: System::Void pictureBox3_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e) 以下是个眼色框的鼠标事件,用来调整颜色
  138. private: System::Void pictureBox4_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  139. private: System::Void pictureBox5_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  140. private: System::Void pictureBox6_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  141. private: System::Void pictureBox7_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  142. private: System::Void pictureBox8_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  143. private: System::Void pictureBox9_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  144. private: System::Void pictureBox10_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  145. private: System::Void pictureBox11_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  146. private: System::Void pictureBox12_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  147. private: System::Void pictureBox13_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  148. private: System::Void pictureBox14_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  149. private: System::Void pictureBox15_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  150. private: System::Void pictureBox16_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  151. private: System::Void pictureBox17_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  152. private: System::Void pictureBox18_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  153. private: System::Void pictureBox19_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  154. private: System::Void pictureBox20_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  155. private: System::Void pictureBox21_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  156. private: System::Void pictureBox22_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  157. private: System::Void pictureBox23_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  158. private: System::Void pictureBox24_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  159. private: System::Void pictureBox25_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  160. private: System::Void pictureBox26_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  161. private: System::Void pictureBox27_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  162. private: System::Void pictureBox28_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  163. private: System::Void pictureBox29_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  164. private: System::Void pictureBox30_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  165. private: System::Void pictureBox31_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  166. private: System::Void pictureBox32_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  167. private: System::Void pictureBox33_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  168. private: System::Void pictureBox34_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  169. private: System::Void pictureBox35_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  170. private: System::Void pictureBox36_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  171. private: System::Void pictureBox37_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  172. private: System::Void pictureBox38_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  173. private: System::Void pictureBox39_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  174. private: System::Void pictureBox40_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  175. private: System::Void pictureBox41_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  176. private: System::Void pictureBox42_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  177. private: System::Void pictureBox43_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  178. private: System::Void pictureBox44_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  179. private: System::Void pictureBox45_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  180. private: System::Void pictureBox46_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  181. private: System::Void pictureBox47_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  182. private: System::Void pictureBox48_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  183. private: System::Void pictureBox49_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  184. private: System::Void pictureBox50_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
  185. private: System::Void color1_DoubleClick(System::Object^ sender, System::EventArgs^ e)//用来双击调出颜色选择对话框
  186. private: System::Void color2_DoubleClick(System::Object^ sender, System::EventArgs^ e) //用来双击调出颜色选择对话框
  187. private: System::Void 清除ToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) //清除菜单实现
  188. private: System::Void pen_Click(System::Object^ sender, System::EventArgs^ e)//实现了铅笔按钮
  189. private: System::Void brush_Click(System::Object^ sender, System::EventArgs^ e) //刷子按钮
  190. private: System::Void circle_Click(System::Object^ sender, System::EventArgs^ e)//圆形工具
  191. private: System::Void sqare_Click(System::Object^ sender, System::EventArgs^ e)//矩形工具
  192. private: System::Void fillin_Click(System::Object^ sender, System::EventArgs^ e)油漆桶工具
  193. private: System::Void line_Click(System::Object^ sender, System::EventArgs^ e) //线段工具
  194. private: System::Void getcolor_Click(System::Object^ sender, System::EventArgs^ e) //取色器工具
  195. private: System::Void erazer_Click(System::Object^ sender, System::EventArgs^ e) //橡皮工具
  196. private: System::Void filled_Click(System::Object^ sender, System::EventArgs^ e) //实心模式
  197. private: System::Void empty_Click(System::Object^ sender, System::EventArgs^ e) //空心模式
  198. private: System::Int32 fitx(int x)//检查x坐标是否在范围内
  199. private: System::Int32 fity(int y) //检查y坐标是否在范围内
  200. private: System::Boolean fit(int x,int y) //检查x、y坐标是否在范围内
  201. private: System::Void mmove(System::Windows::Forms::MouseEventArgs^ e)//实现了各工具拖动鼠标时的响应
  202. private: System::Void pictureBox1_MouseMove(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)//鼠标移动事件调用了mmove()
  203. private: System::Void pictureBox1_MouseDown(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e) //鼠标按下事件实现了各工具按下鼠标时的响应
  204. private: System::Void pictureBox1_MouseUp(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e) //鼠标放开事件实现了各工具放开鼠标时的相应
  205. private: System::Void pictureBox1_MouseEnter(System::Object^ sender, System::EventArgs^ e) //鼠标进入事件用来配合光标的改变(自己绘制光标时将光标隐藏)
  206. private: System::Void sadd_Click(System::Object^ sender, System::EventArgs^ e) //尺寸增大按钮的实现
  207. private: System::Void ssub_Click(System::Object^ sender, System::EventArgs^ e) //尺寸缩小按钮的实现
  208. private: System::Void pictureBox1_Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e) //绘图事件用来绘制部分工具光标及绘制绘画即时响应
  209. private: System::Void pictureBox1_MouseLeave(System::Object^ sender, System::EventArgs^ e) //鼠标离开事件用来配合部分光标的绘制
  210. private: System::Void tchong(int ix,int iy,Color c)//4邻域种子填充
  211. private: System::Void 打开ToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e)// 打开菜单的实现
  212. private: System::Void 另存为ToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e)//另存为菜单的实现
  213. private: System::Void openFileDialog1_FileOk(System::Object^ sender, System::ComponentModel::CancelEventArgs^ e) //打开对话框确定的实现
  214. private: System::Void saveFileDialog1_FileOk(System::Object^ sender, System::ComponentModel::CancelEventArgs^ e) //保存对话框确定的实现
  215. }

5、运行结果

程序界面

铅笔演示

油漆桶演示

矩形(实心)演示

圆形(空心)演示

各种尺寸的刷子、橡皮

保存对话框

6、设计总结

这次设计令我受益匪浅,使我更加熟练的掌握了winform中有关图形和鼠标处理的部分,设计历时10天虽然比较累但是也充满了趣味和成就感,相信在这次设计中我获得的经验必然能在以后派上更大的用处。

上传的附件 cloud_download 基于winform实现的仿windows画板绘图程序.7z ( 1.76mb, 1次下载 )
error_outline 下载需要13点积分

发送私信

去奋斗,去追求,去发现,但不要放弃

11
文章数
12
评论数
最近文章
eject