我有几个标签,我想在具有特殊点的标签之间画线 [英] I have several labels, I want to draw line between labels which has special points

查看:92
本文介绍了我有几个标签,我想在具有特殊点的标签之间画线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.IO;
using System.Drawing;

using System.Windows.Forms;

namespace Travellast
{
    public partial class Form1 : Form
    {
        int infinity = 9999999;
        int n = 0;
      
 Dictionary<string, Point> city = new Dictionary<string, Point>();
        Point p1;   
        CGraph graph = new CGraph();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
             List<string> cityNames = graph.Cities;
            n = cityNames.Count;
            foreach (string cityName in cityNames)
            {
                srcCityBox.Items.Add(cityName);
                destCityBox.Items.Add(cityName);
            }
            Graphics gr = CreateGraphics();
            Pen mypen = new Pen(Color.Black, 3);
            StreamReader reader = new StreamReader("Cities.txt");
            string lines = "";
            char[] separator = { '|' };
            while ((lines = reader.ReadLine()) != null)
            {
                string[] route = lines.Split(separator);
                string Name =route[0];
                int x = Convert.ToInt32(route[1]);
                int y = Convert.ToInt32(route[2]);
                p1.X = x;
                p1.Y = y;
                Label label = new Label();
                label.Text = Name;
                label.Location = new Point(p1.X, p1.Y);
                label.AutoSize = true;
                label.Font = new Font("Tahoma", 14F, FontStyle.Bold, GraphicsUnit.Point, ((byte)(0)));
                label.ForeColor = Color.Green;
                panel1.Controls.Add(label);
                city.Add(Name,p1);
            }
            StreamReader reader1 = new StreamReader("Routs.txt");
            string line = "";
            char[] separator1 = { '|' };
            while ((line = reader1.ReadLine()) != null)
            {
                string[] routes = line.Split(separator);

                string sou = routes[0];
                string des = routes[1];
       
                Point p3 = new Point();
                Point p4 = new Point();
           
                
                    if (city.ContainsKey(sou))
                        p3 = city[sou];
                    if (city.ContainsKey(des))
                        p4 = city[des];
                    gr.DrawLine(mypen, p3, p4);                
            }
            reader1.Close();              
                         
            }

        

        private void button2_Click(object sender, EventArgs e)
        {
            textBox2Result.Text = graph.findShortestPath(srcCityBox.Text, destCityBox.Text);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //to sure Matrix has complited.
            List<string> city = graph.Cities;
            int[,] edge = graph.Cost;
            for (int row = 0; row < n; row++)
            {
                for (int col = 0; col < n; col++)
                    if ((edge[row, col] != 0) && (edge[row, col] != infinity))
                    {
                        MessageBox.Show(city[row] + "  ->" + city[col] + "  -" + edge[row, col]);
                    }
            }
                }
    }

推荐答案

private void panel1_Paint(object sender ,PaintEventArgs e)

{

Graphics gr = e.Graphics;

Pen mypen = new Pen(Color.Black,3);

StreamReader reader1 = new StreamReader(Routs.txt);

string line =;

char [] separator1 = {'| '};

while((line = reader1.ReadLine())!= null)

{

string [] routes = line。拆分(separator1);



string sou = routes [0];

string des =(routes [1]);



点p3 =新点();

点p4 =新点();





if(city.ContainsKey(sou))

p3 = city [sou];

if(city.ContainsKey(des))

p4 = city [des];

gr.DrawLine (mypen,p3,p4);



}

reader1.Close();

}
private void panel1_Paint(object sender, PaintEventArgs e)
{
Graphics gr = e.Graphics;
Pen mypen = new Pen(Color.Black, 3);
StreamReader reader1 = new StreamReader("Routs.txt");
string line = "";
char[] separator1 = { '|' };
while ((line = reader1.ReadLine()) != null)
{
string[] routes = line.Split(separator1);

string sou = routes[0];
string des = (routes[1]);

Point p3 = new Point();
Point p4 = new Point();


if (city.ContainsKey(sou))
p3 = city[sou];
if (city.ContainsKey(des))
p4 = city[des];
gr.DrawLine(mypen, p3, p4);

}
reader1.Close();
}


这篇关于我有几个标签,我想在具有特殊点的标签之间画线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