Здравствуйте, Soul, Вы писали:
S>Задача: сделать ListView такой же как в Windows в Моем компьютере при просмотре папок на диске, т.е. чтобы была иконка сортировки справа.
S>Вопрос: Как это сделать?
Используя Interop. Примерно так:
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Rsdn
{
static class ListViewHelper
{
public static void SetSortImage(ListView listView, int columnIndex, SortOrder sortOrder)
{
if (listView == null)
{
throw new ArgumentNullException("listView");
}
if ((columnIndex < 0) || (columnIndex >= listView.Columns.Count))
{
throw new ArgumentOutOfRangeException("columnIndex");
}
if (!listView.IsHandleCreated)
{
throw new ApplicationException("ListView handle must be created.");
}
LVCOLUMN col = new LVCOLUMN();
col.mask = LVCF_FMT;
SendMessage(new HandleRef(listView, listView.Handle), LVM_GETCOLUMNW, columnIndex, ref col);
col.fmt &= ~(LVCFMT_SORTDOWN | LVCFMT_SORTUP);
switch (sortOrder)
{
case SortOrder.Ascending:
col.fmt |= LVCFMT_SORTUP;
break;
case SortOrder.Descending:
col.fmt |= LVCFMT_SORTDOWN;
break;
}
SendMessage(new HandleRef(listView, listView.Handle), LVM_SETCOLUMNW, columnIndex, ref col);
}
#region Interop
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
struct LVCOLUMN
{
public int mask;
public int fmt;
public int cx;
public IntPtr pszText;
public int cchTextMax;
public int iSubItem;
public int iImage;
public int iOrder;
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(HandleRef hWnd, int msg, int wParam, ref LVCOLUMN lParam);
const int LVM_FIRST = 0x1000;
const int LVM_GETCOLUMNW = LVM_FIRST + 95;
const int LVM_SETCOLUMNW = LVM_FIRST + 96;
const int LVCF_FMT = 1;
const int LVCFMT_SORTDOWN = 0x0200;
const int LVCFMT_SORTUP = 0x0400;
#endregion
}
}