⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 C#150例
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Reflection;

namespace Example109_自画ListBox
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.ListBox listBox1;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.listBox1 = new System.Windows.Forms.ListBox();
			this.SuspendLayout();
			// 
			// listBox1
			// 
			this.listBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
			this.listBox1.Items.AddRange(new object[] {
														  "1",
														  "2",
														  "3",
														  "4",
														  "5"});
			this.listBox1.Location = new System.Drawing.Point(24, 8);
			this.listBox1.Name = "listBox1";
			this.listBox1.Size = new System.Drawing.Size(216, 184);
			this.listBox1.TabIndex = 0;
			this.listBox1.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.listBox1_MeasureItem);
			this.listBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listBox1_DrawItem);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(264, 213);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.listBox1});
			this.Name = "Form1";
			this.Text = "Form1";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private Brush[] listBoxBrushes ;
		//该数组用来存储绘制listBox1背景的Brush对象
		private int[] listBoxHeights = new int[] {50, 25, 33, 15,20} ;
		//该数组用来存储listBox1各列表项的预定义高度

		private void Form1_Load(object sender, System.EventArgs e)
		{
			Bitmap backgroundImage = new Bitmap("..\\..\\First.bmp");
			Brush backgroundBrush = new TextureBrush(backgroundImage);
			//创建brush,将使用它画ListBox中第一个列表项的背景
			
			Rectangle r = new Rectangle(0, 0, listBox1.Width, 100);
			LinearGradientBrush lb = new LinearGradientBrush(r, Color.Red,									Color.Yellow,LinearGradientMode.Horizontal);
			//创建一个渐变画刷,将使用它画第三个列表项的背景

			listBoxBrushes = new Brush[]
				{
					backgroundBrush,
					Brushes.LemonChiffon,
					lb,
					Brushes.CornflowerBlue,
					Brushes.PeachPuff
				};
			//创建Brush类的数组listBoxBrushes
			//其中包含了自定义的backgroundBrush、lb
			//系统定义的Brush中的LemonChiffon、CornflowerBlue、PeachPuff

		}

		private void listBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
		{
			e.DrawBackground();

			Brush brush = listBoxBrushes[e.Index];
			e.Graphics.FillRectangle(brush, e.Bounds);
			//用指定的画刷填充列表项范围所形成的矩形

			e.Graphics.DrawRectangle(SystemPens.WindowText, e.Bounds);
			//画列表项的边框

			bool selected = ((e.State & DrawItemState.Selected)
				== DrawItemState.Selected) ? true : false;
			string displayText = "ITEM #" + e.Index;
			displayText = displayText + (selected ? " SELECTED" : "");
			e.Graphics.DrawString(displayText, this.Font, Brushes.Black, e.Bounds);
			//显示列表项上的字符并且如果该列表项处于选中状态
			//则再添加字符SELECTED

			e.DrawFocusRectangle();
			//绘制聚焦框

		}

		private void listBox1_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)
		{
			string displayText = "ITEM #" + e.Index;
			SizeF stringSize=e.Graphics.MeasureString(displayText, this.Font);
			stringSize.Height += 6;
			if (listBoxHeights[e.Index] > stringSize.Height)
				e.ItemHeight = listBoxHeights[e.Index];
			else
				e.ItemHeight = (int)stringSize.Height;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -