我想在列表视图的标题中显示一个排序箭头,我发现了这个:
How to I display a sort arrow in the header of a list view column using C#?
它有效。
我尝试将代码修改为:
const Int32 HDF_SORTDOWN = 0x200;
const Int32 HDF_SORTUP = 0x400;
const Int32 HDI_FORMAT = 0x4;
const Int32 HDM_GETITEM = 0x120b;
const Int32 HDM_SETITEM = 0x120c;
const Int32 LVM_GETHEADER = 0x101f;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, EntryPoint = "SendMessage")]
static extern IntPtr SendMessageLVCOLUMN(IntPtr hWnd, UInt32 msg, IntPtr wParam, ref LVCOLUMN lParam);
struct LVCOLUMN
{
public UInt32 mask;
public Int32 fmt;
public Int32 cx;
public String pszText;
public Int32 cchTextMax;
public Int32 iSubItem;
public Int32 iImage;
public Int32 iOrder;
public Int32 cxMin;
public Int32 cxDefault;
public Int32 cxIdeal;
}
private void SetSortIcon(ListView lstVw, int column, SortOrder sorting)
{
IntPtr clmHdr = SendMessage(lstVw.Handle, LVM_GETHEADER, IntPtr.Zero, IntPtr.Zero);
for (int i = 0; i < lstVw.Columns.Count; i++)
{
IntPtr clmPtr = new IntPtr(i);
LVCOLUMN lvColumn = new LVCOLUMN();
lvColumn.mask = HDI_FORMAT;
SendMessageLVCOLUMN(clmHdr, HDM_GETITEM, clmPtr, ref lvColumn);
if (sorting != SortOrder.None && i == column)
{
if (sorting == SortOrder.Ascending)
{
lvColumn.fmt &= ~HDF_SORTDOWN;
lvColumn.fmt |= HDF_SORTUP;
}
else
{
lvColumn.fmt &= ~HDF_SORTUP;
lvColumn.fmt |= HDF_SORTDOWN;
}
}
else
{
lvColumn.fmt &= ~HDF_SORTDOWN & ~HDF_SORTUP;
}
SendMessageLVCOLUMN(clmHdr, HDM_SETITEM, clmPtr, ref lvColumn);
}
}
这不起作用。
原始版本是扩展方法,但事实并非如此。
我想知道为什么这个不起作用。
答案 0 :(得分:3)
您需要更正LVCOLUMN结构的布局,将其更改为与原始示例中相同的顺序和类型。
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
private struct LVCOLUMN
{
public Int32 mask;
public Int32 cx;
[System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)]
public string pszText;
public IntPtr hbm;
public Int32 cchTextMax;
public Int32 fmt;
public Int32 iSubItem;
public Int32 iImage;
public Int32 iOrder;
}
答案 1 :(得分:1)
简单的修复,我敢打赌你需要将编译器从任何CPU设置为32或86.它会正常工作。您正在使用win32库。需要将构建设置为32或86.如果这不起作用告诉我播种我将再看一下代码,如果它的工作标记为已回答。