实验要求(用ArrayList类):
(1) 当鼠标左键按下后,拖动鼠标可以在窗体上画出一系列点来,鼠标抬起则停止;
(2) 利用菜单设置线的颜色和粗细。
(3) 利用菜单实现线条的擦除。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
namespace DRAWING
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
ArrayList m_PointList = new ArrayList();
int r = 255;
int g = 0;
int b = 0;
int t = 5;
Form2 f2 = new Form2();
Form3 f3 = new Form3();
bool LBtnDown = false;
bool delP = false;
private void 颜色ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (f2.ShowDialog() == DialogResult.OK)
{
r = int.Parse(f2.Form2ValueR);
g = int.Parse(f2.Form2ValueG);
b = int.Parse(f2.Form2ValueB);
}
}
private void 粗细ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (f3.ShowDialog() == DialogResult.OK)
{
t = int.Parse(f3.Form3ValueT);
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics ghs = e.Graphics;
foreach (Point p in m_PointList)
{
ghs.FillEllipse(new SolidBrush(Color.FromArgb(r, g, b)), p.X, p.Y, t, t);
}
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
LBtnDown = true;
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
LBtnDown = false;
delP = false;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
Point point = new Point();
point.X = e.X;
point.Y = e.Y;
if (LBtnDown == true && delP == false)
{
m_PointList.Add(point);
}
if (delP == true && LBtnDown == true)
{
if (m_PointList.Contains(point))
{
m_PointList.Remove(point);
}
}
}
private void 擦除ToolStripMenuItem_Click(object sender, EventArgs e)
{
delP = true;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DRAWING
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public string Form2ValueR
{
get
{
return this.textBox1.Text;
}
}
public string Form2ValueG
{
get
{
return this.textBox2.Text;
}
}
public string Form2ValueB
{
get
{
return this.textBox3.Text;
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DRAWING
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
public string Form3ValueT
{
get
{
return this.textBox1.Text;
}
}
}
}
我在运行时,没有任何显示,无法达到绘图效果。
我是按着课上给的模板填充的代码,看了几个小时,也没发现什么不对的,所以希望有人能 帮我看看,感谢各位了。