控制listview大图标之间的间距

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;
using System.IO;
using System.Runtime.InteropServices;
using System.Collections;

namespace FormTest
{
    public partial class FormListView : Form
    {
        #region  IMPORT DLL
        // <summary>
        /// 控制ListView项之间的距离
        
/// </summary>
        
/// <param name="Handle"></param>
        
/// <param name="wMsg"></param>
        
/// <param name="wParam">水平间距</param>
        
/// <param name="lParam">垂直间距</param>
        
/// <returns></returns>
        [DllImport("User32.dll")]
        private static extern int SendMessage(IntPtr Handle, int wMsg, int wParam, int lParam);

        const int LVM_FIRST = 0x1000;
        const int LVM_SETICONSPACING = LVM_FIRST + 53;

        public static void SetListViewSpacing(ListView lst, int x, int y)
        {
            SendMessage(lst.Handle, LVM_SETICONSPACING, 0, x * 65536 + y);
        }

        #endregion


        /// <summary>
        
/// 当前屏幕大小
        
/// </summary>
        private int[] screenResolution = new int[] { 00 };
        protected List<ArrayList> lal = new List<ArrayList>();
        private string elevenName = "智能销售", appName = "应用程序", syncName = "更新任务", processName = "任务列表", helpName = "系统工具", downloadName = "活动资料";
        
        public FormListView()
        {
            InitializeComponent();
            int add = 1;

            this.listViewApp.Size = new Size(listViewApp.Size.Width * add, listViewApp.Size.Height * add);
            this.Width = this.Width * add;
            this.Height = this.Height * add;
            if (add.Equals(1))
            {
                listViewApp.BackgroundImage = Resource_App.M3;
            }
            else
            {
                listViewApp.BackgroundImage = Resource_App.MC75A;
            }
        }

        
        private void FormListView_Load(object sender, EventArgs e)
        {
            screenResolution = new int[] { listViewApp.Size.Width, listViewApp.Size.Height };
            
            //其中80控制行距,75控制列距
            if (screenResolution[0].Equals(240))
            {
                SetListViewSpacing(listViewApp, 8075);
                //SetBackgroundImage(listViewApp, Resource_App.M3);
            }
            else
            {
                SetListViewSpacing(listViewApp, 160150);
                //SetBackgroundImage(listViewApp, Resource_App.MC75A);
            }

            InitListView(listViewApp);
        }

        private void InitListView(ListView listView)
        {
            ArrayList al = new ArrayList();
            if (screenResolution[0].Equals(240))
            {
                al.Add(Resource_App.MCMS_M3);
                al.Add(elevenName);
                lal.Add(al);
                al = new ArrayList();
                al.Add(Resource_App.App_M3);
                al.Add(appName);
                lal.Add(al);
                al = new ArrayList();
                al.Add(Resource_App.Sync_M3);
                al.Add(syncName);
                lal.Add(al);
                al = new ArrayList();
                al.Add(Resource_App.Process_M3);
                al.Add(processName);
                lal.Add(al);
                al = new ArrayList();
                al.Add(Resource_App.Help_M3);
                al.Add(helpName);
                lal.Add(al);
                al = new ArrayList();
                al.Add(Resource_App.Download_M3);
                al.Add(downloadName);
                lal.Add(al);
                this.GetAppConfig(4848);//获得应用程序
            }
            else
            {
                al.Add(Resource_App.MCMS_MC75A);
                al.Add(elevenName);
                lal.Add(al);
                al = new ArrayList();
                al.Add(Resource_App.App_MC75A);
                al.Add(appName);
                lal.Add(al);
                al = new ArrayList();
                al.Add(Resource_App.Sync_MC75A);
                al.Add(syncName);
                lal.Add(al);
                al = new ArrayList();
                al.Add(Resource_App.Process_MC75A);
                al.Add(processName);
                lal.Add(al);
                al = new ArrayList();
                al.Add(Resource_App.Help_MC75A);
                al.Add(helpName);
                lal.Add(al);
                al = new ArrayList();
                al.Add(Resource_App.Download_MC75A);
                al.Add(downloadName);
                lal.Add(al);
                this.GetAppConfig(9092);//获得应用程序
            }
        }

        private void GetAppConfig(int w, int h)  //获得应用程序
        {

            listViewApp.Items.Clear();
            ImageList imgDefautIcon = new ImageList();
            imgDefautIcon.ImageSize = new Size(w, h);

            //增加图片

            for (int i = 0; i < lal.Count; i++)
            {
                ListViewItem listview = new ListViewItem();
                listview.Text = lal[i][1].ToString();
                Bitmap b = (Bitmap)lal[i][0];
                imgDefautIcon.Images.Add(b);
                listview.BackColor = Color.FromArgb(105105105);
                listview.ForeColor = Color.Black;
                listview.ImageIndex = i;
                listViewApp.Items.Add(listview);
            }
            listViewApp.LargeImageList = imgDefautIcon;
        }


        private void listViewApp_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.listViewApp.SelectedIndices.Count > 0)
            {
                int AppIdx = listViewApp.SelectedIndices[0];
                onClick(AppIdx);
            }
        }

        private void onClick(int index)
        {
            if (index.Equals(0))
            {
                picboxEleven_Click();
            }
            else if (index.Equals(1))
            {
                picApp_Click();
            }
            else if (index.Equals(2))
            {
                picboxSync_Click();
            }
            else if (index.Equals(3))
            {
                picboxProcess_Click();
            }
            else if (index.Equals(4))
            {
                picboxHelp_Click();
            }
            else if (index.Equals(5))
            {
                picboxDownload_Click();
            }
        }

        private void picboxEleven_Click()
        {
            MessageBox.Show(elevenName);
        }

        private void picApp_Click()
        {
            MessageBox.Show(appName);
        }

        private void picboxSync_Click()
        {
            MessageBox.Show(syncName);
        }

        private void picboxProcess_Click()
        {
            MessageBox.Show(processName);
        }

        private void picboxHelp_Click()
        {
            MessageBox.Show(helpName);
        }

        private void picboxDownload_Click()
        {
            MessageBox.Show(downloadName);
        }
    }
}
原文地址:https://www.cnblogs.com/xsmhero/p/2725845.html