甚至不知道如何开始解决这个问题

时间:2010-11-21 19:32:13

标签: c# winforms user-controls

以下是客户要求我创建的模型: alt text

以下是我在实际软件中的内容:

alt text

问题是我需要能够从下面的控件中删除图片。我不知道如何处理这个问题。任何指导?

以下是我制作的自定义用户控件的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WebServiceScanner
{
    public partial class selectablePicture : UserControl
    {
        public selectablePicture(Image image)
        {
            InitializeComponent();
            ptbImage.BackgroundImage = image;
            ptbImage.BackgroundImageLayout = ImageLayout.Zoom;
        }

        public Image GetImage()
        {
            return ptbImage.BackgroundImage;
        }

        public bool IsSelected()
        {
            return chkSelected.Checked;
        }

        public void DisableCheckbox()
        {
            this.chkSelected.Enabled = false;
        }

        private void ptbImage_Click(object sender, EventArgs e)
        {
            SelectControl();
            ptbImage.Focus();
            ToggleCheckBox();            
        }

        private void selectablePicture_Click(object sender, EventArgs e)
        {
            SelectControl();
            this.Focus();
            ToggleCheckBox();            
        }

        private void ToggleCheckBox()
        {
            if (!chkSelected.Enabled == false)
            {
                if (chkSelected.Checked)
                {
                    chkSelected.Checked = false;
                }
                else
                {
                    chkSelected.Checked = true;
                }
            }            
        }

        private void chkSelected_Click(object sender, EventArgs e)
        {
            SelectControl();
            chkSelected.Focus();
        }

        private void SelectControl()
        {
            if (!chkSelected.Enabled == false)
            {
                this.BackColor = Color.FromArgb(89, 168, 248);
            }            
        }

        private void chkSelected_Leave(object sender, EventArgs e)
        {
            DeSelectControl();
        }

        private void ptbImage_Leave(object sender, EventArgs e)
        {
            DeSelectControl();
        }

        private void selectablePicture_Leave(object sender, EventArgs e)
        {
            DeSelectControl();
        }

        private void DeSelectControl()
        {
            //If none of the controls inside the usercontrol have focus, set this control to white.
            if (!chkSelected.Enabled == false)
            {
                if (!this.Focused && !this.ptbImage.Focused && !this.chkSelected.Focused)
                {
                    this.BackColor = Color.White;
                }
            }            
        }
    }
}

以下是我如何使用它:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WebServiceScanner
{
    public partial class MainForm : Form
    {
        int pictureXPosition = 0;
        List<selectablePicture> Images;

        public MainForm()
        {
            InitializeComponent();
            ptbNewestPicture.BackgroundImageLayout = ImageLayout.Zoom;
            Images = new List<selectablePicture>();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            LoadImageFromScanner();
        }

        private void scanToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoadImageFromScanner();
        }

        private void scanBatchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoadBatchImagesFromScanner();
        }

        private void btnScanBatch_Click(object sender, EventArgs e)
        {
            LoadBatchImagesFromScanner();
        }

        private void connectionSettingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowConfigurationForm();
        }


        private void LoadImageFromScanner()
        {            
            selectablePicture picture = new selectablePicture(Image.FromFile(@"C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg"));
            ptbNewestPicture.BackgroundImage = picture.GetImage();

            picture.Location = new Point(pictureXPosition + panel1.AutoScrollPosition.X, 0);
            panel1.Controls.Add(picture);            
            pictureXPosition += 130;
        }

        private void LoadBatchImagesFromScanner()
        {
            throw new NotImplementedException();
        }

        private void ShowConfigurationForm()
        {
            ConnectionSettingsForm connectionConfig = new ConnectionSettingsForm();
            connectionConfig.ShowDialog();
        }

        private void btnDeleteSelected_Click(object sender, EventArgs e)
        {
            DeleteSelectedPictures();
        }

        private void DeleteSelectedPictures()
        {
            foreach (Control c in panel1.Controls)
            {
                if (((selectablePicture)c).IsSelected())
                {
                    DisablePicture(c);
                    Images.Remove((selectablePicture)c);
                }
            }
        }

        private void DisablePicture(Control c)
        {
            c.BackColor = Color.Gray;
            ((selectablePicture)c).DisableCheckbox();
        }
    }
}

我考虑过维护一个用户控件列表并在每次更改时重新绘制整个列表,但这不好,因为如果有很多图片会发生什么?

任何指导?

如果我想拖动和重新排序图片,我需要遵循哪些步骤?谢谢!

2 个答案:

答案 0 :(得分:2)

一种解决方案根本不是将它们渲染为控件,而是创建一个从左到右渲染缩略图的控件(根据是否选中它来修改所选图像的渲染)。

删除图片现在很简单,就是这样,删除了。然后你不再需要担心它,它不再在你的渲染列表中。无需删除控件。

此自定义控件的开销将更少(每个图像的1个控件与'N'个控件)。

您现在可以轻松实现许多其他优势,例如将效果应用于附近的图像等等。当然,这可以通过单独的控件来完成,但这样就可以减少球的疼痛。

某些指标

N        = Total number of images
T_WIDTH  = Thumbnail width
MARGIN_W = Margin you want either side of the thumbnail
CELL_W   = T_WIDTH + (MARGIN_W * 2)   (Total width of a cell that contains a thumbnail)

// Pseudo code here!!!
Scrollbar.Width = CELL_W * N
Scrollbar.PageWidth = Control.ClientWidth / CELL_W

开始在

渲染的图像索引
Scrollbar.Pos / CELL_W

请记住根据滚动条位置在CELL_W中的位置来偏移开始渲染的实际x位置。

答案 1 :(得分:2)

使用FlowControlPanel代替简单Panel。然后你可以添加和删除它的控件,他们会排队。