高分求解comboBox问题!!!急,急,急!关于改变显示字体!不够再加分!

lilian123 2004-05-03 11:37:58
我在Combbox里用了一个CFontCombo类,可以显示系统所有字体,怎么通过点激comboBox一项选定一种字体,把用pDC->TextOut(0,0,Str); CString str="content:";
输出的str的字体改变,请高手相救,写出原码!!
/////////////////////////////////////////////
*.cpp文件
///////////////////////////////////
#define WM_INITFONTS (WM_USER + 53)
#define GLYPH_WIDTH 15
CFontCombo::CFontCombo()
{
m_clrHilight = GetSysColor (COLOR_HIGHLIGHT);
m_clrNormalText = GetSysColor (COLOR_WINDOWTEXT);
m_clrHilightText = GetSysColor (COLOR_HIGHLIGHTTEXT);
m_clrBkgnd = GetSysColor (COLOR_WINDOW);
m_bInitOver = FALSE;
m_bUseFont = FALSE;
m_ImageList.Create(IDB_GLYPHS,GLYPH_WIDTH,1,RGB(255,0,255));
}

CFontCombo::~CFontCombo()
{
}

BEGIN_MESSAGE_MAP(CFontCombo, CComboBox)
//{{AFX_MSG_MAP(CFontCombo)
ON_WM_CREATE()
ON_WM_DESTROY()
//}}AFX_MSG_MAP
ON_MESSAGE (WM_INITFONTS,OnInitFonts)
END_MESSAGE_MAP()
void CFontCombo::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
DrawFont(lpDrawItemStruct);
}
void CFontCombo::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
}

void CFontCombo::DrawFont(LPDRAWITEMSTRUCT lpDIS)
{
CDC* pDC = CDC::FromHandle(lpDIS->hDC);
CRect rect;
rect.CopyRect(&lpDIS->rcItem);
pDC->SetBkMode( TRANSPARENT );
if (lpDIS->itemState & ODS_SELECTED)
{
pDC->FillSolidRect (rect,m_clrHilight);
pDC->SetTextColor (m_clrHilightText);
}
else
{
pDC->FillSolidRect (rect,m_clrBkgnd);
pDC->SetTextColor (m_clrNormalText);
}

if ((int)(lpDIS->itemID) < 0) {
}
else
{
// Render Bitmaps
if (((TFontObject*)lpDIS->itemData)->m_nType & TRUETYPE_FONT)
m_ImageList.Draw(pDC,1, CPoint(rect.left,rect.top),ILD_TRANSPARENT);

if (((TFontObject*)lpDIS->itemData)->m_nType & PRINTER_FONT)
m_ImageList.Draw(pDC,0, CPoint(rect.left,rect.top),ILD_TRANSPARENT);

rect.left += GLYPH_WIDTH + 2;

CString strText;
GetLBText (lpDIS->itemID,strText);
if(m_bUseFont)
{
CFont newFont;
CFont *pOldFont;

((TFontObject*)lpDIS->itemData)->m_lpELF->elfLogFont.lfHeight = 90;
((TFontObject*)lpDIS->itemData)->m_lpELF->elfLogFont.lfWidth = 0;
newFont.CreatePointFontIndirect(
&((TFontObject*)lpDIS->itemData)->m_lpELF->elfLogFont);
pOldFont = pDC->SelectObject (&newFont);
pDC->DrawText(strText, rect, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
pDC->SelectObject (pOldFont);
newFont.DeleteObject ();
}
else
{
pDC->DrawText(strText, rect, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
}
}
}

void CFontCombo::InitFonts ()
{
// Screen fonts
CDC *pDC = GetDC ();
ResetContent (); //Delete whatever is there
EnumFontFamilies(
pDC->GetSafeHdc(),
NULL,
(FONTENUMPROC)EnumFontFamScreenProc,
(LPARAM)this);//Enumerate
#ifdef _USE_PRINTERFONTS
CPrintDialog dlg(FALSE);
if (AfxGetApp()->GetPrinterDeviceDefaults(&dlg.m_pd))
{
// GetPrinterDC returns a HDC so attach it
HDC hDC;
hDC= dlg.CreatePrinterDC();
ASSERT(hDC != NULL);
EnumFontFamilies(
hDC,
NULL,
(FONTENUMPROC)EnumFontFamPrinterProc,
(LPARAM)this);//Enumerate
}
#endif
m_bInitOver = TRUE;
}

BOOL CALLBACK CFontCombo::EnumFontFamScreenProc(
ENUMLOGFONT *lpELF, // pointer to logical-font data
NEWTEXTMETRIC *lpntm, // pointer to physical-font data
int nFontType, // type of font
LPARAM lpData // address of application-defined data
)
{
if ( !(nFontType & RASTER_FONTTYPE) ) //Add only TTF fellows, If you want you can change it to check for others
{
TFontObject* pFontObject=new TFontObject;
int index = ((CFontCombo *) lpData)->AddString(
/*(const char *)(lpelf->elfFullName)*/
lpELF->elfLogFont.lfFaceName);
pFontObject->m_lpELF = new ENUMLOGFONT;
CopyMemory ((PVOID) (pFontObject->m_lpELF),(CONST VOID *) lpELF,sizeof (ENUMLOGFONT));
pFontObject->m_nType= (nFontType & TRUETYPE_FONTTYPE) ? TRUETYPE_FONT : 0;
((CFontCombo *) lpData)->SetItemData (index,(DWORD) pFontObject);
}
return TRUE;
}

BOOL CALLBACK CFontCombo::EnumFontFamPrinterProc(
ENUMLOGFONT *lpELF, // pointer to logical-font data
NEWTEXTMETRIC *lpntm, // pointer to physical-font data
int nFontType, // type of font
LPARAM lpData // address of application-defined data
)
{
if (!(nFontType & TRUETYPE_FONTTYPE) &&
(nFontType & DEVICE_FONTTYPE) ) //Add only TTF fellows, If you want you can change it to check for others
{
TFontObject* pFontObject=new TFontObject;
int index = ((CFontCombo *) lpData)->AddString(
/*(const char *)(lpelf->elfFullName)*/
lpELF->elfLogFont.lfFaceName);
pFontObject->m_lpELF = new ENUMLOGFONT;
CopyMemory ((PVOID) (pFontObject->m_lpELF),(CONST VOID *) lpELF,sizeof (ENUMLOGFONT));
pFontObject->m_nType=PRINTER_FONT;
((CFontCombo *) lpData)->SetItemData (index,(DWORD) pFontObject);
}
return TRUE;
}

int CFontCombo::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CComboBox::OnCreate(lpCreateStruct) == -1)
return -1;

PostMessage (WM_INITFONTS,0,0);
return 0;
}

