获取列顺序(MFC) [英] Get Column Order (MFC)

查看:96
本文介绍了获取列顺序(MFC)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取MFC CListCtrl中列的顺序.最初,我尝试在消息处理程序中为HDN_ENDDRAG通知调用GetColumnOrderArray(),但是始终返回旧的(拖放前)列顺序.因此,根据

I'm trying to get the order of the columns in an MFC CListCtrl. Initially I tried calling GetColumnOrderArray() in a message handler for the HDN_ENDDRAG notification, but that always returned the old (pre-drag and drop) column order. So, based on the advice in this SO post's comment, I tried handling both the HDN_BEGINDRAG and the HDN_ENDDRAG and grabbing the old and new column orders with phdr->pitem->iOrder. But pitem is always NULL for me in the both handlers. No idea why.

SOOO,我尝试使用存储在消息(phdr->iItem)中的列索引直接与CHeaderCtrl交谈并自己获取列顺序,但是由标题控件填充的结构中的字段均无效;我仍然无法获得列顺序.

SOOO I tried using the column index stored in the message (phdr->iItem) to talk directly to the CHeaderCtrl and grab the column order myself, but the fields in the structure populated by my header control were all invalid; I still couldn't get the column order.

列表控件是否存在更深层次的问题?还是我只是错误地处理了邮件?

Is there some sort of deeper problem with my list control? Or am I just handling messages incorrectly?

HDN_BEGINDRAG消息处理程序:

BOOL CDFAManListView::OnHdnBegindrag(UINT, NMHDR *pNMHDR, LRESULT *pResult)
{
    LPNMHEADER phdr = reinterpret_cast<LPNMHEADER>(pNMHDR);

    phdr->iItem; // this contains a valid column index

    HDITEM columnStruct;

    List->GetHeaderCtrl()->GetItem(phdr->iItem, &columnStruct); // but this call just fills columnStruct with junk values

    if (phdr->pitem) // pitem is always null
    {
        initialPosition = phdr->pitem->iOrder;
    }

    *pResult = 0;
    return TRUE;
}

HDN_ENDDRAG消息处理程序:

void CDFAManListView::OnHdnEnddrag(NMHDR *pNMHDR, LRESULT *pResult)
{
    LPNMHEADER phdr = reinterpret_cast<LPNMHEADER>(pNMHDR);

    HDITEM columnStruct;

    List->GetHeaderCtrl()->GetItem(phdr->iItem, &columnStruct); // still just fills columnStruct with junk

    List->GetColumnOrderArray(signalColumnOrder); // gets **old** column order

    *pResult = 0;
}

推荐答案

虽然这是一个老问题,但我刚才在拖动CListCtrl列时遇到了这个问题,并认为我会对其进行一些更新以防万一给别人.

While this is an old question, I came across it just now while looking into CListCtrl column dragging and thought I'd update it a bit in case it's of use to somebody else.

OP提到了,

HDITEM columnStruct;

List->GetHeaderCtrl()->GetItem(phdr->iItem, &columnStruct); // but this call just fills columnStruct with junk values

这是因为您尚未初始化columnStruct足以告诉GetItem您想要检索哪些数据.您需要使用各种标志(例如HDI_WIDTH |)来初始化columnStruct.mask. HDI_ORDER,如果使用HDI_TEXT,则为columnStruct.pszText提供一个缓冲区,并为columnStruct.cchTextMax提供缓冲区的大小.

This will because you haven't initialised columnStruct enough to tell GetItem what data you're interested in retrieving. You need to initialise columnStruct.mask with various flags such as HDI_WIDTH | HDI_ORDER, and if you use HDI_TEXT then give columnStruct.pszText a buffer and columnStruct.cchTextMax the size of the buffer.

这是记录在MSDN上的CHeaderCtrl :: GetItem文档中例如.

在mask元素中设置的任何标志可确保 相应的元素会在返回时正确填写.如果面膜 元素设置为零,其他结构元素中的值是 毫无意义.

Any flags set in the mask element ensure that values in the corresponding elements are properly filled in upon return. If the mask element is set to zero, values in the other structure elements are meaningless.

这篇关于获取列顺序(MFC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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