使用 MFC 将 CListBox 中的单个项目设置为粗体
Setting individual items in a CListBox as bold with MFC
我偶然发现了这篇文章:
http://asg.unige.ch/Past/fuentes/Mfc/HowTo_44.html
所以,我在我的项目中复制了class:
// 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 等正确设置了属性,但结果如下:
粗体为最后一项。为什么渲染不正确?
我该如何解决这个问题和/或是否有更新的方法来完成这个问题?
你只需要CFont::CreateFontIndirect
使用。使用 CFont::CreatePointFontIndirect
会导致从字体点到物理点的转换。你不需要那个。
也只创建一次字体。您可以在 DrawItem 中按需创建它。只需在子类 CListBox 中创建一个成员...
我偶然发现了这篇文章:
http://asg.unige.ch/Past/fuentes/Mfc/HowTo_44.html
所以,我在我的项目中复制了class:
// 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 等正确设置了属性,但结果如下:
粗体为最后一项。为什么渲染不正确?
我该如何解决这个问题和/或是否有更新的方法来完成这个问题?
你只需要CFont::CreateFontIndirect
使用。使用 CFont::CreatePointFontIndirect
会导致从字体点到物理点的转换。你不需要那个。
也只创建一次字体。您可以在 DrawItem 中按需创建它。只需在子类 CListBox 中创建一个成员...