首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

c# 有控件会显示下图的效果吗

2013-09-07 
c# 有控件能显示下图的效果吗?关键是哪个复选框怎么加上啊?listview和imagelist能显示图片using Systemus

c# 有控件能显示下图的效果吗?
c# 有控件会显示下图的效果吗

关键是哪个复选框怎么加上啊?
listview和imagelist能显示图片


using System;
using System.Windows.Forms;
using System.Drawing;
namespace Demo
{
    /// <summary>
    /// 图片格子用户控件
    /// </summary>
    public partial class ImageCell : UserControl
    {
        public ImageCell()
        {
            InitializeComponent();
            SetupControls();
        }
        private void SetupControls()
        {
            //图像按比例缩放
            pbImage.SizeMode = PictureBoxSizeMode.StretchImage;
            //设置标签居中显示
            lblTitle.AutoSize = false;
            lblTitle.TextAlign = ContentAlignment.TopCenter;
        }

        //复选框
        public bool ImageCellCheck
        {
            get { return ckbCheck.Checked; }
            set { ckbCheck.Checked = value; }
        }


        //图像
        public Image ImageCellImage
        {
            get { return pbImage.Image; }
            set { pbImage.Image = value; }
        }
        //标题
        public string ImageCellTitle
        {
            get { return lblTitle.Text; }
            set { lblTitle.Text = value; }
        }
    }
}


定义一个容器ImageCellContainer:

using System;
using System.Windows.Forms;
using System.IO;
using System.Drawing;
namespace Demo
{
    public class ImageCellContainer : FlowLayoutPanel
    {
        private const string FILE_PATH = "Cards/";
        private ImageCell[] imageCells=null;

        public ImageCellContainer()
        {
            LoadImage();
        }
        //或者从其他数据源获取数据进行绑定
        private void LoadImage()
        {
            try
            {
                string[] files = Directory.GetFiles(FILE_PATH);
                imageCells = new ImageCell[files.Length];
                for (int i = 0; i < files.Length; i++)
                {
                    Image image = Image.FromFile(string.Format("{0}", files[i]));


                    ImageCell cell = new ImageCell();
                    cell.ImageCellCheck = i % 2 == 0 ? true : false;
                    cell.ImageCellImage = image;
                    cell.ImageCellTitle = (i + 1) + "." + files[i];
                    imageCells[i]=cell;
                }
                this.Controls.AddRange(imageCells);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}


使用该自定义控件:

using System;
using System.Windows.Forms;
using System.Drawing;
namespace Demo
{
    public partial class Form1 : Form
    {
        ImageCellContainer imageCellContainer;
        public Form1()
        {
            InitializeComponent();

            this.BackColor = Color.LightGreen;
            imageCellContainer = new ImageCellContainer();
            imageCellContainer.Dock = DockStyle.Fill;
            imageCellContainer.AutoScroll = true;
            this.Controls.Add(imageCellContainer);
        }


    }
}


效果:
c# 有控件会显示下图的效果吗
这个实现很粗糙,有待你的改善

热点排行