首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

ListView控件小图标或列表显示时,如何调整各item的间距?

2012-07-23 
ListView控件小图标或列表显示时,怎么调整各item的间距????????????我用ListView列出文件(夹),但用小图标

ListView控件小图标或列表显示时,怎么调整各item的间距????????????
我用ListView列出文件(夹),但用小图标或列表显示时,各个Item挨得很近,密密麻麻。
不是文件名遮住图标,就是文件名显示不全。
网上搜了半天也没找到解决方法。

[解决办法]
可以把文字后面加点空格不?
[解决办法]
参见:
ListView.OwnerDraw Property 
http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.ownerdraw.aspx


using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Globalization;
using System.Windows.Forms;

public class ListViewOwnerDraw : Form
{
private ListView listView1 = new ListView();
private ContextMenu contextMenu1 = new ContextMenu();

public ListViewOwnerDraw()
{
// Initialize the ListView control.
listView1.BackColor = Color.Black;
listView1.ForeColor = Color.White;
listView1.Dock = DockStyle.Fill;
listView1.View = View.Details;
listView1.FullRowSelect = true;

// Add columns to the ListView control.
listView1.Columns.Add("Name", 100, HorizontalAlignment.Center);
listView1.Columns.Add("First", 100, HorizontalAlignment.Center);
listView1.Columns.Add("Second", 100, HorizontalAlignment.Center);
listView1.Columns.Add("Third", 100, HorizontalAlignment.Center);

// Create items and add them to the ListView control.
ListViewItem listViewItem1 = new ListViewItem(new string[] { "One", "20", "30", "-40" }, -1);
ListViewItem listViewItem2 = new ListViewItem(new string[] { "Two", "-250", "145", "37" }, -1);
ListViewItem listViewItem3 = new ListViewItem(new string[] { "Three", "200", "800", "-1,001" }, -1);
ListViewItem listViewItem4 = new ListViewItem(new string[] { "Four", "not available", "-2", "100" }, -1);
listView1.Items.AddRange(new ListViewItem[] { listViewItem1, listViewItem2, listViewItem3, listViewItem4 });

// Initialize the shortcut menu and 
// assign it to the ListView control.
contextMenu1.MenuItems.Add("List",
new EventHandler(menuItemList_Click));
contextMenu1.MenuItems.Add("Details",
new EventHandler(menuItemDetails_Click));
listView1.ContextMenu = contextMenu1;

// Configure the ListView control for owner-draw and add 
// handlers for the owner-draw events.
listView1.OwnerDraw = true;
listView1.DrawItem += new
DrawListViewItemEventHandler(listView1_DrawItem);
listView1.DrawSubItem += new
DrawListViewSubItemEventHandler(listView1_DrawSubItem);
listView1.DrawColumnHeader += new
DrawListViewColumnHeaderEventHandler(listView1_DrawColumnHeader);

// Add a handler for the MouseUp event so an item can be 
// selected by clicking anywhere along its width.
listView1.MouseUp += new MouseEventHandler(listView1_MouseUp);

// Add handlers for various events to compensate for an 
// extra DrawItem event that occurs the first time the mouse 
// moves over each row. 
listView1.MouseMove += new MouseEventHandler(listView1_MouseMove);
listView1.ColumnWidthChanged += new ColumnWidthChangedEventHandler(listView1_ColumnWidthChanged);
listView1.Invalidated += new InvalidateEventHandler(listView1_Invalidated);

// Initialize the form and add the ListView control to it.
this.ClientSize = new Size(450, 150);


this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Text = "ListView OwnerDraw Example";
this.Controls.Add(listView1);
}

// Clean up any resources being used.
protected override void Dispose(bool disposing)
{
if (disposing)
{
contextMenu1.Dispose();
}
base.Dispose(disposing);
}

[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new ListViewOwnerDraw());
}

// Sets the ListView control to the List view.
private void menuItemList_Click(object sender, EventArgs e)
{
listView1.View = View.List;
listView1.Invalidate();
}

// Sets the ListView control to the Details view.
private void menuItemDetails_Click(object sender, EventArgs e)
{
listView1.View = View.Details;

// Reset the tag on each item to re-enable the workaround in
// the MouseMove event handler.
foreach (ListViewItem item in listView1.Items)
{
item.Tag = null;
}
}

// Selects and focuses an item when it is clicked anywhere along 
// its width. The click must normally be on the parent item text.
private void listView1_MouseUp(object sender, MouseEventArgs e)
{
ListViewItem clickedItem = listView1.GetItemAt(5, e.Y);
if (clickedItem != null)
{
clickedItem.Selected = true;
clickedItem.Focused = true;
}
}

// Draws the backgrounds for entire ListView items.
private void listView1_DrawItem(object sender,
DrawListViewItemEventArgs e)
{
if ((e.State & ListViewItemStates.Selected) != 0)
{
// Draw the background and focus rectangle for a selected item.
e.Graphics.FillRectangle(Brushes.Maroon, e.Bounds);
e.DrawFocusRectangle();
}
else
{
// Draw the background for an unselected item.
using (LinearGradientBrush brush =
new LinearGradientBrush(e.Bounds, Color.Orange,
Color.Maroon, LinearGradientMode.Horizontal))
{
e.Graphics.FillRectangle(brush, e.Bounds);
}
}

// Draw the item text for views other than the Details view.
if (listView1.View != View.Details)
{
e.DrawText();
}
}


[解决办法]
楼主把楼上的复制放到ide里试试,太多了看的头晕。
[解决办法]
如果是平铺的情况可以用 listview 的属性 TileSize。
图标或列表显示就不知道了。
帮顶

热点排行