using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data; using System.ComponentModel; using System.Runtime.InteropServices; using Microsoft.Win32; using System.Diagnostics; namespace TradeIdeas.TIProGUI { /*This class was copied verbatim from a website(link below). I just had to modifiy some stuff within the listview column click to work with our column sort. website: http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/5efd6d3a-c5bd-4729-b48c-d05b06265911/ */ public class ListViewWithColumnArrow : System.Windows.Forms.ListView { #region Win32 [StructLayout(LayoutKind.Sequential)] private struct HDITEM { public Int32 mask; public Int32 cxy; [MarshalAs(UnmanagedType.LPTStr)] public String pszText; public IntPtr hbm; public Int32 cchTextMax; public Int32 fmt; public Int32 lParam; public Int32 iImage; public Int32 iOrder; }; private int sortIconColumn = 0; [DllImport("user32")] static extern IntPtr SendMessage(IntPtr Handle, Int32 msg, IntPtr wParam, IntPtr lParam); [DllImport("user32", EntryPoint = "SendMessage")] static extern IntPtr SendMessage2(IntPtr Handle, Int32 msg, IntPtr wParam, ref HDITEM lParam); const Int32 HDI_WIDTH = 0x0001; const Int32 HDI_HEIGHT = HDI_WIDTH; const Int32 HDI_TEXT = 0x0002; const Int32 HDI_FORMAT = 0x0004; const Int32 HDI_LPARAM = 0x0008; const Int32 HDI_BITMAP = 0x0010; const Int32 HDI_IMAGE = 0x0020; const Int32 HDI_DI_SETITEM = 0x0040; const Int32 HDI_ORDER = 0x0080; const Int32 HDI_FILTER = 0x0100; // 0x0500 const Int32 HDF_LEFT = 0x0000; const Int32 HDF_RIGHT = 0x0001; const Int32 HDF_CENTER = 0x0002; const Int32 HDF_JUSTIFYMASK = 0x0003; const Int32 HDF_RTLREADING = 0x0004; const Int32 HDF_OWNERDRAW = 0x8000; const Int32 HDF_STRING = 0x4000; const Int32 HDF_BITMAP = 0x2000; const Int32 HDF_BITMAP_ON_RIGHT = 0x1000; const Int32 HDF_IMAGE = 0x0800; const Int32 HDF_SORTUP = 0x0400; // 0x0501 const Int32 HDF_SORTDOWN = 0x0200; // 0x0501 const Int32 LVM_FIRST = 0x1000; // List messages const Int32 LVM_GETHEADER = LVM_FIRST + 31; const Int32 HDM_FIRST = 0x1200; // Header messages const Int32 HDM_SETIMAGELIST = HDM_FIRST + 8; const Int32 HDM_GETIMAGELIST = HDM_FIRST + 9; const Int32 HDM_GETITEM = HDM_FIRST + 11; const Int32 HDM_SETITEM = HDM_FIRST + 12; #endregion public ListViewWithColumnArrow() { this.ColumnClick += new ColumnClickEventHandler(ListViewWithColumnArrow_ColumnClick); } void ListViewWithColumnArrow_ColumnClick(object sender, ColumnClickEventArgs e) { if (this.Columns[e.Column].Tag == null) { this.Columns[e.Column].Tag = "None"; } for (int i = 0; i < this.Columns.Count; i++) { this.ShowHeaderIcon(this, i, SortOrder.None); } if (this.Columns[e.Column].Tag.ToString() == "Ascending") { this.ShowHeaderIcon(this, e.Column, SortOrder.Ascending); this.Columns[e.Column].Tag = "Descending"; } else if (this.Columns[e.Column].Tag.ToString() == "Descending") { this.ShowHeaderIcon(this, e.Column, SortOrder.Descending); this.Columns[e.Column].Tag = "Ascending"; } else if (this.Columns[e.Column].Tag.ToString() == "None") { this.ShowHeaderIcon(this, e.Column, SortOrder.Descending); this.Columns[e.Column].Tag = "Descending"; } } public int getColumnIconIndex() { return sortIconColumn; } #region ShowHeaderIcon public void ShowHeaderIcon(ListView list, int columnIndex, SortOrder sortOrder) { if (columnIndex < 0 || columnIndex >= list.Columns.Count) return; IntPtr hHeader = SendMessage(list.Handle, LVM_GETHEADER, IntPtr.Zero, IntPtr.Zero); ColumnHeader colHdr = list.Columns[columnIndex]; HDITEM hd = new HDITEM(); hd.mask = HDI_FORMAT; HorizontalAlignment align = colHdr.TextAlign; if (align == HorizontalAlignment.Left) hd.fmt = HDF_LEFT | HDF_STRING | HDF_BITMAP_ON_RIGHT; else if (align == HorizontalAlignment.Center) hd.fmt = HDF_CENTER | HDF_STRING | HDF_BITMAP_ON_RIGHT; else // HorizontalAlignment.Right hd.fmt = HDF_RIGHT | HDF_STRING; if (sortOrder == SortOrder.Ascending) { hd.fmt |= HDF_SORTUP; sortIconColumn = columnIndex; } else if (sortOrder == SortOrder.Descending) { hd.fmt |= HDF_SORTDOWN; sortIconColumn = columnIndex; } SendMessage2(hHeader, HDM_SETITEM, new IntPtr(columnIndex), ref hd); } #endregion } }