public static void SetListViewHeaderArrow(ListView listView, int columnIndex, bool isAscending) { IntPtr header = (IntPtr)SendMessage(listView.Handle, 0x1000 + 31 /* LVM_GETHEADER */, 0, 0); for (int i = 0; i < listView.Columns.Count; i++) { HDITEM hdi = new HDITEM(); hdi.mask = 0x0004 /* HDI_FORMAT */; SendMessage(header, 0x1200 + 11 /* HDM_GETITEM */, (uint)i, ref hdi); hdi.fmt &= ~(0x0200 /* HDF_SORTDOWN */ | 0x0400 /* HDF_SORTUP */); if (i == columnIndex) { hdi.fmt |= isAscending ? 0x0400 /* HDF_SORTUP */ : 0x0200 /* HDF_SORTDOWN */; } SendMessage(header, 0x1200 + 12 /* HDM_SETITEM */, (uint)i, ref hdi); } SendMessage(listView.Handle, 0x1000 + 140 /* LVM_SETSELECTEDCOLUMN */, (uint)columnIndex, 0); }
static extern int SendMessage(IntPtr hWnd, uint Msg, uint wParam, ref HDITEM lParam);