使用MFC将CListBox中的各个项设置为粗体

时间:2017-10-09 15:44:53

标签: mfc clistbox

我偶然发现了这篇文章:

http://asg.unige.ch/Past/fuentes/Mfc/HowTo_44.html

所以,我在我的项目中重现了这个课程:

// FontStyleListBox.cpp : implementation file
//

#include "stdafx.h"
#include "Meeting Schedule Assistant.h"
#include "FontStyleListBox.h"


// CFontStyleListBox

IMPLEMENT_DYNAMIC(CFontStyleListBox, CListBox)

CFontStyleListBox::CFontStyleListBox()
{

}

CFontStyleListBox::~CFontStyleListBox()
{
}


BEGIN_MESSAGE_MAP(CFontStyleListBox, CListBox)
    ON_WM_DRAWITEM_REFLECT()
END_MESSAGE_MAP()



// CFontStyleListBox message handlers




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

    // Draw the colored rectangle portion
    rect.CopyRect(&lpDrawItemStruct->rcItem);

    pDC->SetBkMode(TRANSPARENT);

    if (lpDrawItemStruct->itemState & ODS_SELECTED)
    {
        pDC->FillSolidRect(rect, GetSysColor(COLOR_HIGHLIGHT));
        pDC->SetTextColor(GetSysColor(COLOR_HIGHLIGHTTEXT));
    }
    else
    {
        pDC->FillSolidRect(rect, GetSysColor(COLOR_WINDOW));
        pDC->SetTextColor(GetSysColor(COLOR_WINDOWTEXT));
    }

    if ((int)(lpDrawItemStruct->itemID) >= 0) // Valid ID
    {
        CString sText;
        int     nIndex;

        CFont newFont;
        CFont *pOldFont;

        nIndex = lpDrawItemStruct->itemID;
        GetText(nIndex, sText);

        FONTSTYLE fontStyle = (FONTSTYLE)GetItemData(nIndex);

        // To avoid unnecessary processing
        if (fontStyle == NORMAL) {
            pDC->DrawText(sText, rect, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
            return;
        }

        LOGFONT logFont;
        CFont *pFont = GetFont();
        pFont->GetLogFont(&logFont);

        switch (fontStyle)
        {
            //case NORMAL: logFont.lfWeight = FW_NORMAL;
            //           break;
        case BOLD:   logFont.lfWeight = FW_BOLD;
            break;
        case ITALIC: logFont.lfItalic = TRUE;
            break;
        }

        newFont.CreatePointFontIndirect(&logFont);
        pOldFont = pDC->SelectObject(&newFont);
        pDC->DrawText(sText, rect, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
        pDC->SelectObject(pOldFont);
        newFont.DeleteObject();
    }
}


int CFontStyleListBox::GetFontStyle(int nIndex)
{
    return (FONTSTYLE)GetItemData(nIndex);
}


void CFontStyleListBox::SetFontStyle(int nIndex, FONTSTYLE fontStyle)
{
    SetItemData(nIndex, (DWORD)fontStyle);
    Invalidate();
}

然后我在我的应用程序中使用它。我为ownerdraw等正确设置了属性,但结果如下:

ListBox Output

粗体条目是最后一个。为什么渲染不正确?

我如何解决这个问题和/或是否有更新的方法来完成这项工作?

1 个答案:

答案 0 :(得分:3)

您只需mydf.merge(mydf.groupby('group1').apply(lambda x: len(set(x['value1'].values).intersection(valueslist))).reset_index().rename(columns={0: 'count'}), how='inner', on='group1') group1 value1 count 0 0 P 1 1 0 F 1 2 1 100 1 3 1 10 1 4 2 0 0 使用即可。使用CFont::CreateFontIndirect会导致从字体点转换为物理点。你不需要那个。

也只创建一次字体。您可以在DrawItem中按需创建它。只需在子类CListBox中创建一个成员......