使用按钮在PictureBox上画一条线作为坐标 [英] Draw a line using buttons as a coordinates on a PictureBox

查看:86
本文介绍了使用按钮在PictureBox上画一条线作为坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在根据显示的图片为客房/办公室"搜索程序.

Currently I am making a searching program for Rooms/Office based on the image shown.

我在想在2D地图上显示一条线的程序上遇到麻烦,该处将使用按钮来连接该线的每个坐标.但是搜索房间后,将不会出现任何行.

I am having trouble on the program that I want show a line on the 2D map where buttons will be use to connect each coordinates of the line. But after searching a room no lines that will appear.

private void button2_Click(object sender, EventArgs e)
    {
        System.Drawing.Pen myPen;
        myPen = new System.Drawing.Pen(System.Drawing.Color.Red, 5);
        System.Drawing.Graphics frmGraphics = pictureBox1.CreateGraphics();

        if (textBox1.Text == "")
        {
            MessageBox.Show("Nothing to Search!", "", MessageBoxButtons.OK);
        }
        else
        {
            if (radioButton2.Checked == true)
            {

                if(textBox1.Text == "dental clinic")
                {


                    frmGraphics.DrawLine(myPen, path1.Location.X, path1.Location.Y, path2.Location.X, path2.Location.Y);
                    Thread.Sleep(500);
                    frmGraphics.DrawLine(myPen, path2.Location.X, path2.Location.Y, path3.Location.X, path3.Location.Y);
                    Thread.Sleep(500);
                    frmGraphics.DrawLine(myPen, path3.Location.X, path3.Location.Y, path4.Location.X, path4.Location.Y);


                    lbres.Text = "Dental Clinic";
                    lbloc.Text = "OutPatient Department";
                    OPDView opdfrm = new OPDView();
                    dview = opdfrm;
                }
                else
                {
                    MessageBox.Show("No Results Found!", "", MessageBoxButtons.OK);
                }
                myPen.Dispose();
                frmGraphics.Dispose(); 
                return;

          }
       }

我有4个按钮,其中的一个按钮已重命名为path1-path4,并希望将它们全部连接在线.对此,我需要一些帮助.

I have 4 buttons which a had rename as path1 - path4 and wish to connect them all on the line. I need some help about this, thanks.

推荐答案

代码有更多问题,但我只会在最后提示它们..

The code has more issues but I'll only hint at them at the end..

您可能遇到的错误是坐标不正确.

The error you probably trip on is in not getting the coordinates right.

PictureBox不是容器控件,因此放置在顶部上的任何内容都不是不是坐在其内部.通过移动PB进行测试:Buttons 不会移动.--请注意,这与将Button放在PanelGroupBox上不同..

PictureBox is not a container control, so whatever you put on top of it is not sitting inside it. Test by moving the PB: The Buttons won't move with it.. - Note that this is different from putting a Button onto a Panel or a GroupBox..

有两种解决方法:

  • 您可以在代码或
  • 中将PictureBoxButtons子代化
  • 您可以计算出正确的位置.

以下是制作PB的Button子代的方法:

Here is how to make the Button children of the PB:

path1.Parent = pictureBox1;    
path2.Parent = pictureBox1;    
path3.Parent = pictureBox1;

请注意,您必须采用现在可以使按钮移动的代码

Note that you will have to adopt the code that moces the buttons now

或者您在考虑PB位置的情况下更正位置:

Or you correct the position taking the PB's location into account:

Point np1 = Point.Subtract(path1.Location, (Size)pictureBox1.Location);
Point np2 = Point.Subtract(path2.Location, (Size)pictureBox1.Location);
Point np3 = Point.Subtract(path3.Location, (Size)pictureBox1.Location);
frmGraphics.DrawLine(myPen, Point.Empty, np1 , np2);
...

另一个问题是使用control.CreateGraphics的绘制方式不是持久性的.实际上,这几乎总是错误的!结果将在每次调整窗口大小时消失.等等.更好的方法是使用e.Graphics参数绘制Picturebox.Paint事件.

Another issue is thatthe way to draw, using control.CreateGraphics is not persistent. In fact it is almost always wrong! The results will disappear at each window resize etc.. The better way is to draw in the Picturebox.Paint event using the e.Graphics parameter.

我也想知道Buttons首先是什么?您也可以创建List<Point>并在没有它们的情况下进行绘制.

I also wonder what the Buttons are there for in the first place? You can create a List<Point> and draw without them just as well.

还将Thread.Sleep(500)用于动画也会冻结UI线程.而是使用Timer浏览点列表.您需要固定数量的动画方法,才能创建Dictionary<string, List<Point>>来保存所有动画.

Also using Thread.Sleep(500) for an animation will freeze the UI thread. Instead use a Timer to go through the list of points. It you need a fixed number of ways to animate you can create a Dictionary<string, List<Point>> to hold them all..

更新

创建点列表很简单.在课堂上进行声明:

To create a list of points is simple. Declare it at class level:

List<Point> way = new List<Point>();

并在知道数据时填充它:

And fill it when you know the data:

way.Add(new Point(100, 100));
way.Add(new Point(150, 100));
way.Add(new Point(150, 200));
way.Add(new Point(10, 200));
way.Add(new Point(10, 220));
way.Add(new Point(50, 220));
way.Add(new Point(50, 250));

还创建一个计时器并设置其间隔.在其Tick事件中,您将指针向上移动到当前段,并调用Paint事件使PictureBox无效:

Also create a Timer and set its Interval. In its Tick event you move up a pointer to the current segment and call the Paint event be invalidating the PictureBox:

void aTimer_Tick(object sender, EventArgs e)
{
    point = Math.Min(point + 1, way.Count);
    pictureBox1.Invalidate();
    if (point >= way.Count) aTimer.Stop();
}

然后在PictureBox的Paint事件中,绘制从开始到当前索引的路径:

And in the Paint event of the PictureBox you draw the path from start to the current index:

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    using (Pen pen = new Pen(Color.DarkGoldenrod, 2.5f))
        for (int i = 1; i < point; i++) e.Graphics.DrawLine(pen , way[i - 1], way[i]);
}

我省去了几笔支票,您将想出一种存储或构造点列表的方法.

I left out a few checks and you will want to think of a way to store or construct the point lists..

您可能还想通过绘制较小的线段来改善动画效果.为此,您可以添加更多的点,但是使用函数会更灵活..:

You also may want to improve the animation by drawing smaller segments. For this you could add more points but using a function will be more flexible..:

int stepLen = 10;  // draw 10 pixels on each tick

List<Point> SlowWay(List<Point> way)
{
    List<Point> slow = new List<Point>();

    for (int i = 1; i < way.Count; i++ )
    {
        int dx = way[i].X - way[i - 1].X;
        int dy = way[i].Y - way[i - 1].Y;
        float len = (float)Math.Sqrt(dx*dx+dy*dy);
        int stepCount = (int)(len / stepLen);
        float stepX = dx / stepCount;
        float stepY = dy / stepCount;

        slow.Add(way[i - 1]);
        for (int s = 0; s < stepCount; s++)
            slow.Add(new Point((int)(way[i - 1].X + s * stepX), 
                               (int)(way[i - 1].Y + s * stepY)));
    }
    slow.Add(way[way.Count - 1]);
    return slow;
}

这篇关于使用按钮在PictureBox上画一条线作为坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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