111,120
社区成员
发帖
与我相关
我的任务
分享
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 10;i++ )
{
this.myListBox1.Items.Add("item" + i.ToString ());
}
}
}
public class MyListBox : System.Windows.Forms.ListBox
{
public MyListBox()
{
this.DrawMode = DrawMode.OwnerDrawVariable;
this.DrawItem += new DrawItemEventHandler(MyListBox_DrawItem);
}
private void MyListBox_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
if (e.Index == -1) return;
Font txtFnt = new Font(this.Font.Name, this.Font.Size);
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
txtFnt = new Font(this.Font.Name, this.Font.Size, FontStyle.Italic | FontStyle.Bold);
}
if (e.Index % 4 == 0 | e.Index % 4 == 1) {
e.Graphics.FillRectangle(Brushes.White, e.Bounds);
}
else {
e.Graphics.FillRectangle(Brushes.LightCyan, e.Bounds);
}
StringFormat SF = new StringFormat();
SF.LineAlignment = StringAlignment.Center;
e.Graphics.DrawString(" " + this.Items[e.Index].ToString(), txtFnt, Brushes.Blue, e.Bounds, SF);
e.DrawFocusRectangle();
}
}
}
Public Class MyListBox
Inherits System.Windows.Forms.ListBox
Public Sub New()
Me.DrawMode = Windows.Forms.DrawMode.OwnerDrawVariable
End Sub
Private Sub MyListBox_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Me.DrawItem
If e.Index = -1 Then Exit Sub
Dim txtFnt As Font = New Font(Me.Font.Name, Me.Font.Size)
If e.State And System.Windows.Forms.DrawItemState.Selected Then
txtFnt = New Font(Me.Font.Name, Me.Font.Size, FontStyle.Italic Or FontStyle.Bold)
End If
If e.Index Mod 4 = 0 Or e.Index Mod 4 = 1 Then
e.Graphics.FillRectangle(Brushes.White, e.Bounds)
Else
e.Graphics.FillRectangle(Brushes.LightCyan, e.Bounds)
End If
Dim SF As New StringFormat : SF.LineAlignment = StringAlignment.Center
e.Graphics.DrawString(" " + Items(e.Index).ToString, txtFnt, Brushes.Blue, e.Bounds, SF)
e.DrawFocusRectangle()
End Sub
Private Sub MyListBox_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles Me.MeasureItem
Dim itmSize As SizeF = e.Graphics.MeasureString(Items(e.Index).ToString, Me.Font, New Size(Me.Width, 400))
e.ItemHeight = itmSize.Height * 2 : e.ItemWidth = itmSize.Width
End Sub
End Class
public class MyListBox : System.Windows.Forms.ListBox
{
public MyListBox()
{
this.DrawMode = DrawMode.OwnerDrawVariable;
}
private void MyListBox_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
if (e.Index == -1) return;
Font txtFnt = new Font(this.Font.Name, this.Font.Size);
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
txtFnt = new Font(this.Font.Name, this.Font.Size, FontStyle.Italic | FontStyle.Bold);
}
if (e.Index % 4 == 0 | e.Index % 4 == 1) {
e.Graphics.FillRectangle(Brushes.White, e.Bounds);
}
else {
e.Graphics.FillRectangle(Brushes.LightCyan, e.Bounds);
}
StringFormat SF = new StringFormat();
SF.LineAlignment = StringAlignment.Center;
e.Graphics.DrawString(" " + this.Items[e.Index].ToString(), txtFnt, Brushes.Blue, e.Bounds, SF);
e.DrawFocusRectangle();
}
private void MyListBox_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)
{
SizeF itmSize = e.Graphics.MeasureString(this.Items[e.Index].ToString(), this.Font, new Size(this.Width, 400));
e.ItemHeight =(int) itmSize.Height * 2;
e.ItemWidth = (int)itmSize.Width;
}
}