工控编程吧
标题:
上位机VC MFC自绘实现3D文本按钮
[打印本页]
作者:
qq263946146
时间:
2017-11-11 21:47
标题:
上位机VC MFC自绘实现3D文本按钮
(, 下载次数: 9)
上传
点击文件名下载附件
例程通过从CButton派生,通过自绘方式实现3维文本按钮效果;
实现也非常简单
可以自己从CButton派生类C3DTextBtn
依次编写函数代码
DrawItem,
OnEraseBkgnd
PreSubclassWindow
void C3DTextBtn::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) {
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
ASSERT_VALID(pDC);
CRect rectClient = lpDrawItemStruct->rcItem;
Draw(pDC,rectClient, lpDrawItemStruct->itemState);
}
void C3DTextBtn::Draw(CDC* pDC, const CRect& rect, UINT state) {
CString text; GetWindowText(text);
int l = text.GetLength();
CRect rectClient = rect;
// get font from control
CFont* pFont = GetFont();
// ensure we have a valid height and width and select the font
LOGFONT logfont;
pFont->GetObject(sizeof(LOGFONT),&logfont);
if (logfont.lfHeight == 0) logfont.lfHeight = 20;
logfont.lfWidth = 0; // 0 so it will be calculated
logfont.lfWeight = 1000;
logfont.lfEscapement = logfont.lfOrientation = 0;
CFont tryfont; VERIFY(tryfont.CreateFontIndirect(&logfont));
CFont* pFontOld = pDC->SelectObject(&tryfont);
// get the control size and adjust font width & height accordingly
if (m_bUse3D) rectClient.DeflateRect(3,3);
CSize textSizeClient = pDC->GetTextExtent(text,l);
if (rectClient.Width()*textSizeClient.cy >
rectClient.Height()*textSizeClient.cx) {
logfont.lfHeight = ::MulDiv(logfont.
lfHeight,rectClient.Height(),textSizeClient.cy);
} else {
logfont.lfHeight = ::MulDiv(logfont.
lfHeight,rectClient.Width(),textSizeClient.cx);
}
logfont.lfHeight--; // fudge factor
if (m_bUse3D) rectClient.InflateRect(3,3);
// create adjusted font and select
CFont font; font.CreateFontIndirect(&logfont);
pDC->SelectObject(&font);
textSizeClient = pDC->GetTextExtent(text,l);
int minx = rectClient.left+(rectClient.Width()-textSizeClient.cx)/2;
int miny = rectClient.top+(rectClient.Height()-textSizeClient.cy)/2;
int oldBkMode = pDC->SetBkMode(TRANSPARENT);
COLORREF textcol = ::GetSysColor((state & ODS_FOCUS) ? COLOR_GRAYTEXT
: COLOR_BTNTEXT);
COLORREF oldTextColor = pDC->SetTextColor(textcol);
int cx = minx;
int cy = miny;
if (m_bUse3D) {
int s = (state & ODS_SELECTED) ? -1 : +1;
cx += 3; cy += 3;
// draw 3D highlights
pDC->SetTextColor(::GetSysColor(COLOR_3DDKSHADOW));
pDC->TextOut(cx-s*2,cy+s*2,text);
pDC->TextOut(cx+s*2,cy-s*2,text);
pDC->TextOut(cx+s*2,cy+s*2,text);
pDC->SetTextColor(::GetSysColor(COLOR_3DHILIGHT));
pDC->TextOut(cx+s*1,cy-s*2,text);
pDC->TextOut(cx-s*2,cy+s*1,text);
pDC->TextOut(cx-s*2,cy-s*2,text);
pDC->SetTextColor(::GetSysColor(COLOR_3DSHADOW));
pDC->TextOut(cx-s*1,cy+s*1,text);
pDC->TextOut(cx+s*1,cy-s*1,text);
pDC->TextOut(cx+s*1,cy+s*1,text);
pDC->SetTextColor(::GetSysColor(COLOR_3DLIGHT));
pDC->TextOut(cx,cy-s*1,text);
pDC->TextOut(cx-s*1,cy,text);
pDC->TextOut(cx-s*1,cy-s*1,text);
pDC->SetTextColor(textcol);
}
// draw the text
pDC->TextOut(cx,cy,text);
// restore DC
pDC->SetTextColor(oldTextColor);
pDC->SetBkMode(oldBkMode);
pDC->SelectObject(pFontOld);
}
BOOL C3DTextBtn::OnEraseBkgnd(CDC*) {
return true;
}
复制代码
有了自己编写的类后,就是关联与使用
#include "3DTextBtn.h"
C3DTextBtn m_Btn;
实现很简单,入门的朋友要项目源代码可直接下载附件,内有编译好的程序与源代码
(, 下载次数: 0)
上传
点击文件名下载附件
[weixinlianxi]1[/weixinlianxi]
欢迎光临 工控编程吧 (https://www.gkbc8.com/)
Powered by Discuz! X3.4