Hello,
The code below represents a simple ownerdrawn,
Listview class in C#. Its purpose is to avoid
the flicker of the MS ListView. So I can't fall
back to their one.
Does anybody know why I cannot change the
size of the ListView's font (ListView.Font)
from e.g.
Microsoft Sans Serif; 8,25pt
to e.g.
Microsoft Sans Serif; 15,75pt ?
Can someone help ?
What I noticed is, that the text displayed is
growing as intended.
However, the rectangle returned by
ListViewItem.GetBounds(ItemBoundsPortion.Entire)
always remains fixed. That is, it remains for ever on the bounds
corresponding
to 8,25pt .
When compiled and being run, the blue rectangle around each item
displays
the entire item bounds. Pls, increase the font size, compile and run
again.
You'll then see the mess.
Regards, Martin
it follows FlickerfreeListView.cs and Form1.cs
pls watch for word-wrap
FlickerfreeListView.cs
------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace flickerfree
{
/// <summary>
/// Summary description for FlickerfreeListView.
/// </summary>
public class FlickerfreeListView : System.Windows.Forms.ListView
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public FlickerfreeListView()
{
// This call is required by the Windows.Forms Form
Designer.
InitializeComponent();
// TODO: Add any initialization after the InitForm call
SetStyle( ControlStyles.DoubleBuffer
| ControlStyles.UserPaint
| ControlStyles.AllPaintingInWmPaint
, true);
}
void DrawItem( ListViewItem item, PaintEventArgs pea )
{
SolidBrush fbrush;
SolidBrush bbrush;
if( true == item.Selected )
{
if( this.Focused )
{
bbrush = new SolidBrush( SystemColors.Highlight );
fbrush = new SolidBrush(
SystemColors.HighlightText );
}
else
{
bbrush = new SolidBrush(
SystemColors.InactiveCaption );
fbrush = new SolidBrush(
SystemColors.InactiveCaptionText );
}
}
else
{
bbrush = new SolidBrush( SystemColors.Window );
fbrush = new SolidBrush( item.ForeColor );
}
pea.Graphics.FillRectangle(bbrush,item.GetBounds(I temBoundsPortion.Label));
pea.Graphics.DrawString(item.Text,item.Font,fbrush ,item.GetBounds(ItemBoundsPortion.Label).Location) ;
pea.Graphics.DrawRectangle(System.Drawing.Pens.Blu e,item.GetBounds(ItemBoundsPortion.Entire));
}
protected override void OnPaint(PaintEventArgs pea)
{
Rectangle rc = pea.ClipRectangle;
foreach( ListViewItem item in Items )
{
if( true == rc.IntersectsWith(
item.GetBounds(ItemBoundsPortion.Entire)) )
DrawItem( item, pea );
}
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Component 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()
{
components = new System.ComponentModel.Container();
}
#endregion
}
}
Form1.cs
-----------------------------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace flickerfree
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private flickerfree.FlickerfreeListView flickerfreeListView1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after
InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.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.flickerfreeListView1 = new
flickerfree.FlickerfreeListView();
this.SuspendLayout();
//
// flickerfreeListView1
//
this.flickerfreeListView1.Dock =
System.Windows.Forms.DockStyle.Fill;
this.flickerfreeListView1.Font = new
System.Drawing.Font("Microsoft Sans Serif", 8.25F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
((System.Byte)(0)));
this.flickerfreeListView1.Name = "flickerfreeListView1";
this.flickerfreeListView1.Size = new
System.Drawing.Size(512, 277);
this.flickerfreeListView1.TabIndex = 0;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
this.ClientSize = new System.Drawing.Size(512, 277);
this.Controls.AddRange(new System.Windows.Forms.Control[]
{
this.flickerfreeListView1});
this.Font = new System.Drawing.Font("Microsoft Sans
Serif", 9.75F, System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
for(int i=0; i<5; i++)
this.flickerfreeListView1.Items.Add("item#"+i.ToSt ring());
}
}
}