本文整理汇总了C#中System.Windows.Forms.MeasureItemEventArgs.ItemWidth属性的典型用法代码示例。如果您正苦于以下问题:C# MeasureItemEventArgs.ItemWidth属性的具体用法?C# MeasureItemEventArgs.ItemWidth怎么用?C# MeasureItemEventArgs.ItemWidth使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类System.Windows.Forms.MeasureItemEventArgs
的用法示例。
在下文中一共展示了MeasureItemEventArgs.ItemWidth属性的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Dispose
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListBox listBox1;
private System.ComponentModel.Container components = null;
protected override void Dispose(bool disposing)
{
if( disposing )
{
if ( components != null )
components.Dispose();
if ( foreColorBrush != null )
foreColorBrush.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.listBox1 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
this.listBox1.Location = new System.Drawing.Point(16, 48);
this.listBox1.Name = "listBox1";
this.listBox1.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended;
this.listBox1.Size = new System.Drawing.Size(256, 134);
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.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.listBox1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void listBox1_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)
{
Font font = ((ListBoxFontItem)listBox1.Items[e.Index]).Font;
SizeF stringSize = e.Graphics.MeasureString(font.Name, font);
// Set the height and width of the item
e.ItemHeight = (int)stringSize.Height;
e.ItemWidth = (int)stringSize.Width;
}
// For efficiency, cache the brush to use for drawing.
private SolidBrush foreColorBrush;
private void listBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
Brush brush;
// Create the brush using the ForeColor specified by the DrawItemEventArgs
if ( foreColorBrush == null )
{
foreColorBrush = new SolidBrush(e.ForeColor);
}
else if ( foreColorBrush.Color != e.ForeColor )
{
// The control's ForeColor has changed, so dispose of the cached brush and
// create a new one.
foreColorBrush.Dispose();
foreColorBrush = new SolidBrush(e.ForeColor);
}
// Select the appropriate brush depending on if the item is selected.
// Since State can be a combinateion (bit-flag) of enum values, you can't use
// "==" to compare them.
if ( (e.State & DrawItemState.Selected) == DrawItemState.Selected )
brush = SystemBrushes.HighlightText;
else
brush = foreColorBrush;
// Perform the painting.
Font font = ((ListBoxFontItem)listBox1.Items[e.Index]).Font;
e.DrawBackground();
e.Graphics.DrawString(font.Name, font, brush, e.Bounds);
e.DrawFocusRectangle();
}
/// <summary>
/// A wrapper class for use with storing Fonts in a ListBox. Since ListBox uses the
/// ToString() of its items for the text it displays, this class is needed to return
/// the name of the font, rather than its ToString() value.
/// </summary>
public class ListBoxFontItem
{
public Font Font;
public ListBoxFontItem(Font f)
{
Font = f;
}
public override string ToString()
{
return Font.Name;
}
}
}
示例2: Main
//引入命名空间
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
class HatchBrushMenu: Form
{
HatchStyleMenuItem hsmiChecked;
const int iMin = 0, iMax = 52;
public static void Main()
{
Application.Run(new HatchBrushMenu());
}
public HatchBrushMenu()
{
ResizeRedraw = true;
Menu = new MainMenu();
Menu.MenuItems.Add("&Hatch-Style");
for (HatchStyle hs = (HatchStyle)iMin; hs <= (HatchStyle)iMax;hs++)
{
HatchStyleMenuItem hsmi = new HatchStyleMenuItem();
hsmi.HatchStyle = hs;
hsmi.Click += new EventHandler(MenuHatchStyleOnClick);
if ((int)hs % 8 == 0)
hsmi.BarBreak = true;
Menu.MenuItems[0].MenuItems.Add(hsmi);
}
hsmiChecked = (HatchStyleMenuItem) Menu.MenuItems[0].MenuItems[0];
hsmiChecked.Checked = true;
}
void MenuHatchStyleOnClick(object obj, EventArgs ea)
{
hsmiChecked.Checked = false;
hsmiChecked = (HatchStyleMenuItem) obj;
hsmiChecked.Checked = true;
Invalidate();
}
protected override void OnPaint(PaintEventArgs pea)
{
Graphics grfx = pea.Graphics;
HatchBrush hbrush = new HatchBrush(hsmiChecked.HatchStyle,
Color.White, Color.Black);
grfx.FillEllipse(hbrush, ClientRectangle);
}
}
class HatchStyleMenuItem: MenuItem
{
const int cxImage = 32, cyImage = 32, iMargin = 2;
readonly int cxCheck, cyCheck;
public HatchStyle HatchStyle;
public HatchStyleMenuItem()
{
OwnerDraw = true;
cxCheck = SystemInformation.MenuCheckSize.Width;
cyCheck = SystemInformation.MenuCheckSize.Height;
}
protected override void OnMeasureItem(MeasureItemEventArgs miea)
{
miea.ItemWidth = 2 * cxImage + 3 * iMargin - cxCheck;
miea.ItemHeight = cyImage + 2 * iMargin;
}
protected override void OnDrawItem(DrawItemEventArgs diea)
{
diea.DrawBackground();
if ((diea.State & DrawItemState.Checked) != 0)
{
ControlPaint.DrawMenuGlyph(diea.Graphics,
diea.Bounds.Location.X + iMargin,
diea.Bounds.Location.Y + iMargin,
cxImage, cyImage, MenuGlyph.Checkmark);
}
HatchBrush hbrush = new HatchBrush(HatchStyle,
Color.White, Color.Black);
diea.Graphics.FillRectangle(hbrush,
diea.Bounds.X + 2 * iMargin + cxImage,
diea.Bounds.Y + iMargin, cxImage, cyImage);
}
}