long CFontCombo::OnInitFonts (WPARAM, LPARAM)
{
InitFonts ();
return 0L;
}


void CFontCombo::OnDestroy()
{
int nCount;
nCount = GetCount ();
for (int i = 0; i < nCount; i++)
{
TFontObject* pFontObject=(TFontObject*)GetItemData(i);
delete (pFontObject->m_lpELF);//delete the FontObject actually created..
delete (pFontObject);
}
CComboBox::OnDestroy();
}

void CFontCombo::FillFonts ()
{
// SendMessage is needed for immediate execution
SendMessage (WM_INITFONTS,0,0); //Process in one place
}

int CFontCombo::GetSelFont (LOGFONT& lf)
{
int index = GetCurSel ();
if (index == LB_ERR)
return LB_ERR;
LPLOGFONT lpLF = &((TFontObject*)GetItemData(index))->m_lpELF->elfLogFont;
CopyMemory ((PVOID)&lf, (CONST VOID *) lpLF, sizeof (LOGFONT));

return index; //return the index here.. Maybe the user needs it:-)
}

/////////////////////////////////////
*.h 文件
////////////////////////////////////////
#define TRUETYPE_FONT 0x0001
#define PRINTER_FONT 0x0002
#define DEVICE_FONT 0x0004

struct TFontObject
{
ENUMLOGFONT* m_lpELF;
int m_nType;
};

