Ron*_*Ron 19 c# listview row-height listviewitem
我想在listview中更改Item的/ Row的高度.
我找遍了那里,我想通,为了改变高度,我需要使用LBS_OWNERDRAWFIXED或MeasureItem或类似的东西.
问题是我不知道该做什么以及如何使用它.
任何人都可以帮助我吗?
编辑:
我不能使用ImageList黑客,因为我使用SmallImageList是真实的,我需要从ImageList图像大小不同的行高.
谢谢!
小智 25
对于仍在努力解决这个问题的人来说,这是我使用的代码:
private void SetHeight(ListView listView, int height)
{
ImageList imgList = new ImageList();
imgList.ImageSize = new Size(1, height);
listView.SmallImageList = imgList;
}
Run Code Online (Sandbox Code Playgroud)
要使用它,只需:
SetHeight(lvConnections, 25);
Run Code Online (Sandbox Code Playgroud)
Dav*_*son 15
你必须使用一点点黑客.诀窍是在StateImageList属性中使用图像列表.ListView将根据ImageList的ImageSize属性的高度调整其项高度.您不必为项目指定图像,但只需使用StateImageList将强制ListView进行调整.在下面的示例中,我将图像列表大小设置为32x32,从而产生32px高度的ListViewItem(s).

Gra*_*ian 14
可以使用这个SmallImageList技巧来完成- 你必须要小心.ObjectListView - 围绕标准.NET的开源包装器ListView- 使用该技巧成功实现RowHeight属性.
如果每行需要32个像素,请分配ImageList16x32(宽x高),然后将每个图像放在32像素高度的垂直中间.
此屏幕截图显示32像素行和由于额外空间可能的自动换行:

ObjectListView这一切对你有用吗 事实上,如果你试图用ListView做任何事情,你应该认真考虑使用一个ObjectListView.它使许多困难的事情(例如按列类型,自定义工具提示排序)变得微不足道,并且几个不可能的事情(例如覆盖,虚拟列表上的组)成为可能.
可悲的是LBS_OWNERDRAWFIXED,这些年来没有人回答您最初的问题如何使用。
您接受的答案是集成一个庞大的项目(带有 3.3MB 的演示和文档)。但只是为了设置 ListView 的行高,这是过度膨胀的。
此处建议的其他解决方法(添加 ImageList)仅适用于增加行高。但它不允许真正设置独立于图像高度的 RowHeight。此外,默认行高取决于操作系统。例如,在 Windows 7 上,行数远高于 XP。你不能选择让它们更紧,只能更高。
但是用很少的行,你可以做你想做的。只需复制并粘贴以下类:
using System;
using System.Drawing;
using System.Diagnostics;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace ExtendedControls
{
public class ListViewEx : ListView
{
#region Windows API
/*
struct MEASUREITEMSTRUCT
{
public int CtlType; // Offset = 0
public int CtlID; // Offset = 1
public int itemID; // Offset = 2
public int itemWidth; // Offset = 3
public int itemHeight; // Offset = 4
public IntPtr itemData;
}
*/
[StructLayout(LayoutKind.Sequential)]
struct DRAWITEMSTRUCT
{
public int ctlType;
public int ctlID;
public int itemID;
public int itemAction;
public int itemState;
public IntPtr hWndItem;
public IntPtr hDC;
public int rcLeft;
public int rcTop;
public int rcRight;
public int rcBottom;
public IntPtr itemData;
}
// LVS_OWNERDRAWFIXED: The owner window can paint ListView items in report view.
// The ListView control sends a WM_DRAWITEM message to paint each item. It does not send separate messages for each subitem.
const int LVS_OWNERDRAWFIXED = 0x0400;
const int WM_SHOWWINDOW = 0x0018;
const int WM_DRAWITEM = 0x002B;
const int WM_MEASUREITEM = 0x002C;
const int WM_REFLECT = 0x2000;
#endregion
bool mb_Measured = false;
int ms32_RowHeight = 14;
/// <summary>
/// Constructor
/// </summary>
public ListViewEx()
{
SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
}
/// <summary>
/// Sets the row height in Details view
/// This property appears in the Visual Studio Form Designer
/// </summary>
[Category("Appearance")]
[Description("Sets the height of the ListView rows in Details view in pixels.")]
public int RowHeight
{
get { return ms32_RowHeight; }
set
{
if (!DesignMode) Debug.Assert(mb_Measured == false, "RowHeight must be set before ListViewEx is created.");
ms32_RowHeight = value;
}
}
protected override CreateParams CreateParams
{
get
{
CreateParams k_Params = base.CreateParams;
k_Params.Style |= LVS_OWNERDRAWFIXED;
return k_Params;
}
}
/// <summary>
/// The messages WM_MEASUREITEM and WM_DRAWITEM are sent to the parent control rather than to the ListView itself.
/// They come here as WM_REFLECT + WM_MEASUREITEM and WM_REFLECT + WM_DRAWITEM
/// They are sent from Control.WmOwnerDraw() --> Control.ReflectMessageInternal()
/// </summary>
protected override void WndProc(ref Message k_Msg)
{
base.WndProc(ref k_Msg); // FIRST
switch (k_Msg.Msg)
{
case WM_SHOWWINDOW: // called when the ListView becomes visible
{
Debug.Assert(View == View.Details, "ListViewEx supports only Details view");
Debug.Assert(OwnerDraw == false, "In ListViewEx do not set OwnerDraw = true");
break;
}
case WM_REFLECT + WM_MEASUREITEM: // called once when the ListView is created, but only in Details view
{
mb_Measured = true;
// Overwrite itemHeight, which is the fifth integer in MEASUREITEMSTRUCT
Marshal.WriteInt32(k_Msg.LParam + 4 * sizeof(int), ms32_RowHeight);
k_Msg.Result = (IntPtr)1;
break;
}
case WM_REFLECT + WM_DRAWITEM: // called for each ListViewItem to be drawn
{
DRAWITEMSTRUCT k_Draw = (DRAWITEMSTRUCT) k_Msg.GetLParam(typeof(DRAWITEMSTRUCT));
using (Graphics i_Graph = Graphics.FromHdc(k_Draw.hDC))
{
ListViewItem i_Item = Items[k_Draw.itemID];
Color c_BackColor = i_Item.BackColor;
if (i_Item.Selected) c_BackColor = SystemColors.Highlight;
if (!Enabled) c_BackColor = SystemColors.Control;
using (SolidBrush i_BackBrush = new SolidBrush(c_BackColor))
{
// Erase the background of the entire row
i_Graph.FillRectangle(i_BackBrush, i_Item.Bounds);
}
for (int S=0; S<i_Item.SubItems.Count; S++)
{
ListViewItem.ListViewSubItem i_SubItem = i_Item.SubItems[S];
// i_Item.SubItems[0].Bounds contains the entire row, rather than the first column only.
Rectangle k_Bounds = (S>0) ? i_SubItem.Bounds : i_Item.GetBounds(ItemBoundsPortion.Label);
// You can use i_Item.ForeColor instead of i_SubItem.ForeColor to get the same behaviour as without OwnerDraw
Color c_ForeColor = i_SubItem.ForeColor;
if (i_Item.Selected) c_ForeColor = SystemColors.HighlightText;
if (!Enabled) c_ForeColor = SystemColors.ControlText;
TextFormatFlags e_Flags = TextFormatFlags.NoPrefix | TextFormatFlags.EndEllipsis | TextFormatFlags.VerticalCenter | TextFormatFlags.SingleLine;
switch (Columns[S].TextAlign)
{
case HorizontalAlignment.Center: e_Flags |= TextFormatFlags.HorizontalCenter; break;
case HorizontalAlignment.Right: e_Flags |= TextFormatFlags.Right; break;
}
TextRenderer.DrawText(i_Graph, i_SubItem.Text, i_SubItem.Font, k_Bounds, c_ForeColor, e_Flags);
}
}
break;
}
}
}
} // class
} // namespace
Run Code Online (Sandbox Code Playgroud)
将 ListViewEx 添加到您的表单后,您将在 Visual Studio 表单设计器中看到一个新属性,该属性允许以像素为单位设置行高:
您在那里输入的值将以像素为单位的行高,并且在所有操作系统上都将受到严格遵守。我在 Windows XP、7 和 10 上对其进行了测试:
此外,我的班级与原始 ListView 相比还有两个优点:它可以无闪烁地绘制,并且尊重原始 Microsoft ListView 忽略的ForeColor和Font集ListViewSubItem。所以你可以用不同的颜色和字体绘制每个单元格。
重要提示:由于MSDN说LBS_OWNERDRAWFIXED已经设计只为详细视图(报表视图)。我的代码仅适用于这种模式,这是因为 Microsoft 是这样设计的。
另外请注意,设置ListView.OwnerDraw = true与使用LVS_OWNERDRAWFIXED.
我没有实现绘图图标,因为我不需要那个。但是您可以轻松添加它。
| 归档时间: |
|
| 查看次数: |
60268 次 |
| 最近记录: |