?? 3dtextbutton.cpp
字號:
// 3DTextButton.cpp : implementation file
//
#include "stdafx.h"
#include "3DTextButton.h"
#include "yilufa.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// C3DTextButton
C3DTextButton::C3DTextButton()
{
m_ColorR=255;
m_ColorG=0;
m_ColorB=255;
}
C3DTextButton::~C3DTextButton()
{
}
BEGIN_MESSAGE_MAP(C3DTextButton, CButton)
//{{AFX_MSG_MAP(C3DTextButton)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// C3DTextButton message handlers
void C3DTextButton::Draw(CDC *pDC, const CRect &rect, UINT state)
{
//數據定義區
CFont font;
CFont tryfont;
CFont* pFont=GetFont();
CFont* pFontOld;
//CBitmap bitmap,*pOldBitmap;
CSize textSizeClient;
CRect rectClient;
//CDC mdc;
LOGFONT logfont;
COLORREF textCol;
COLORREF oldTextColor;
int minx;
int miny;
int oldBkMode;
int cx;
int cy;
int s;
int l;
///////////////////////////////////
///////////
m_pDC=pDC;
m_Rect=▭
m_State=state;
//GetWindowText(text);
//mdc.CreateCompatibleDC(pDC);
//bitmap.LoadBitmap(IDB_BTN);
//mdc.SelectObject(&bitmap);
l=text.GetLength();
rectClient=rect;
///////////////////////////////////
//繪出按鈕背景
pDC->FillSolidRect(rect.left,rect.top,rect.Width(),rect.Height(),RGB(0,0,0));
pDC->FillSolidRect(rect.left+1,rect.top+1,rect.Width()-3,rect.Height()-2,RGB(100,200,120));
//::StretchBlt(pDC->GetSafeHdc(),rect.left,rect.top,rect.Width(),rect.Height(),mdc.GetSafeHdc(),0,0,80,20,SRCCOPY);
//確定所選字體的有效高度和寬度
pFont->GetObject(sizeof(LOGFONT),&logfont);
if(logfont.lfHeight==0) logfont.lfHeight=20;
logfont.lfWidth=0;
logfont.lfWeight=1000;
logfont.lfEscapement=logfont.lfOrientation=0;
VERIFY(tryfont.CreateFontIndirect(&logfont));
pFontOld=pDC->SelectObject(&tryfont);
//根據控件大小調整字體高度
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,rect.Width(),textSizeClient.cx);
}
//創建并協調字體
font.CreateFontIndirect(&logfont);
pDC->SelectObject(&font);
textSizeClient=pDC->GetTextExtent(text,l);
//確定文本與控件的距離
minx=rectClient.left+(rectClient.Width()-textSizeClient.cx)/2;
miny=rectClient.top+(rectClient.Height()-textSizeClient.cy)/2;
oldBkMode=pDC->SetBkMode(TRANSPARENT);
textCol=::GetSysColor(COLOR_BTNTEXT);
oldTextColor=pDC->SetTextColor(textCol);
cx=minx;
cy=miny;
s=(state&ODS_SELECTED?-1:+1);
cx+=2;
cy+=2;
//實現3D效果
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*2,cy+s*2,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);
//輸出標題
pDC->SetTextColor(RGB(200+s*55,55-s*55,0));
pDC->TextOut(cx-s,cy-s,text);
//恢復設備描述表
pDC->SelectObject(pFontOld);
pDC->SetTextColor(oldTextColor);
pDC->SetBkMode(oldBkMode);
////////
//釋放內存
font.DeleteObject();
tryfont.DeleteObject();
pFont->DeleteObject();
pFontOld->DeleteObject();
//bitmap.DeleteObject();
//pOldBitmap->DeleteObject();
DeleteObject(&textSizeClient);
DeleteObject(&rectClient);
//mdc.DeleteDC();
DeleteObject(&logfont);
DeleteObject(&textCol);
DeleteObject(&oldTextColor);
//
DeleteObject(&minx);
DeleteObject(&miny);
DeleteObject(&oldBkMode);
DeleteObject(&cx);
DeleteObject(&cy);
DeleteObject(&s);
DeleteObject(&l);
//
}
void C3DTextButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC* pDC=CDC::FromHandle(lpDrawItemStruct->hDC);
ASSERT_VALID(pDC);
CRect rectClient=lpDrawItemStruct->rcItem;
Draw(pDC,rectClient,lpDrawItemStruct->itemState);
}
void C3DTextButton::DrawAgin()
{
m_ColorR--;
m_ColorG++;
m_ColorB-=2;
if(m_ColorR==0) m_ColorR=255;
if(m_ColorG==255) m_ColorG=0;
if(m_ColorB==0) m_ColorB=255;
CPaintDC dc(this);
CRect rc;
GetClientRect(&rc);
Draw(&dc,&rc,0);
Invalidate(true);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -