-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathClassListViewExtensions.cs
More file actions
137 lines (118 loc) · 4.62 KB
/
ClassListViewExtensions.cs
File metadata and controls
137 lines (118 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* Created by SharpDevelop.
* User: User
* Date: 05/06/2012
* Time: 19:14
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace WordListAnalyser2
{
/// <summary>
/// Description of ClassListViewExtensions.
///
/// From:
/// http://stackoverflow.com/questions/254129/how-to-i-display-a-sort-arrow-in-the-header-of-a-list-view-column-using-c
/// you can call the extension method like such:
///myListView.SetSortIcon(0, SortOrder.Ascending);
///It works by using P/Invoke to:
///Get the handle to the header control for a list view using the LVM_GETHEADER message.
///Get the information about a header column using the HDM_GETITEM message.
///It then modifies the fmt to set / clear the HDF_SORTDOWN and HDF_SORTUP flags on the returned HDITEM structure.
///Finally it re-sets the information usintg the HDM_SETITEM message.
///
/// </summary>
//public static class ClassListViewExtensions
//{
//public ClassListViewExtensions()
//{
//}
//extension method to set the sort arrow to a particular column:
[EditorBrowsable(EditorBrowsableState.Never)]
public static class ListViewExtensions
{
[StructLayout(LayoutKind.Sequential)]
public struct HDITEM
{
public Mask mask;
public int cxy;
[MarshalAs(UnmanagedType.LPTStr)] public string pszText;
private IntPtr hbm; //was Public
public int cchTextMax;
public Format fmt;
private IntPtr lParam; //was public
// _WIN32_IE >= 0x0300
public int iImage;
public int iOrder;
// _WIN32_IE >= 0x0500
public uint type;
private IntPtr pvFilter; //was public
// _WIN32_WINNT >= 0x0600
public uint state;
[Flags]
public enum Mask
{
Format = 0x4, // HDI_FORMAT
};
[Flags]
public enum Format
{
SortDown = 0x200, // HDF_SORTDOWN
SortUp = 0x400, // HDF_SORTUP
};
};
public const int LVM_FIRST = 0x1000;
public const int LVM_GETHEADER = LVM_FIRST + 31;
public const int HDM_FIRST = 0x1200;
public const int HDM_GETITEM = HDM_FIRST + 11;
public const int HDM_SETITEM = HDM_FIRST + 12;
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 msg, IntPtr wParam, ref HDITEM lParam);
//TODO Severity Code Description Project File Line Suppression State
// Warning CA1401 Change the accessibility of P/Invoke 'ListViewExtensions.SendMessage(IntPtr, uint, IntPtr, ref ListViewExtensions.HDITEM)' so that it is no longer visible from outside its assembly.WordListAnalyser2 C:\Users\Ray\Documents\GitHub\WordListAnalyser\ClassListViewExtensions.cs 88 Active
public static void SetSortIcon(this ListView listViewControl, int columnIndex, SortOrder order)
{
IntPtr columnHeader = SendMessage(listViewControl.Handle, LVM_GETHEADER, IntPtr.Zero, IntPtr.Zero);
for (int columnNumber = 0; columnNumber <= listViewControl.Columns.Count - 1; columnNumber++)
{
var columnPtr = new IntPtr(columnNumber);
var item = new HDITEM
{
mask = HDITEM.Mask.Format
};
if (SendMessage(columnHeader, HDM_GETITEM, columnPtr, ref item) == IntPtr.Zero)
{
throw new Win32Exception();
}
if (order != SortOrder.None && columnNumber == columnIndex)
{
switch (order)
{
case SortOrder.Ascending:
item.fmt &= ~HDITEM.Format.SortDown;
item.fmt |= HDITEM.Format.SortUp;
break;
case SortOrder.Descending:
item.fmt &= ~HDITEM.Format.SortUp;
item.fmt |= HDITEM.Format.SortDown;
break;
}
}
else
{
item.fmt &= ~HDITEM.Format.SortDown & ~HDITEM.Format.SortUp;
}
if (SendMessage(columnHeader, HDM_SETITEM, columnPtr, ref item) == IntPtr.Zero)
{
throw new Win32Exception();
}
}
}
}
}