您的位置:首页 > 其它

MFC设置字体及颜色

2008-11-14 23:40 246 查看
设置字体
函数原型:
BOOL CreateFont( int nHeight,
int nWidth, int nEscapement, int nOrientation, int nWeight, BYTE bItalic, BYTE
bUnderline, BYTE cStrikeOut, BYTE nCharSet, BYTE nOutPrecision, BYTE
nClipPrecision, BYTE nQuality, BYTE nPitchAndFamily, LPCTSTR lpszFacename );

参数说明:
nHeight :字体高度. 三中情况
1、>0:字体的高度值(设备坐标);2、=0:字体采用缺省值. 3、<0:此值的绝对值为高度.
nWidth
:字体宽度.
nEscapement :文本行的倾斜度.
nOrientation
:字符基线的倾斜度.
nWeight :字体的粗细.如下:

.FW_DONTCARE
.FW_THIN
.FW_EXTRALIGHT

.....
bItalic :字体是否为斜体
bUnderline
:字体是否带下划线
cStrikeOut :字体是否带删除线
nCharSet
:字体的字符集
.ANSI_CHARSET
.DEFAULT_CHARSET

.SYMBOL_CHARSET.....
nOutPrecision :字符的输出精度

nClipPrecision :字符裁剪的精度
nQuality :字符的输出质量

nPitchAndFamily :字符间距和字体族(低位说明间距,高位说明字符族)
lpszFacename
:字体名称
[程序实现]
假设你已有了名为My的对话框工程.并有一个ID=IDC_EDIT1的Edit控件.

class CMyDlg : public CDialog
{
public:

CFont *m_Font;//最好用指针,我用对象调用好像不起作用,不清楚原因!
m_Font = new
CFont;//结束时记得要delete m_Font;
........
};
BOOL
CTMyDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// TODO: Add extra initialization here
//CFont
m_Font;

m_Font->CreateFont(-11,0,0,0,100,FALSE,FALSE,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,FF_SWISS,"Arial");

CEdit *m_Edit=(CEdit *)GetDlgItem(IDC_EDIT1);

m_Edit->SetFont(m_Font,FALSE);
return TRUE; // return TRUE
unless you set the focus to a control
}

小小说明:在OnInitDialog()中的//CFont m_Font;前的"//"号去掉,将类声明中的CFont
m_Font;去掉会是什么结果?请自己试试.
改变Edit字体颜色!
HBRUSH
CButtonDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{

HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

//
TODO: Change any attributes of the DC here
if(nCtlColor ==
CTLCOLOR_EDIT)
{
if(pWnd->GetDlgCtrlID()==
IDC_EDIT1)
{

pDC->SetTextColor(RGB(255,255,0));

pDC->SetBkColor(RGB(251, 247, 200));

pDC->SetBkMode(TRANSPARENT);
return (HBRUSH)
m_brush.GetSafeHandle();
}
}

// TODO: Return a different brush if the default is not
desired
return hbr;
}

CBrush m_brushedit;
m_brushedit.CreateSolidBrush
(RGB ( 255, 255, 0 ) );


在Dlg::OnCtlColor函数中加入:
if(nCtlColor ==
CTLCOLOR_EDIT){
pDC->SetTextColor(RGB(255,0,0));
//文字颜色
pDC->SetBkColor(RGB(255,255,200)); //文字背景颜色

return (HBRUSH)m_brushedit.GetSafeHandle() ;
//edit框的颜色
}

改变对话框背景

CBitmap m_BkGndBmp;
m_BkGndBmp.LoadBitmap(IDB_BITMAP1);

BOOL CButtonDlg::OnEraseBkgnd(CDC* pDC)
{
CRect
rcClient;
GetClientRect(&rcClient);
BITMAP
bm;
m_BkGndBmp.GetBitmap(&bm);
CDC
memDC;
memDC.CreateCompatibleDC(pDC);
CBitmap *pOldBmp =
memDC.SelectObject(&m_BkGndBmp);
pDC->StretchBlt(0,0,rcClient.Width(),rcClient.Height(),&memDC,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);
memDC.SelectObject(pOldBmp);
memDC.DeleteDC();
return
TRUE;

// return CDialog::OnEraseBkgnd(pDC);
}

在一个mfc应用程序中,要改变控件的背景色可通过重载onctlcolor()函数来实现。方法是在该函数中设置所需颜色后再返回一个画刷句柄便可重绘控件背景色。onctlcolor()函数对于控件背景色的处理是通过捕捉相应的控件消息来实现的。常用的此类消息有:

ctlcolor_dlg 对话框

ctlcolor_edit 编辑框

ctlcolor_listbox 列表框

ctlcolor_msgbox 消息框

ctlcolor_scrollbar 滑动条

ctlcolor_static 静态文本框、矩形等。

以下示例代码说明如何更改以上控件的背景色:

afx_msg hbrush onctlcolor(cdc* pdc, cwnd* pwnd, uint nctlcolor); //重载方法原形声明
*.h
afx_msg BOOL OnEraseBkgnd(CDC*
pDC);//对话框背景
......
BEGIN_MESSAGE_MAP(CButtonDlg, CDialog)

ON_WM_CTLCOLOR() //建立消息映射 *.cpp
ON_WM_ERASEBKGND()
//设置对话框图片背景


END_MESSAGE_MAP()

在程序中可以直接设置如下字体:黑体、宋体、仿宋—GB2312、楷体—GB2312、隶书、幼圆
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: