Why this method doesn't work?
I want to display a sort arrow int the header of a list view, and I found this:
How to I display a sort arrow in the header of a list view column using C#?
And it works.
I tried to modify the code into this:
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 wP开发者_StackOverflowaram, 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);
}
}
This doesn't work.
The original version is an Extension Method, and this isn't.
I want to know why this one doesn't work.
You need to correct the layout of the LVCOLUMN struct, change it to be the same order and types as in the original example.
[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;
}
Easy fix, my bet is you need to set the compiler from Any CPU to 32 or 86. and it will work just fine. You are using win32 libraries. Need to set the build to 32 or 86. If this doesn't work tell me sow i will take another look at the code, if it works mark as answered.
精彩评论