我无法选择图片框来绘制它们之间的界限 [英] I can not select pictureboxes to draw line between them

查看:84
本文介绍了我无法选择图片框来绘制它们之间的界限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的程序中,我有一个列表 picturebox ,并且在程序中我按下一个新按钮<将创建code> picturebox 。但是我希望通过 MouseDown MouseUp 在两个选定的图片框之间绘制2行,但不会绘制任何行。 />
这是我的代码:

In my program I have a List of picturebox and during the program when I push a button a new picturebox will be created. But I want to draw 2 lines between two selected pictureboxes by MouseDown and MouseUp but no line will be drawn.
Here is my code:

public partial class Form1 : Form
    {
        int x1, y1, x2, y2, x3, y3, x4, y4;
        List<PictureBox> pics = new List<PictureBox>();
        PictureBox thisPB;
        int i = 0;
        int j;
        void drawlines(int x1, int y1, int x2, int y2)
        {
            Graphics g = this.CreateGraphics();
            Pen p = new Pen(Color.Black,2);
            g.DrawLine(p, x1, y1, x1, y2);
            g.DrawLine(p, x1, y2, x2, y2);
            g.Dispose();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            pics.Add(new PictureBox());
            pics[i].Name = "pic" + i.ToString();
            pics[i].Left = 200 * i;
            pics[i].Top = 100 * i;
            pics[i].Height = 100;
            pics[i].Width = 100;
            pics[i].SizeMode = PictureBoxSizeMode.StretchImage;
            pics[i].Image = (Image)Properties.Resources.Tulips;
            //pics[i].MouseMove += new MouseEventHandler(pic_MouseMove);
            pics[i].MouseDown += new MouseEventHandler(pic_MouseDown);
            pics[i].MouseUp+=new MouseEventHandler(pic_MouseUp);
            this.Controls.Add(pics[i]);
            i++;
        }

        void pic_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                PictureBox thisPB = (PictureBox)sender;
                thisPB.Left = e.X;
                thisPB.Top = e.Y;
            }
        }

        void pic_MouseDown(object sender, MouseEventArgs e) 
        {
            x3 = e.X;
            y3 = e.Y;
            //PictureBox thisPB = pics.Find(p => p.Name == pics[pics.IndexOf(p)].Name);
            thisPB = (PictureBox)sender;
            x1 = (thisPB.Right - thisPB.Left) / 2;
            y1 = (thisPB.Top - thisPB.Bottom) / 2;
        }

        void pic_MouseUp(object sender, MouseEventArgs e)
        {
            x4 = e.X;
            y4 = e.Y;
            thisPB = (PictureBox)sender;
            //PictureBox thisPB = pics.Find(p => p.Name == pics[pics.IndexOf(p)].Name);
            //if (x4 < thisPB.Right)
            x2 = (thisPB.Right - thisPB.Left) / 2;
            y2 = (thisPB.Top - thisPB.Bottom) / 2;
            
            drawlines(x1, y1, x4, y4);
        }
}



如何选择要绘制的图片?


How can I choose a picture to be selected for drawing line?

推荐答案

很多不同的人一次又一次地达成同样的想法真是太棒了。我非常确定:这是可能的,但这是一个真正的死胡同,没有任何实际意义。图片框对你没有任何帮助。



请看我过去的答案: mdi子窗体之间的绘制线 [ ^ ]。



不做感觉?



请参阅这些答案:

捕获面板上的图纸 [ ^ ],

什么样的俏皮方法是Paint? (DataGridViewImageCell.Paint(...)) [ ^ ],

如何加速我的vb.net应用程序? [ ^ ],

如何从旧图纸中清除面板 [ ^ ],

用C#绘制一个矩形 [ ^ ],

在图片框中附加图片 [ ^ ]。



而且,对于更远程的替代品,还要考虑WPF:

连接不同的控件 [ ^ ](再次,询问者出现了想要连接控件),

C#或VB.NET中的CorelDraw或Inkscape等矢量图形软件[ ^ ],

架构图提示 [ ^ ]。



祝你好运,

-SA
It's wonderful how many different people come to the same idea again and again. I'm pretty much sure: this is possible, but this is a really a dead-end approach, making no practical sense. Picture boxes won't help you at all.

Please see my past answer: Drawing Lines between mdi child forms[^].

Doesn't it make sense?

Please see also these answers:
capture the drawing on a panel[^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
How to speed up my vb.net application?[^],
How do I clear a panel from old drawing[^],
draw a rectangle in C#[^],
Append a picture within picturebox[^].

And, for more remote alternative, also consider WPF:
Connect different controls[^] (again, the inquirer came up with the idea to connect controls),
Vector graphics software like CorelDraw or Inkscape in C# or VB.NET[^],
Architecture diagram tips[^].

Good luck,
—SA


除了一个小细节之外,您的代码似乎工作正常:图片框的坐标被错误计算。有一个简单的解决方案:

x1或x2 = pic.Left;

y1或y2 = pic.Top;
Your code seems to be working fine except for one small detail: the coordinates of the picturebox are wrongly calculated. There is a simple solution for that:
x1 or x2 = pic.Left;
y1 or y2 = pic.Top;


这篇关于我无法选择图片框来绘制它们之间的界限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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