c#程序设计之画图小程序(2)

2019-03-03 11:55

}

private void Form1_MouseDown(object sender, MouseEventArgs e) {

this.CanMove = true; startpt.X = e.X; startpt.Y = e.Y;

}

private void Form1_MouseMove(object sender, MouseEventArgs e) {

if(this.CanMove==true) {

Graphics g = this.CreateGraphics(); endpt.X = e.X; endpt.Y = e.Y;

g.DrawLine(new Pen(Color.Red, 1), startpt, endpt); startpt = endpt; } }

private void Form1_MouseUp(object sender, MouseEventArgs e) {

Graphics g = this.CreateGraphics();

g.DrawLine(new Pen(Color.Red,1),this.startpt,new Point(e.X,e.Y)); this.CanMove=false; }

} }

Using 命令 (画点,画线) using System.IO;

using System.Runtime.Serialization; //系列1

using System.Runtime.Serialization.Formatters.Binary;//系列2 using System.Drawing.Drawing2D;

namespace _024xubao {

public partial class Form1 : Form {

AdjMGraph G = new AdjMGraph(); int flag;

int n1, n2;//选 中的起点,选中的终点 const double alfa = 10; int index = -1;

const int Stone = 88888888;//无穷大

public MemoryStream fs2;

public BinaryFormatter formatter; public int sizeofnode = 0; public byte[] buffer;

public Form1() {

InitializeComponent(); }

public int FindPoint(int x, int y) {

//选择点,选中则返回该点的下标,否则返回-1 double d; int i, n;

n = G.Vertices.Count; for (i = 0; i < n; i++) {

Node ob = G.Vertices[i];

d = (x - ob.x) * (x - ob.x) + (y - ob.y) * (y - ob.y); d = Math.Sqrt(d);

if (d < alfa) return i; }

return -1; }

private void 打开ToolStripMenuItem_Click(object sender, EventArgs e) {

OpenFileDialog dlg = new OpenFileDialog();

if (dlg.ShowDialog() != DialogResult.OK) return;

FileStream fs = new FileStream(dlg.FileName, FileMode.Open); BinaryReader reader = new BinaryReader(fs); int n = 0;

G.Vertices.Clear(); Node ob; int i, j;

n = reader.ReadInt32(); for (i = 0; i < n; i++) {

reader.Read(buffer, 0, sizeofnode); fs2 = new MemoryStream(buffer);

ob = (Node)formatter.Deserialize(fs2); G.Vertices.Add(ob); }

for (i = 0; i < n; i++) {

for (j = 0; j < n; j++) {

G.edge[i, j] = reader.ReadInt32(); } }

reader.Close(); fs.Close();

//slb3.Text = \

Invalidate();//调用paint中重绘 }

private void 保存ToolStripMenuItem_Click(object sender, EventArgs e) {

SaveFileDialog dlg = new SaveFileDialog();

if (dlg.ShowDialog() != DialogResult.OK) return;

FileStream fs = new FileStream(dlg.FileName, FileMode.Create); BinaryWriter writer = new BinaryWriter(fs); int n = G.Vertices.Count;// Node ob; int i, j;

writer.Write(n);

for (i = 0; i < n; i++) {

ob = (Node)G.Vertices[i]; fs2 = new MemoryStream();

formatter.Serialize(fs2, ob); buffer = fs2.ToArray();

writer.Write(buffer, 0, sizeofnode); }

for (i = 0; i < n; i++) {

for (j = 0; j < n; j++) {

writer.Write(G.edge[i, j]); } }

writer.Close(); fs.Close(); }

private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) {

DialogResult a = MessageBox.Show(\是否要退出本程序\, \系统提示\, MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (Convert.ToString(a) == \) {

Application.Exit(); } }

private void 清除ToolStripMenuItem_Click(object sender, EventArgs e) {

flag = 0; //初始化图 int i, j;

G.Vertices = new List(); G.edge = new int[100, 100]; G.numOfEdges = 0;

for (i = 0; i < 100; i++) {

for (j = 0; j < 100; j++) {

G.edge[i, j] = Stone; } }

//获得node对象的系列化字节数 fs2 = new MemoryStream(); Node ob = new Node();

formatter = new BinaryFormatter();

formatter.Serialize(fs2, ob); buffer = fs2.ToArray();

sizeofnode = buffer.Length; fs2.Close();

Invalidate();//调用paint中重绘 }

private void 移动ToolStripMenuItem_Click(object sender, EventArgs e) {

flag = 3;

//SLabl.Text = \当前操作:移动\ }

private void 画线ToolStripMenuItem_Click(object sender, EventArgs e) {

flag = 2;

//SLabl.Text = \当前操作:画线\ }

private void 画点ToolStripMenuItem_Click(object sender, EventArgs e) {

flag = 1;

//SLabl.Text = \当前操作:画点\ }

private void Form1_Load_1(object sender, EventArgs e) {

flag = 0; //初始化图 int i, j;

G.Vertices = new List(); G.edge = new int[100, 100]; G.numOfEdges = 0;

for (i = 0; i < 100; i++) {

for (j = 0; j < 100; j++) {

G.edge[i, j] = Stone; } }

fs2 = new MemoryStream(); Node ob = new Node();

formatter = new BinaryFormatter(); formatter.Serialize(fs2, ob); buffer = fs2.ToArray();

sizeofnode = buffer.Length; fs2.Close(); }

private void Form1_MouseDown_1(object sender, MouseEventArgs e) {

if (flag == 1)

{

int n = G.Vertices.Count; Node ob = new Node(); ob.c = Color.Black; ob.ID = n + 1;

string str = \ + ob.ID + \; ob.Name = str.Substring(0, 10); ob.w = 2;

ob.x = e.X; ob.y = e.Y; G.Vertices.Add(ob);

//SLabl1.Text = \ Pen pen = new Pen(ob.c, ob.w);

Graphics g = this.CreateGraphics(); g.DrawEllipse(pen, ob.x, ob.y, 8, 8);

g.DrawString(ob.Name, new Font(\宋体\, 15), Brushes.Blue, new Point(ob.x + 5, ob.y));

}

if (flag == 2) //选起点 {

n1 = FindPoint(e.X, e.Y);

//SLabl3.Text = n1.ToString(); }

if (flag == 3) {

index = FindPoint(e.X, e.Y); //SLabl3.Text = n1.ToString(); } }

private void Form1_MouseUp_1(object sender, MouseEventArgs e) {

double d;

if (flag == 2)//选终点 {

n2 = FindPoint(e.X, e.Y);

//SLabl2.Text = n2.ToString(); if (n1 >= 0 && n2 >= 0) {

//插入边

Node ob1 = G.Vertices[n1]; Node ob2 = G.Vertices[n2];

d = (ob2.x - ob1.x) * (ob2.x - ob1.x) + (ob2.y - ob1.y) * (ob2.y - ob1.y); d = Math.Sqrt(d);

G.edge[n1, n2] = (int)d;//边长 G.numOfEdges++;

Graphics g = this.CreateGraphics(); Pen pen = new Pen(Color.Red, 4);

g.DrawLine(pen, new Point(ob1.x, ob1.y), new Point(ob2.x, ob2.y));

//SLabl4.Text = \ } }

if (flag == 3 && index >= 0)//移动 {

Node ob2 = new Node(); ob2 = G.Vertices[n1];


c#程序设计之画图小程序(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:拌合站安全、质量、环境、职业健康目标

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: