拖放时为什么面板上的图片之间有空白?

时间:2019-04-14 13:58:31

标签: c# visual-studio

因此,我正在尝试在可以正常工作的面板上拖放图片框,但是不知何故,框之间有空白空间,我不明白为什么会发生这种情况?

下面,通过单击图片,您将可以看到我的情况的示例,以及后面使用面板和图像框上的事件的代码-DragDrop,DragOver和MouseDown。

Example

private void panelMap_DragOver(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.All;
    }

 private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        if (!runningSimulation)
        {
            pictureBox1.DoDragDrop(pictureBox1.Image, DragDropEffects.Copy);
            dragTypeOne = true;
        }
    }` private void panelMap_DragDrop(object sender, DragEventArgs e)
    {
        Point cursor = PointToClient(Cursor.Position);
        Point drawPoint = new Point();

        //Finding out which cell the crossing is dropped on
        determineCell(e);
        drawPoint = map.Cells[selectedCell - 1].Location;

        //Moving the crossing
        if (e.Effect == DragDropEffects.Move)
        {
            foreach (Crossing cr in map.Crossings)
            {
                if (cr.Location == mouseDown && !map.Cells[selectedCell - 1].Taken)
                {
                    cr.Location = drawPoint;
                    // cr.Pb_Background.Location = drawPoint;
                    cr.Pb_Transparent.Location = drawPoint;
                    map.Cells[selectedCell - 1].Taken = true;
                    map.Cells[selectedCell - 1].Crossing = map.Cells[firstSelection - 1].Crossing;
                    map.Cells[firstSelection - 1].Taken = false;
                    map.Cells[firstSelection - 1].Crossing = null;
                    //grid.CheckNeighbours(selectedCell - 1);
                    for (int i = 0; i < map.Cells.Count; i++)
                    {
                        if (map.Cells[i].Taken)
                        {
                            map.CheckNeighbours(i);
                        }
                    }
                }
            }
        }
        else if (!map.Cells[selectedCell - 1].Taken)
        {
            map.Cells[selectedCell - 1].Taken = true;
            PictureBox pb_b = new PictureBox();
            pb_b.Height = panelMap.Height / 3;
            pb_b.Width = panelMap.Width / 4;
            pb_b.SizeMode = PictureBoxSizeMode.StretchImage;
            pb_b.BackColor = Color.Transparent;
            pb_b.MouseDown += new MouseEventHandler(Mouse_Down);
            pb_b.Image = (Image)e.Data.GetData(DataFormats.Bitmap);

            //Added transparent layer
            PictureBox pb_t = new PictureBox();
            pb_t.Size = pb_b.Size;
            pb_t.MouseDown += new MouseEventHandler(Mouse_Down);

            //Which crossing will be dragged
            if (dragTypeOne)
            {
                System.Drawing.Graphics formGraphics = this.CreateGraphics();
                Cross_Shaped cr = new Cross_Shaped(drawPoint, pb_b, pb_t);
                pb_b.Location = cr.Location;
                pb_b.Tag = cr;
                pb_t.Tag = cr;
                map.AddCrossing(cr);
                map.Cells[selectedCell - 1].Crossing = cr;

                pb_b.Image = CTF.Properties.Resources.crossing;
            }

            this.Controls.Add(pb_b);
            this.Controls.Add(pb_t);


            pb_t.BringToFront();


            pb_b.Location = drawPoint;

            pb_b.Parent = panelMap;
            pb_t.Parent = pb_b;
            map.CheckNeighbours(selectedCell - 1);
        }
    }`

0 个答案:

没有答案