⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fontbutton.h

📁 一个使用WTL的字体按钮类
💻 H
字号:
// FontButton.h: interface for the CFontButton class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_FONTBUTTON_H__1F0B61B6_7CCD_483F_8D08_06EA058C4EA1__INCLUDED_)
#define AFX_FONTBUTTON_H__1F0B61B6_7CCD_483F_8D08_06EA058C4EA1__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000


#pragma once

#ifndef __cplusplus
#error WTL requires C++ compilation (use a .cpp suffix)
#endif

#ifndef __ATLAPP_H__
#error This control requires atlapp.h to be included first
#endif

#ifndef __ATLCTRLS_H__
#error This control requires atlctrls.h to be included first
#endif


template< class T, class TBase = CButton, class TWinTraits = CControlWinTraits >
class ATL_NO_VTABLE CFontButtonImpl: 
public CWindowImpl< T, TBase, TWinTraits >,
public COwnerDraw< T >
{
public:
	typedef CFontButtonImpl< T, TBase, TWinTraits > thisClass;
	
	DECLARE_WND_SUPERCLASS(NULL, TBase::GetWndClassName())
	bool m_bUse3D;
	CString m_strFontStyle;

	BOOL SubclassWindow(HWND hWnd)
	{
		BOOL bRet = CWindowImpl< T, TBase, TWinTraits >::SubclassWindow(hWnd);
		if( bRet ) _Init();
		return bRet;
	}
	void Draw(CDCHandle& pDC, const CRect& rect, UINT state)
	{
		CString text;// GetWindowText(text); 
		// Draw text
		TCHAR szText[128] = { 0 };
		
		GetWindowText(szText, (sizeof(szText)/sizeof(TCHAR))-1);
		
		
		text=szText;
		//MessageBox(szText);
		int l = text.GetLength();
		CRect rectClient = rect;     // get font from control
		HFONT pFont = GetFont();
		// ensure we have a valid height and width and select the font
		LOGFONT logfont; 
	    //pFont->GetFont(sizeof(LOGFONT),&logfont);
		//GetLogFont(&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; 
		tryfont.CreateFontIndirect(&logfont);
		HFONT pFontOld = pDC.SelectFont(tryfont);
		// get the control size and adjust font width & height accordingly
		if (m_bUse3D) rectClient.DeflateRect(3,3);
		//CSize textSizeClient = pDC.GetTextExtent(text,l);
		CSize textSizeClient;
		pDC.GetTextExtent(text,l,&textSizeClient);

		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.CreateFont(logfont.lfHeight,0,0,0,1000,0,0,0,
			DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,
			CLIP_DEFAULT_PRECIS,
			DEFAULT_QUALITY,
			DEFAULT_PITCH|FF_DONTCARE,
			m_strFontStyle);     
		
		//font.CreateFontIndirect(&logfont);
		pDC.SelectFont(font);     
		//textSizeClient = pDC.GetTextExtent(text,l);
		pDC.GetTextExtent(text,l,&textSizeClient);
		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 =COLOR_BTNTEXT; //::GetSysColor((state & ODS_FOCUS) ? COLOR_GRAYTEXT: COLOR_BTNTEXT);
		//COLORREF textcol =RGB(111,111,0);
		//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.SelectFont(pFontOld);
	}
	void SetFontStyle(CString str="system")
	{
		m_strFontStyle=str;
		
	}
	void _Init()
	{
		// Ajust button styles before we make it ownerdrawn.
		// The ownerdrawn style would otherwise remove some of the
		// existing style customizations.
		m_bUse3D=TRUE;
		
		SetFontStyle("system");
		DWORD dwStyle = GetStyle();
		
		// Finally make it owner-drawn
		ModifyStyle(0x0F, BS_OWNERDRAW);
		
	}


	BEGIN_MSG_MAP(thisClass)
		MESSAGE_HANDLER(WM_CREATE, OnCreate)
		CHAIN_MSG_MAP_ALT( COwnerDraw< T >, 1 )
		//DEFAULT_REFLECTION_HANDLER()
	END_MSG_MAP()

	LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	{      
		
		LRESULT lRes = DefWindowProc();
		_Init();
		return lRes;
	}
	void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
	{
		//CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);  
		CDCHandle pDC = lpDrawItemStruct->hDC;
		CRect rectClient = lpDrawItemStruct->rcItem;
		//Draw(pDC,rectClient, lpDrawItemStruct->itemState);
		Draw(pDC,rectClient, 0);
		EnableWindow(false);
	}
};

class CFontButtonCtrl : public CFontButtonImpl<CFontButtonCtrl>
{
public:
	DECLARE_WND_SUPERCLASS(_T("WTL_FontButton"), GetWndClassName())
};
#endif // !defined(AFX_FONTBUTTON_H__1F0B61B6_7CCD_483F_8D08_06EA058C4EA1__INCLUDED_)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -