Winform 缩放的时候 ListBox自动换行

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            this.ListBox1 = new System.Windows.Forms.ListBox();

            // Set the location and size.
            ListBox1.Location = new Point(20, 20);
            ListBox1.Size = new Size(400, 940);
            ListBox1.Dock = DockStyle.Fill;
            // Populate the ListBox.ObjectCollection property 
            // with several strings, using the AddRange method.
            this.ListBox1.Items.AddRange(new object[]{"System.Windows.Forms",
                "System.Drawing", "System.Xml", "System.Net", "System.Runtime.RemotingSystem.Runtime.RemotingSystem.Runtime.RemotingSystem.Runtime.RemotingSystem.Runtime.RemotingSystem.Runtime.RemotingSystem.Runtime.RemotingSystem.Runtime.RemotingSystem.Runtime.RemotingSystem.Runtime.RemotingSystem.Runtime.RemotingSystem.Runtime.RemotingSystem.Runtime.RemotingSystem.Runtime.RemotingSystem.Runtime.RemotingSystem.Runtime.RemotingSystem.Runtime.RemotingSystem.Runtime.RemotingSystem.Runtime.RemotingSystem.Runtime.RemotingSystem.Runtime.RemotingSystem.Runtime.RemotingSystem.Runtime.RemotingSystem.Runtime.RemotingSystem.Runtime.RemotingSystem.Runtime.RemotingSystem.Runtime.RemotingSystem.Runtime.RemotingSystem.Runtime.RemotingSystem.Runtime.RemotingSystem.Runtime.RemotingEND",
                "System.Web"});

            // Turn off the scrollbar.
            ListBox1.ScrollAlwaysVisible = false;
            ListBox1.IntegralHeight = false; 
            // Set the border style to a single, flat border.
            ListBox1.BorderStyle = BorderStyle.FixedSingle;

            ListBox1.SizeChanged += delegate { ListBox1.Refresh();};
            ListBox1.Resize += ListBox1_Resize;
            // Set the DrawMode property to the OwnerDrawVariable value. 
            // This means the MeasureItem and DrawItem events must be 
            // handled.
            ListBox1.DrawMode = DrawMode.OwnerDrawVariable;
            ListBox1.MeasureItem +=
                new MeasureItemEventHandler(ListBox1_MeasureItem);
            ListBox1.DrawItem += new DrawItemEventHandler(ListBox1_DrawItem);
            this.Controls.Add(this.ListBox1);


        }

        private void ListBox1_Resize(object sender, EventArgs e)
        {
            for (int i = 0; i < ListBox1.Items.Count; i++)
            {
                MeasureItemEventArgs eArgs = new MeasureItemEventArgs(ListBox1.CreateGraphics(), i);
                ListBox1_MeasureItem(ListBox1, eArgs);
                SendMessage(ListBox1.Handle, LB_SETITEMHEIGHT, i, eArgs.ItemHeight);
            }
           // ListBox1.Refresh();
        }
      
        public ListBox ListBox1 { get; set; }

        private void ListBox1_DrawItem(object sender, DrawItemEventArgs e)
        {

            // If the item is the selected item, then draw the rectangle
            // filled in blue. The item is selected when a bitwise And  
            // of the State property and the DrawItemState.Selected 
            // property is true.
            if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
            {
                e.Graphics.FillRectangle(Brushes.CornflowerBlue, e.Bounds);
            }
            else
            {
                // Otherwise, draw the rectangle filled in beige.
                e.Graphics.FillRectangle(Brushes.Beige, e.Bounds);
            }

            // Draw a rectangle in blue around each item.
            e.Graphics.DrawRectangle(Pens.Blue, e.Bounds);

            // Draw the text in the item.
            e.Graphics.DrawString(ListBox1.Items[e.Index].ToString(),
                this.Font, Brushes.Black, e.Bounds);

            // Draw the focus rectangle around the selected item.
            e.DrawFocusRectangle();
        }

        // Handle the MeasureItem event for an owner-drawn ListBox.
        private void ListBox1_MeasureItem(object sender,
            MeasureItemEventArgs e)
        {
            var lb = sender as ListBox;
            lb.DrawMode = DrawMode.OwnerDrawVariable;
          
 
            // Cast the sender object back to ListBox type.
            ListBox theListBox = (ListBox)sender;

            // Get the string contained in each item.
            string itemString = (string)theListBox.Items[e.Index];



            e.ItemHeight = (int)e.Graphics.MeasureString(itemString, lb.Font, lb.Width - 20).Height + 10;


          
        }

        private const int LB_SETITEMHEIGHT = 0x01A0;

        [DllImport("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int wMsg, int itemIndex, int itemHeight);

       
    }
}

效果图

原文地址:https://www.cnblogs.com/congqiandehoulai/p/15469397.html