MFC中使用多行项目

MFC中使用多行项目

本文介绍了如何在ListControl MFC中使用多行项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Visual Studio 2013(C ++)中有一个带有项目列表(报告视图)的MFC列表控件

I have an MFC List Control in Visual Studio 2013 (C++) with a List of items (Report view)

   LVCOLUMN lvColumn;

        lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
        lvColumn.fmt = LVCFMT_LEFT;
        lvColumn.cx = 120;
        lvColumn.pszText = "Full Name";
        ((CListCtrl*)GetDlgItem(IDC_LIST1))->InsertColumn(0, &lvColumn);

        lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
        lvColumn.fmt = LVCFMT_LEFT;
        lvColumn.cx = 75;
        lvColumn.pszText = "Profession";
        ((CListCtrl*)GetDlgItem(IDC_LIST1))->InsertColumn(1, &lvColumn);

        lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
        lvColumn.fmt = LVCFMT_LEFT;
        lvColumn.cx = 80;
        lvColumn.pszText = "Fav Sport";
        ((CListCtrl*)GetDlgItem(IDC_LIST1))->InsertColumn(2, &lvColumn);

        lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
        lvColumn.fmt = LVCFMT_LEFT;
        lvColumn.cx = 75;
        lvColumn.pszText = "Hobby";
        ((CListCtrl*)GetDlgItem(IDC_LIST1))->InsertColumn(3, &lvColumn);

        LVITEM lvItem;
        int nItem;

        lvItem.mask = LVIF_TEXT;
        lvItem.iItem = 0;
        lvItem.iSubItem = 0;
        lvItem.pszText = "Sandra C. Anschwitz";
        nItem = ((CListCtrl*)GetDlgItem(IDC_LIST1))->InsertItem(&lvItem);

        ((CListCtrl*)GetDlgItem(IDC_LIST1))->SetItemText(nItem, 1, "Singer");
        ((CListCtrl*)GetDlgItem(IDC_LIST1))->SetItemText(nItem, 2, "HandBall");
        ((CListCtrl*)GetDlgItem(IDC_LIST1))->SetItemText(nItem, 3, "Beach");

如何为全名,职业,体育和爱好提供多行项目?

How can I have multiline items for Full Name, Profession, Sport and Hobby?

推荐答案

令人惊讶的是,使用默认的CListCtrl不可能做到这一点.但是,只需进行一些自定义编码(和一些技巧),就可以得到想要的效果.

Surprisingly, this is not possible with the default CListCtrl. But, with a little custom coding (and some trickery), you can get the effect you want.

首先,您需要从CListCtrl派生您自己的类,并为控件样式设置所有者绘制位( Owner Draw Fixed = true ).在您的父对话框类中,创建一个图像列表(这是窍门).图像列表将用于指定列表控件每一行的高度.在下面的示例中,我使用了:

First, you’ll need to derive your own class from CListCtrl and set the owner draw bit (Owner Draw Fixed = true) for the control style. In your parent dialog class, create an image list (here’s the trickery). The image list will be used to specify the height of each row of the list control. In my example below, I used:

m_imagelist.Create(48, 48, ILC_COLOR4, 10, 10);
m_listctrl.SetImageList(&m_imagelist, LVSIL_SMALL);

您需要使用图像列表的 cx cy 值来适应您的需求.您的控件将使用图像列表来调整每行的大小,因为它预期会显示图标.接下来,为DrawItem添加一个处理程序,如下所示:

You’ll need to play around with the cx and cy values for the image list to fit your needs. Your control will use the image list to size each row because it’s anticipating displaying icons. Next, add a handler for DrawItem like this:

void MyClistCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
    {
    CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);

    CString text = _T("Now is the time \nfor all good men\nto come to the aid");
    pDC->DrawText(text , &lpDrawItemStruct->rcItem, DT_TOP);
    // TODO:  Add your code to draw the specified item
    }

在我的示例中,这导致…

In my example, this results in…

这可能不是一个很好的解决方案,但是它可以工作.注意:采用这种方法,每一行的高度都将相同.

It may not be an elegant solution, but, it works. Note: With this approach, every row will have the same height.

编辑:有几种获取行文本的方法.最简单的方法是像这样使用GetItemText:

EDIT: There are a few ways to obtain the row text. The easiest would be to use GetItemText like so:

CString txt = GetItemText(lpDrawItemStruct->itemID, 0);
pDC->DrawText(txt, &lpDrawItemStruct->rcItem, DT_TOP);

以上假设您使用CListCtrl方法之一为每一行设置了文本.

The above assumes you set the text for each row using one of the CListCtrl methods to set text.

这篇关于如何在ListControl MFC中使用多行项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 12:31