文章出处:http://xiaoliang1982.blog.hexun.com/5723850_d.html

以下是源代码,直接粘贴到.cs中就可以用了。

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing.Drawing2D;using System.Data;namespace WindowsApptest9_21
{/// <summary>/// Form1 的摘要说明。/// </summary>public class Form1 : System.Windows.Forms.Form{public System.Windows.Forms.PictureBox pictureBox1;private Point p1 = Point.Empty, p2 = Point.Empty;private Point p3=Point.Empty;private bool isMouseDown = false, isMouseUp = false;ArrayList addArray = new ArrayList();ArrayList aaaArray = new ArrayList();private System.Windows.Forms.Button button1;private System.Windows.Forms.Button button2;public string shape;private System.Windows.Forms.Button button3;private System.Windows.Forms.Button button4;private Color color=Color.Black;private System.Windows.Forms.Button button5;private System.Windows.Forms.Button button6;
//  private Pen pen;//      Pen pen=new Pen(col,2);public struct SharpType{public string type;public Point p1, p2;public Color foreColor, backColor;public Brush brush;public SharpType(string type, Point p1, Point p2, Color foreColor, Color backColor, Brush brush ){this.type = type;this.p1 = p1;this.p2 = p2;this.foreColor = foreColor;this.backColor = backColor;this.brush = brush;}
//   public Point[]points=new Point[];}/// <summary>/// 必需的设计器变量。/// </summary>private System.ComponentModel.Container components = null;public Form1(){//// Windows 窗体设计器支持所必需的//InitializeComponent();//// TODO: 在 InitializeComponent 调用后添加任何构造函数代码//}/// <summary>/// 清理所有正在使用的资源。/// </summary>protected override void Dispose( bool disposing ){if( disposing ){if (components != null) {components.Dispose();}}base.Dispose( disposing );}#region Windows 窗体设计器生成的代码/// <summary>/// 设计器支持所需的方法 - 不要使用代码编辑器修改/// 此方法的内容。/// </summary>private void InitializeComponent(){this.pictureBox1 = new System.Windows.Forms.PictureBox();this.button1 = new System.Windows.Forms.Button();this.button2 = new System.Windows.Forms.Button();this.button3 = new System.Windows.Forms.Button();this.button4 = new System.Windows.Forms.Button();this.button5 = new System.Windows.Forms.Button();this.button6 = new System.Windows.Forms.Button();this.SuspendLayout();// // pictureBox1// this.pictureBox1.BackColor = System.Drawing.Color.White;this.pictureBox1.Cursor = System.Windows.Forms.Cursors.Cross;this.pictureBox1.Location = new System.Drawing.Point(0, 0);this.pictureBox1.Name = "pictureBox1";this.pictureBox1.Size = new System.Drawing.Size(432, 397);this.pictureBox1.TabIndex = 0;this.pictureBox1.TabStop = false;this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);this.pictureBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp);this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);// // button1// this.button1.Location = new System.Drawing.Point(448, 72);this.button1.Name = "button1";this.button1.TabIndex = 1;this.button1.Text = "矩形";this.button1.Click += new System.EventHandler(this.button1_Click);// // button2// this.button2.Location = new System.Drawing.Point(448, 112);this.button2.Name = "button2";this.button2.TabIndex = 2;this.button2.Text = "椭圆";this.button2.Click += new System.EventHandler(this.button2_Click);// // button3// this.button3.Location = new System.Drawing.Point(448, 144);this.button3.Name = "button3";this.button3.TabIndex = 3;this.button3.Text = "直线";this.button3.Click += new System.EventHandler(this.button3_Click);// // button4// this.button4.Location = new System.Drawing.Point(448, 224);this.button4.Name = "button4";this.button4.TabIndex = 4;this.button4.Text = "选择颜色";this.button4.Click += new System.EventHandler(this.button4_Click);// // button5// this.button5.Location = new System.Drawing.Point(448, 184);this.button5.Name = "button5";this.button5.TabIndex = 5;this.button5.Text = "清除";this.button5.Click += new System.EventHandler(this.button5_Click);// // button6// this.button6.Location = new System.Drawing.Point(448, 32);this.button6.Name = "button6";this.button6.TabIndex = 6;this.button6.Text = "曲线";this.button6.Click += new System.EventHandler(this.button6_Click);// // Form1// this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);this.ClientSize = new System.Drawing.Size(552, 397);this.Controls.Add(this.button6);this.Controls.Add(this.button5);this.Controls.Add(this.button4);this.Controls.Add(this.button3);this.Controls.Add(this.button2);this.Controls.Add(this.button1);this.Controls.Add(this.pictureBox1);this.Name = "Form1";this.Text = "Form1";this.ResumeLayout(false);}#endregion/// <summary>/// 应用程序的主入口点。/// </summary>[STAThread]static void Main() {Application.Run(new Form1());}private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e){if( ! isMouseUp ){this.isMouseDown = true;this.p1 = new Point( e.X, e.Y );}}private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e){Graphics g = this.pictureBox1.CreateGraphics();if( isMouseDown && p2 != Point.Empty ){if(this.shape=="DrawEllipse"){g.DrawEllipse( Pens.White, p1.X, p1.Y, Math.Abs( p1.X - p2.X ), Math.Abs( p1.Y - p2.Y ) );}if(this.shape=="DrawRectangle"){g.DrawRectangle( Pens.White, p1.X, p1.Y, Math.Abs( p1.X - p2.X ), Math.Abs( p1.Y - p2.Y ) );}if(this.shape=="DrawLine"){g.DrawLine(Pens.White,this.p1,this.p2);}}if( isMouseDown && ! isMouseUp ){p2 = new Point( e.X, e.Y );if(this.shape=="DrawEllipse"){g.DrawEllipse(new Pen(color,1), p1.X, p1.Y, Math.Abs( p1.X - p2.X ), Math.Abs( p1.Y - p2.Y ) );}if(this.shape=="DrawRectangle"){//     g.DrawRectangle( Pens.Black, p1.X, p1.Y, Math.Abs( p1.X - p2.X ), Math.Abs( p1.Y - p2.Y ) );g.DrawRectangle( new Pen(color,1), p1.X, p1.Y, Math.Abs( p1.X - p2.X ), Math.Abs( p1.Y - p2.Y ) );}if(this.shape=="DrawLine"){g.DrawLine(new Pen(color,1),this.p1,this.p2);}if(this.shape=="DrawCurve"){g.DrawLine(new Pen(color,1),this.p1,this.p2);addArray.Add( new SharpType("DrawCurve", p1, p2, color, Color.Empty, Brushes.Black ) );this.p1=this.p2;this.p2=this.p3;}}foreach( SharpType type in addArray ){if(type.type=="DrawEllipse"){g.DrawEllipse( new Pen(type.foreColor,1), type.p1.X, type.p1.Y, Math.Abs( type.p1.X -  type.p2.X ), Math.Abs( type.p1.Y - type.p2.Y ) );}if(type.type=="DrawRectangle"){g.DrawRectangle(new Pen(type.foreColor,1), type.p1.X, type.p1.Y, Math.Abs( type.p1.X -  type.p2.X ), Math.Abs( type.p1.Y - type.p2.Y ) );}if(type.type=="DrawLine"){g.DrawLine(new Pen(type.foreColor,1),type.p1,type.p2);}if(type.type=="DrawCurve"){g.DrawLine(new Pen(type.foreColor,1),type.p1,type.p2);}}g.Dispose();}private void pictureBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e){this.isMouseDown = false;p2 = new Point( e.X, e.Y );Graphics g = this.pictureBox1.CreateGraphics();if(this.shape=="DrawEllipse"){g.DrawEllipse( new Pen(color,1), p1.X, p1.Y, Math.Abs( p1.X - p2.X ), Math.Abs( p1.Y - p2.Y ) );addArray.Add( new SharpType( "DrawEllipse", p1, p2, color, Color.Empty, Brushes.Black ) );}if(this.shape=="DrawRectangle"){g.DrawRectangle(new Pen(color,1), p1.X, p1.Y, Math.Abs( p1.X - p2.X ), Math.Abs( p1.Y - p2.Y ) );
//    addArray.Add( new SharpType("DrawRectangle", p1, p2, Color.Black, Color.Empty, Brushes.Black ) );addArray.Add( new SharpType("DrawRectangle", p1, p2, color, Color.Empty, Brushes.Black ) );}if(this.shape=="DrawLine"){g.DrawLine(new Pen(color,1),this.p1,this.p2);addArray.Add( new SharpType("DrawLine", p1, p2, color, Color.Empty, Brushes.Black ) );}if(this.shape=="DrawCurve"){g.DrawLine(new Pen(color,1),this.p1,this.p2);addArray.Add( new SharpType("DrawCurve", p1, p2, color, Color.Empty, Brushes.Black ) );
//    this.p1=this.p2;
//    this.p2=this.p3;}p1 = Point.Empty;p2 = Point.Empty;g.Dispose();}private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e){foreach( SharpType type in addArray ){if(type.type=="DrawEllipse"){e.Graphics.DrawEllipse(new Pen(type.foreColor,1), type.p1.X, type.p1.Y, Math.Abs( type.p1.X -  type.p2.X ), Math.Abs( type.p1.Y - type.p2.Y ) );}if(type.type=="DrawRectangle"){e.Graphics.DrawRectangle( new Pen(type.foreColor,1), type.p1.X, type.p1.Y, Math.Abs( type.p1.X -  type.p2.X ), Math.Abs( type.p1.Y - type.p2.Y ) );}if(type.type=="DrawLine"){e.Graphics.DrawLine(new Pen(type.foreColor,1),type.p1,type.p2);}if(type.type=="DrawCurve"){e.Graphics.DrawLine(new Pen(type.foreColor,1),type.p1,type.p2);}}}private void button1_Click(object sender, System.EventArgs e){shape="DrawRectangle";}private void button2_Click(object sender, System.EventArgs e){shape="DrawEllipse";
//   this.color=Color.Black;}private void button3_Click(object sender, System.EventArgs e){shape="DrawLine";}private void button4_Click(object sender, System.EventArgs e){ColorDialog ColorDialog1 = new ColorDialog ( ) ;ColorDialog1.AllowFullOpen = true  ;ColorDialog1.FullOpen = true ;//设定此颜色对话框存在"帮助"按钮,缺省是没有的ColorDialog1.ShowHelp = true  ;// 设定此颜色对话框的初始颜色,所以如果在对话框中选择"取消",则此对话框会重新此颜色ColorDialog1.Color = Color.Black ;if ( ColorDialog1.ShowDialog ( ) != DialogResult.Cancel ){this.color = ColorDialog1.Color ;//    showInfo ( ) ;} else{this.color=Color.Black;}}private void button5_Click(object sender, System.EventArgs e){this.addArray.Clear();this.pictureBox1.Refresh(); }private void button6_Click(object sender, System.EventArgs e){shape="DrawCurve";}}
}

