2012-10-08 19 views
1

我有一個winforms應用程序(C#,.NET 3.5,但最終遷移到4.0),它使用ListView以類似的風格呈現附件(文件/網頁鏈接) Windows資源管理器。在列表視圖中使用列ImageKey來模擬Windows資源管理器風格排序

此功能已得到他的應用程序用戶的好評。請求已作出edxpand這包括分類的細節通過單擊列標題(同Windows資源管理器)查看

Windows explorer sorting

這是排序是我的目標風格(使用三角形字形以指示排列方向)

我已經接近通過使用ImageKey屬性來實現此目的。

if (lvAttachments.ListViewItemSorter != null) 
    { 
     lvAttachments.Sort(); 

     if (lvwColumnSorter != null) 
     { 
      if (lvwColumnSorter.Order == System.Windows.Forms.SortOrder.Ascending) 
       lvAttachments.Columns[lvwColumnSorter.SortColumn].ImageKey = "sortAsc"; 
      if (lvwColumnSorter.Order == System.Windows.Forms.SortOrder.Descending) 
       lvAttachments.Columns[lvwColumnSorter.SortColumn].ImageKey = "sortDesc"; 
     } 
    } 

不幸的是,圖像顯示在文本的左側,而不是右側。

enter image description here

的columnHeader TextAlign屬性設置爲左,也有設計師提供一些其他的特性,沒有出現任何用處。

有沒有人知道是否有可能使圖像顯示在文本的右側,而它是左對齊,還是有另一種方法,我將不得不使用?

我遇到的一個選擇是在標題文本中使用unicode字形。但是,如果可能的話,我寧願避免這種情況,因爲unicode字形並不特別好。

回答

0

我知道如何做到這一點的唯一方法是通過PInvoke的:

[StructLayoutAttribute(LayoutKind.Sequential)] 
    internal struct LV_COLUMN 
    { 
     public UInt32 mask; 
     public Int32 fmt; 
     public Int32 cx; 
     public String pszText; 
     public Int32 cchTextMax; 
     public Int32 iSubItem; 
     public Int32 iImage; 
     public Int32 iOrder; 
    } 

    [DllImport("User32", EntryPoint = "SendMessage", CharSet = CharSet.Auto)] 
    internal static extern IntPtr SendLVColMessage(IntPtr hWnd, UInt32 msg, UInt32 wParam, ref LV_COLUMN lParam); 

    internal const uint LVCF_FMT = 0x1; 
    internal const uint LVCF_IMAGE = 0x10; 
    internal const int LVCFMT_IMAGE = 0x800; 
    internal const int LVCFMT_BITMAP_ON_RIGHT = 0x1000; 

    // Get the old format flags 
    col.mask = LVNative.LVCF_FMT; 
    LVNative.SendLVColMessage(lvSessions.Handle, LVNative.LVM_GETCOLUMN, (UInt32)iCol, ref col); 

    // Set the new format flags 
    col.mask = LVNative.LVCF_FMT | LVNative.LVCF_IMAGE; 
    col.fmt |= LVNative.LVCFMT_IMAGE | LVNative.LVCFMT_BITMAP_ON_RIGHT; 
    col.iImage = (bAscending) ? (int)SessionIcons.SortAscending : (int)SessionIcons.SortDescending; 
    LVNative.SendLVColMessage(lvSessions.Handle, LVNative.LVM_SETCOLUMN, (UInt32)iCol, ref col); 
+1

但見http://stackoverflow.com/questions/254129/how-to-i-display-a-sort-arrow -in-the-header-of-a-list-view-column-using-c,它是一個模仿資源管理器的例子。 – EricLaw

相關問題