class CFontCombo : public CComboBox
{
// Construction
public:
CFontCombo();

// Attributes
protected:
COLORREF m_clrHilight;
COLORREF m_clrNormalText;
COLORREF m_clrHilightText;
COLORREF m_clrBkgnd;
BOOL m_bInitOver;
BOOL m_bUseFont;

CImageList m_ImageList;

// Operations
public:
void SetHilightColors (COLORREF hilight,COLORREF hilightText)
{
m_clrHilight = hilight;
m_clrHilightText = hilightText;
};
void SetNormalColors (COLORREF clrBkgnd,COLORREF clrText)
{
m_clrNormalText = clrText;
m_clrBkgnd = clrBkgnd;
};
void SetUseFont (BOOL bUseFont)
{
m_bUseFont = bUseFont;
};
BOOL GetUseFont ()
{
return m_bUseFont;
};
static BOOL CALLBACK EnumFontFamScreenProc(ENUMLOGFONT FAR *lpelf, NEWTEXTMETRIC FAR *lpntm, int FontType, LPARAM lParam);
static BOOL CALLBACK EnumFontFamPrinterProc(ENUMLOGFONT FAR *lpelf, NEWTEXTMETRIC FAR *lpntm, int FontType, LPARAM lParam);
void FillFonts ();
int GetSelFont (LOGFONT&);

void DrawDefault (LPDRAWITEMSTRUCT);
void DrawFont(LPDRAWITEMSTRUCT);

void InitFonts ();

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CFontCombo)
public:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
virtual void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct);
//}}AFX_VIRTUAL

// Implementation
public:
virtual ~CFontCombo();

// Generated message map functions
protected:
//{{AFX_MSG(CFontCombo)
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnDestroy();
//}}AFX_MSG
afx_msg long OnInitFonts (WPARAM, LPARAM);
DECLARE_MESSAGE_MAP()
};


...全文
98 7 点赞 打赏 收藏 举报
写回复
7 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
UDX协议 2004-05-04
可以把
int sel = m_combox.GetCursel();
if(sel == -1)
return;
if(m_Font)
m_Font.deleteobject();
TFontObject* pFontObject=(TFontObject* )m_combox.getitemdate(sel);
m_Font.CreatePointFontIndirect(pFontObjectm_lpELF->elfLogFont);
CFont* pOldFont = pDC->SelectObject (&m_Font);
pDC->DrawText(strText, rect, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
pDC->SelectObject (pOldFont);
m_Font.DeleteObject ();
直接放在你的重绘代码里。
  • 打赏
  • 举报
回复
UDX协议 2004-05-04
响应你的父窗口的CCombox的ON_CBN_SELCHANGE这个notify消息。

在你的试图里,加一个CFont的变量m_font;

在响应函授里,
int sel = m_combox.GetCursel();
if(sel == -1)
return;
if(m_Font)
m_Font.deleteobject();
TFontObject* pFontObject=(TFontObject* )m_combox.getitemdate(sel);
m_Font.CreatePointFontIndirect(pFontObjectm_lpELF->elfLogFont);
CFont* pOldFont = pDC->SelectObject (&m_Font);
pDC->DrawText(strText, rect, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
pDC->SelectObject (pOldFont);
m_Font.DeleteObject ();


  • 打赏
  • 举报
回复
lilian123 2004-05-04
自己搞定了,哈哈!
  • 打赏
  • 举报
回复
sam_lai 2004-05-04
elfLogFont.lfHeight
  • 打赏
  • 举报
回复
lilian123 2004-05-04
to wwwlg
是不是要改变显示字体大小和高度?
怎么改?请给出原码,谢谢了!
  • 打赏
  • 举报
回复
lilian123 2004-05-04
现在可以显示了,但是字只有一条线一样细,为什么呀?
我在DrawText 上加
CRect rect(10,10,500,500)
CString strText="Content:"
pDC->DrawText( )
我把if(m_Font)注掉了
m_Font.CreatePointFontIndirect(pFontObjectm_lpELF->elfLogFont);
我知道怎么改了
  • 打赏
  • 举报
回复
lilian123 2004-05-04
if(m_Font)

error C2451: conditional expression of type 'class CFont' is illegal
Ambiguous user-defined-conversion
m_Font.CreatePointFontIndirect(pFontObjectm_lpELF->elfLogFont);
pFontObjectm_lpELF' : undeclared identifier
error C2227: left of '->elfLogFont' must point to class/struct/union
我的ComboBox 添加变量的基类选的是CFontCombo m_combo
  • 打赏
  • 举报
回复
相关推荐
发帖
界面
加入

1.5w+

社区成员

VC/MFC 界面
申请成为版主
帖子事件
创建了帖子
2004-05-03 11:37
社区公告
暂无公告