c#制作的简单的画图板相关推荐

  1. 基于JavaSwing开发简单的画图板程序 课程设计 大作业源码

    基于JavaSwing开发简单的画图板程序:  (大作业) 开发环境: Windows操作系统 开发工具: MyEclipse+Jdk 运行效果图: 基于JavaSwing开发简单的画图板程序:  ( ...

  2. 简单python代码讲解-使用Python编写简单的画图板程序的示例教程

    从这次开始,我会由简单到困难(其实也不会困难到哪里去)讲几个例程,每一个例程都是我自己写(或者修改,那样的话我会提供原始出处)的,都具有一定的操作性和娱乐性.例程中汇尽量覆盖到以前所讲的pygame中 ...

  3. 一个功能强大的画图板(三)

    大家看过我上两篇博客[画图板一](http://blog.csdn.net/songhengqian/article/details/51792420),[画图板二](http://blog.csdn ...

  4. 画图板的实现— —让你成为抽象派画家

    简单画图板的实现 前言 先来欣赏一张图片 这张图片是不是非常神奇,你是不是感受到了代码的强大!下面还有更多图片哦! 接下让我们来实现一个简单的画图板吧 内容框架 1.创建一个画图的JFrame界面 2 ...

  5. 安卓入门,简单画图板的实现

    安卓简单画图板的实现 之前已经做了一个登录界面,现在,让我们一起做一个简单画图板吧.因为安卓画图板和之前学的java的步骤都差不多,所以我们实现画圆就行了(可以画圆之后,我们就基本上可以画其他形状了) ...

  6. java,制作简易画图板

    简易画图板 前些日子,我学习了关于xp系统简易画图板的制作,虽然我写的画图板功能不多,但这是我接触java以来完成的第一个项目,制作的时候感觉很艰难,有的时候明明知道思路,却因为不知道具体的方法而走了 ...

  7. Android入门,简单画图板的实现,自定义组件的实现

    Android的入门开发: 1:在Eclipse中新建一个Android项目,基本流程是:新建一个项目,点击File-New-Android Project,然后在三         个框中分别输入你 ...

  8. Java画图板的简单实现。(重绘,多边形,曲线画笔,画图片)

    思路: ①画图板的界面 ②创建监听器类 ③给按钮加上鼠标监听 ③画图版的重绘 第一部分:界面类 ①画图板的界面 ③给按钮加上鼠标监听 public class SampleDraw extends J ...

  9. java绘图板_Java中的画图板简单功能实现

    首先画图板就是用到了我们之前所学的知识,并不是很难,如果要构建一些复杂的功能,还需进一步学习.画图板所需要的API: JFrame //窗体 : JPanel //面板 :JButton //按钮 A ...

最新文章

  1. Spring Cloud Config采用数据库存储配置内容
  2. 20行python代码的入门级小游戏-用Python设计一个经典小游戏
  3. of python检索文献 science web_硕博士都在学的Python神技,有大神带!
  4. Python处理pdf文件库 - PyPDF2详解
  5. 4位先行进位加法器_行波进位/超前进位加法器详解
  6. sql 数据库 实例删除
  7. 金蝶云系统服务器,金蝶系统云服务器已离线
  8. 天猫超市回应“大数据杀熟”传言:系新人专享价未显示
  9. AFNnetworking详解
  10. java规范 控制语句
  11. python学习之多线程多进程
  12. 去除input框的值
  13. TinyPNG 智能PNG和JPEG图片压缩
  14. java 偏向锁的作用_java--偏向锁
  15. 制作ftl文件通过FreeMarke生成PDF文件(含图片处理)
  16. Windows使用MinGW编译ffmpeg
  17. Eureka自我保护模式——难点重点
  18. IO流实现写入规定的acci码值
  19. Windows 下使用anaconda虚拟环境pip install 安装pytorch出现报错There was a problem confirming the ssl certificate
  20. 结构方程模型(SEM)定义及应用【免费教程】

热门文章

  1. 洛谷——P2550 [AHOI2001]彩票摇奖
  2. AWT_Swing_JPasswordField密码框(Java)
  3. JavaScript:闭包
  4. 计算机工程学院运动会方阵口号,运动会方阵口号(精选多篇)
  5. halcon算子翻译——deserialize_measure
  6. python中关于集合的基础运用
  7. Eclipse JaveEE版本
  8. 机器学习基础-线性代数学习
  9. ajax css文件,wordpress 添加JS,css文件,实现AJAX效果
  10. 【Decision Tree】林轩田机器学习技法