ListView
基本コントロールが列内の値を揃える必要があるタブ文字を食べるため、描画された所有者を実装しようとしています。
MSDNの例をベースとして使用すると、近づくことができました。私がまだ持っている唯一の問題は、テキストが列に収まらないときに使用される省略記号のピリオドが、デフォルトのテキスト レンダリングよりもはるかに狭い間隔になっていることです。フォントが太字の場合、ピリオドが一緒になってアンダースコアになるという点。
以下のプログラムは、問題を示しています。4 つListView
の s があります。上部の 2 つは、デフォルトのレンダリングを使用して描画されます。下の 2 つは所有者が描いたもので、右側のペアは太字です。長さの理由から、問題を示すために必要のないものはすべて削除しました。これが、描画された所有者ListView
に列ヘッダーがない理由です。
拡大されたスクリーンショットを見ると、描画された所有者の省略記号の期間はListView
1 ピクセル離れています。デフォルトの描画では、2 ピクセルの間隔があります。太字がピリオドを 2 ピクセルに広げると、所有者が描いたピリオドが一緒になってアンダースコアのように見える固体の塊になります。
テキストのレンダリングには、他にも小さな違いがあります。しかし、ズームしなくてもすぐにわかるのは省略記号だけです。ただし、これらの違いにより、問題がより一般的な問題であると思われます。 おそらくGDI対GDI +レンダリング?ただし、それはアプリケーション レベルでしか変化しないと考えていました。 どうやらそうではなく、トグルApplication.SetCompatibleTextRenderingDefault()
しても何の影響もありませんでした。
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public class Form1 : Form
{
private void ListViewDrawSubItem(object sender,
DrawListViewSubItemEventArgs e)
{
ListView listView = sender as ListView;
using (StringFormat sf = new StringFormat())
{
// Draw the standard background.
e.DrawBackground();
sf.SetTabStops(0, new float[] {12, 12, 12, 12, 12});
sf.FormatFlags = sf.FormatFlags | StringFormatFlags.NoWrap;
sf.Trimming = StringTrimming.EllipsisCharacter;
// Draw the header text.
// passing the controls font directly causes an
// ArguementException);
using (Font headerFont = new Font(listView.Font.Name,
listView.Font.Size,
listView.Font.Style))
{
e.Graphics.DrawString(e.SubItem.Text, headerFont,
Brushes.Black, e.Bounds, sf);
}
}
}
public Form1()
{
InitializeComponent();
LoadData(listView1);
LoadData(listView2);
LoadData(listView3);
LoadData(listView4);
}
private void LoadData(ListView listView)
{
listView.Columns.Add("first", 35);
listView.Columns.Add("second", 75);
for (int i = 0; i < 5; i++)
{
listView.Items.Add("test");
listView.Items[i].SubItems.Add("test test test test");
}
}
#region from Form1.Designer
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
/// disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (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.listView1 = new System.Windows.Forms.ListView();
this.listView2 = new System.Windows.Forms.ListView();
this.listView3 = new System.Windows.Forms.ListView();
this.listView4 = new System.Windows.Forms.ListView();
this.SuspendLayout();
//
// listView1
//
this.listView1.Location = new System.Drawing.Point(12, 12);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(121, 116);
this.listView1.TabIndex = 0;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.View = System.Windows.Forms.View.Details;
//
// listView2
//
this.listView2.Font = new System.Drawing.Font("Microsoft Sans Serif",
8.25F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point,
((byte)(0)));
this.listView2.Location = new System.Drawing.Point(151, 12);
this.listView2.Name = "listView2";
this.listView2.Size = new System.Drawing.Size(121, 116);
this.listView2.TabIndex = 1;
this.listView2.UseCompatibleStateImageBehavior = false;
this.listView2.View = System.Windows.Forms.View.Details;
//
// listView3
//
this.listView3.Location = new System.Drawing.Point(12, 134);
this.listView3.Name = "listView3";
this.listView3.OwnerDraw = true;
this.listView3.Size = new System.Drawing.Size(121, 116);
this.listView3.TabIndex = 2;
this.listView3.UseCompatibleStateImageBehavior = false;
this.listView3.View = System.Windows.Forms.View.Details;
this.listView3.DrawSubItem += new
System.Windows.Forms.DrawListViewSubItemEventHandler(
this.ListViewDrawSubItem);
//
// listView4
//
this.listView4.Font = new System.Drawing.Font("Microsoft Sans Serif",
8.25F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point,
((byte)(0)));
this.listView4.Location = new System.Drawing.Point(151, 134);
this.listView4.Name = "listView4";
this.listView4.OwnerDraw = true;
this.listView4.Size = new System.Drawing.Size(121, 116);
this.listView4.TabIndex = 3;
this.listView4.UseCompatibleStateImageBehavior = false;
this.listView4.View = System.Windows.Forms.View.Details;
this.listView4.DrawSubItem += new
System.Windows.Forms.DrawListViewSubItemEventHandler(
this.ListViewDrawSubItem);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.listView4);
this.Controls.Add(this.listView3);
this.Controls.Add(this.listView2);
this.Controls.Add(this.listView1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.ListView listView2;
private System.Windows.Forms.ListView listView3;
private System.Windows.Forms.ListView listView4;
#endregion
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}