这段代码是一个绘画板开发C#项目,尽管这个C#项目很小,但这段代码还是有其可借鉴性的。51CTO编辑推荐《C#实用基础教程》
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- namespace WindowsApplication1
- {
- public partial class Form1 : Form
- {
- bool isPressed = false;
- int px = 0;
- int py = 0;
- Color color = Color.Black;
- public int size = 0;
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_MouseDown(object sender, MouseEventArgs e)
- {
- px = e.X;
- py = e.Y;
- isPressed = true;
- }
- private void Form1_MouseMove(object sender, MouseEventArgs e)
- {
- if (isPressed)
- {
- int tempX = e.X;
- int tempY = e.Y;
- Graphics g = this.CreateGraphics();
- g.DrawLine(new Pen(color, size),px,py,tempX,tempY);
- px = tempX;
- py = tempY;
- }
- }
- private void Form1_MouseUp(object sender, MouseEventArgs e)
- {
- isPressed = false;
- }
- private void 画笔颜色ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- ColorDialog dialog = new ColorDialog();
- dialog.ShowDialog();
- color = dialog.Color;
- }
- private void 画笔粗细ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- FrmSize frmSize = new FrmSize(this);
- frmSize.ShowDialog();
- }
- }
- }
绘画板开发C#项目源码就展示到这里
【编辑推荐】