为什么这个方法不起作用? [英] Why this method doesn't work?

查看:106
本文介绍了为什么这个方法不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要显示排序箭头的int类型的列表视图的头,我发现这一点:

<一个href=\"http://stackoverflow.com/questions/254129/how-to-i-display-a-sort-arrow-in-the-header-of-a-list-view-column-using-c\">How要我用一个列表视图列的标题显示排序箭头C#?

和它的作品。

我试图修改code这个:

 
常量的Int32 HDF_SORTDOWN =为0x200;
常量的Int32 HDF_SORTUP = 0x400的;
常量的Int32 HDI_FORMAT =为0x4;
常量的Int32 HDM_GETITEM = 0x120b;
常量的Int32 HDM_SETITEM = 0x120c;
常量的Int32 LVM_GETHEADER = 0x101f;函数[DllImport(user32.dll中,字符集= CharSet.Auto)
静态外部的IntPtr SendMessage函数(IntPtr的的HWND,UInt32的味精,IntPtr的的wParam,lParam的IntPtr的);函数[DllImport(user32.dll中,字符集= CharSet.Auto,入口点=SendMessage函数)]
静态外部的IntPtr SendMessageLVCOLUMN(IntPtr的的HWND,UInt32的味精,IntPtr的wParam中,裁判LVCOLUMN lParam的);结构LVCOLUMN
{
    公共UInt32的口罩;
    公众的Int32 FMT;
    公众的Int32 CX;
    公共字符串pszText;
    公众的Int32 cchTextMax定义;
    公众的Int32 iSubItem;
    公众的Int32 IIMAGE;
    公众的Int32 iOrder;
    公众的Int32 cxMin;
    公众的Int32 cxDefault;
    公众的Int32 cxIdeal;
}私人无效SetSortIcon(ListView中lstVw,INT列,SortOrder的排序)
{
    IntPtr的clmHdr = SendMessage函数(lstVw.Handle,LVM_GETHEADER,IntPtr.Zero,IntPtr.Zero);    的for(int i = 0; I&LT; lstVw.Columns.Count;我++)
    {
        的IntPtr clmPtr =新的IntPtr(ⅰ);
        LVCOLUMN LVCOLUMN =新LVCOLUMN();        lvColumn.mask = HDI_FORMAT;
        SendMessageLVCOLUMN(clmHdr,HDM_GETITEM,clmPtr,楼盘LVCOLUMN);
        如果(排序!= SortOrder.None &&我==列)
        {
            如果(排序== SortOrder.Ascending)
            {
                lvColumn.fmt&=〜HDF_SORTDOWN;
                lvColumn.fmt | = HDF_SORTUP;
            }
            其他
            {
                lvColumn.fmt&=〜HDF_SORTUP;
                lvColumn.fmt | = HDF_SORTDOWN;
            }
        }
        其他
        {
            lvColumn.fmt&=〜HDF_SORTDOWN&〜HDF_SORTUP;
        }
        SendMessageLVCOLUMN(clmHdr,HDM_SETITEM,clmPtr,楼盘LVCOLUMN);
    }
}

这是行不通的。

原始形式是一种扩展方法,而这是不

我想知道为什么这个是行不通的。


解决方案

您需要更正LVCOLUMN结构布局,将其更改为相同的顺序和类型,在原来的例子。

<$p$p><$c$c>[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
私人结构LVCOLUMN
{
    公众的Int32口罩;
    公众的Int32 CX;
    [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)
    公共字符串pszText;
    公共IntPtr的HBM;
    公众的Int32 cchTextMax定义;
    公众的Int32 FMT;
    公众的Int32 iSubItem;
    公众的Int32 IIMAGE;
    公众的Int32 iOrder;
}

I want to display a sort arrow int the header of a list view, and I found this:

How to I display a sort arrow in the header of a list view column using C#?

And it works.

I tried to modify the code into this:


const Int32 HDF_SORTDOWN = 0x200;
const Int32 HDF_SORTUP = 0x400;
const Int32 HDI_FORMAT = 0x4;
const Int32 HDM_GETITEM = 0x120b;
const Int32 HDM_SETITEM = 0x120c;
const Int32 LVM_GETHEADER = 0x101f;

[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam);

[DllImport("user32.dll", CharSet = CharSet.Auto, EntryPoint = "SendMessage")]
static extern IntPtr SendMessageLVCOLUMN(IntPtr hWnd, UInt32 msg, IntPtr wParam, ref LVCOLUMN lParam);

struct LVCOLUMN
{
    public UInt32 mask;
    public Int32 fmt;
    public Int32 cx;
    public String pszText;
    public Int32 cchTextMax;
    public Int32 iSubItem;
    public Int32 iImage;
    public Int32 iOrder;
    public Int32 cxMin;
    public Int32 cxDefault;
    public Int32 cxIdeal;
}

private void SetSortIcon(ListView lstVw, int column, SortOrder sorting)
{
    IntPtr clmHdr = SendMessage(lstVw.Handle, LVM_GETHEADER, IntPtr.Zero, IntPtr.Zero);

    for (int i = 0; i < lstVw.Columns.Count; i++)
    {
        IntPtr clmPtr = new IntPtr(i);
        LVCOLUMN lvColumn = new LVCOLUMN();

        lvColumn.mask = HDI_FORMAT;
        SendMessageLVCOLUMN(clmHdr, HDM_GETITEM, clmPtr, ref lvColumn);
        if (sorting != SortOrder.None && i == column)
        {
            if (sorting == SortOrder.Ascending)
            {
                lvColumn.fmt &= ~HDF_SORTDOWN;
                lvColumn.fmt |= HDF_SORTUP;
            }
            else
            {
                lvColumn.fmt &= ~HDF_SORTUP;
                lvColumn.fmt |= HDF_SORTDOWN;
            }
        }
        else
        {
            lvColumn.fmt &= ~HDF_SORTDOWN & ~HDF_SORTUP;
        }
        SendMessageLVCOLUMN(clmHdr, HDM_SETITEM, clmPtr, ref lvColumn);
    }
}

This doesn't work.

The original version is an Extension Method, and this isn't.

I want to know why this one doesn't work.

解决方案

You need to correct the layout of the LVCOLUMN struct, change it to be the same order and types as in the original example.

[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]    
private struct LVCOLUMN    
{        
    public Int32 mask;        
    public Int32 cx;        
    [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)]
    public string pszText;
    public IntPtr hbm;
    public Int32 cchTextMax;
    public Int32 fmt;
    public Int32 iSubItem;
    public Int32 iImage;
    public Int32 iOrder;
}

这篇关于为什么这个方法不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