📄 guibutton.cpp
字号:
//-----------------------------------------------------------------------//
// This is a part of the GuiLib MFC Extention. //
// Autor : Francisco Campos //
// (C) 2002 Francisco Campos <www.beyondata.com> All rights reserved //
// This code is provided "as is", with absolutely no warranty expressed //
// or implied. Any use is at your own risk. //
// You must obtain the author's consent before you can include this code //
// in a software library. //
// If the source code in this file is used in any application //
// then acknowledgement must be made to the author of this program //
//fco_campos@tutopia.com //
/// version : 1.1 //
// Modified by : Francisco Campos //
//-----------------------------------------------------------------------//
#include "stdafx.h"
#include "..\header\GuiButton.h"
#include "..\header\GuiDrawLayer.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define OVER 0
#define FOCUS 1
#define NORMAL 2
#define DISABLED 3
/////////////////////////////////////////////////////////////////////////////
// CGuiButton
CGuiButton::CGuiButton()
{
m_iAling=ALING_LEFT;
m_iStyle=STYLE_SEMIFLAT;
m_lpMsg="";
m_iSelected=0;
m_viewImage=FALSE;
m_ViewText=FALSE;
m_iTypeButton =TYPE_NORMAL; //Normal by default
m_iMenuActivo=FALSE;
m_clrface=GuiDrawLayer::GetRGBColorFace();
m_ReposWindow=FALSE;
m_bIsDark=FALSE;
}
CGuiButton::~CGuiButton()
{
}
BEGIN_MESSAGE_MAP(CGuiButton, CButton)
//{{AFX_MSG_MAP(CGuiButton)
ON_WM_CREATE()
ON_WM_ERASEBKGND()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONUP()
ON_WM_LBUTTONDOWN()
ON_WM_TIMER()
ON_WM_SYSCOLORCHANGE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CGuiButton message handlers
//*****************************************************************************
void CGuiButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your code to draw the specified item
ICONINFO bm;
CSize sizeImag;
CBrush cb;
CSize SizeCad=CSize(0,0);
CSize pt;
CPoint m_point;
int m_Index=0;
ZeroMemory(&bm, sizeof(ICONINFO));
CDC* pDC= CDC::FromHandle(lpDrawItemStruct->hDC);
CRect m_rect=&lpDrawItemStruct->rcItem;
UINT m_State=lpDrawItemStruct->itemState;
//---------------------------------------------------------------------
//Se obtiene la cadena del boton si tiene
//---------------------------------------------------------------------
CString m_caption;
GetWindowText(m_caption);
//---------------------------------------------------------------------
// Se dibuja el fondo segun sea el estilo
// si es TooBarXP
// si no si es un boton Normal
//---------------------------------------------------------------------
if (m_State & ODS_SELECTED) //el boton esta presionado
{
if (STYLEXP == m_iStyle)
cb.CreateSolidBrush(m_clrface);
}
else if(m_iSelected == 1) //el mouse esta sobre el boton
{
if (STYLEXP == m_iStyle)
cb.CreateSolidBrush(GuiDrawLayer::GetRGBColorBTNHigh());
}
else //el boton esta normal
{
if (STYLEXP == m_iStyle)
cb.CreateSolidBrush(GuiDrawLayer::GetRGBColorBTNHigh());
}
if (STYLEXP == m_iStyle)
{
CRect rcTemp=m_rect;
rcTemp.DeflateRect(2,2);
rcTemp.right-=2;
pDC->FillRect(&rcTemp,&cb);
}
else
{
cb.CreateSolidBrush(GuiDrawLayer::GetRGBColorFace());
pDC->FillRect(&m_rect,&cb);
}
//********************************************************************
int nMode = pDC->SetBkMode(TRANSPARENT);
if (m_caption.GetLength() > 0)
SizeCad=pDC->GetTextExtent(m_caption);
//-------------------------------------------------------------------
//si el boton esta seleccionado, se mueve el rectangulo para dar la
//impresion de presion
//-------------------------------------------------------------------
if (m_State & ODS_SELECTED)
m_rect.bottom+=2;
//-------------------------------------------------------------------
//El boton tiene una imagen
//-------------------------------------------------------------------
if (m_viewImage == TRUE)
{
if (m_State & ODS_SELECTED)
m_Index=2;
else if (m_iSelected== 1)
m_Index=1;
else
m_Index=0;
if (m_hicon[m_Index]!= NULL)
{
::GetIconInfo(m_hicon[m_Index], &bm);
sizeImag=CSize((BYTE)(bm.xHotspot * 2),(BYTE)(bm.yHotspot * 2));
}
else m_Index=-1;
}
//---------------------------------------------------------------------
//el boton tiene una imagen pero no texto
//---------------------------------------------------------------------
if (m_viewImage == TRUE && m_ViewText== FALSE)
{
if (m_Index != -1)
{
int calculodify;
m_rect.right=m_rect.left+sizeImag.cx+5;
calculodify=m_rect.Height()-(sizeImag.cy);
calculodify/=2;
m_point=CPoint(2,calculodify);
if(m_bIsDark && m_iSelected == 1 && !(m_State & ODS_DISABLED))
{
CPoint p(m_point.x+1,m_point.y+1);
pDC->DrawState(p,sizeImag,m_hicon[m_Index],DSS_MONO,CBrush (GuiDrawLayer::GetRGBColorShadow()));
m_point.x-=1; m_point.y-=1;
}
pDC->DrawState (m_point, sizeImag,m_hicon[m_Index],(m_State==ODS_DISABLED?DSS_DISABLED:DSS_NORMAL),CBrush (GuiDrawLayer::GetRGBColorFace()));
}
}
//-----------------------------------------------------------------------
//El boton tiene una imagen y ademas texto
//-----------------------------------------------------------------------
else if (m_viewImage == TRUE && m_ViewText== TRUE)
{
int calculodifx;
int calculodify;
if (m_Index != -1)
{
CRect rectletra;
calculodifx=m_rect.Width()-(sizeImag.cx);
calculodifx/=2;
calculodify=m_rect.Height()-(sizeImag.cy);
calculodify/=2;
if (m_iAling ==ALING_RIGTH)
{
m_point=CPoint(4,calculodify);
if(m_bIsDark && m_iSelected == 1 && !(m_State & ODS_DISABLED))
{
CPoint p(m_point.x+1,m_point.y+1);
pDC->DrawState(p,sizeImag,m_hicon[m_Index],DSS_MONO,CBrush (GuiDrawLayer::GetRGBColorShadow()));
m_point.x-=1; m_point.y-=1;
}
pDC->DrawState (m_point, sizeImag,m_hicon[m_Index],
(m_State==ODS_DISABLED?DSS_DISABLED:DSS_NORMAL),(CBrush*)NULL);
rectletra=m_rect;
rectletra.left+=sizeImag.cx+8;
pt=CSize(rectletra.top,rectletra.left);
if (m_State & ODS_DISABLED)
pDC->DrawState(pt, SizeCad, m_caption, DSS_DISABLED, TRUE, 0, (CBrush*)NULL);
else
pDC->DrawText(m_caption,rectletra,DT_SINGLELINE|DT_LEFT|DT_VCENTER);
}
else
{
rectletra=m_rect;
rectletra.left+=4;
if(m_bIsDark && m_iSelected == 1 && !(m_State & ODS_DISABLED))
{
m_point=CPoint(0,0);
CPoint p=CPoint(m_point.x+1,m_point.y+1);
pDC->DrawState(p,sizeImag,m_hicon[m_Index],DSS_MONO,CBrush (GuiDrawLayer::GetRGBColorShadow()));
m_point.x-=1; m_point.y-=1;
}
pt=CSize(rectletra.top,rectletra.left);
if (m_State & ODS_DISABLED)
pDC->DrawState(pt, SizeCad, m_caption, DSS_DISABLED, TRUE, 0, (CBrush*)NULL);
else
pDC->DrawText(m_caption,rectletra,DT_SINGLELINE|DT_LEFT|DT_VCENTER);
m_point=CPoint((rectletra.right-sizeImag.cx)-4,calculodify);
pDC->DrawState (m_point, sizeImag,m_hicon[m_Index],(m_State==ODS_DISABLED?DSS_DISABLED:DSS_NORMAL),(CBrush*)NULL);
}
}
}
//-----------------------------------------------------------------------
//Si el texto es visible
//-----------------------------------------------------------------------
else if (m_ViewText== TRUE)
{
CSize sz=pDC->GetTextExtent(m_caption);
int ntempcx=m_rect.Width()-sz.cx;
int ntempcy=m_rect.Height()-sz.cy;
pt=CSize(m_rect.left+(ntempcx/2),m_rect.top+(ntempcy/2));
if (m_State & ODS_DISABLED)
pDC->DrawState(pt, SizeCad, m_caption, DSS_DISABLED, TRUE, 0, (HBRUSH)NULL);
else
pDC->DrawText(m_caption,m_rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
}
m_rect=&lpDrawItemStruct->rcItem;
//-----------------------------------------------------------------
//se inicia el dibujo de bordes
//1) si es semiflat
//2) si es Flat
//3) si es estilo ToolBarXp
//4) si es estilo 3d
//-----------------------------------------------------------------
if (m_State & ODS_SELECTED)
{
if (STYLE_SEMIFLAT== m_iStyle)
pDC->Draw3dRect(m_rect,GuiDrawLayer::GetRGBColorShadow(),GuiDrawLayer::GetRGBColorBTNHigh());
else if (STYLE_FLAT==m_iStyle)
pDC->Draw3dRect(m_rect, GuiDrawLayer::GetRGBColorShadow(),GuiDrawLayer::GetRGBColorBTNHigh());
else if(STYLEXP == m_iStyle)
DrawBottonXP(pDC,m_rect,NORMAL);
else //si es 3d
{
pDC->Draw3dRect(m_rect, GuiDrawLayer::GetRGBColorShadow(),GuiDrawLayer::GetRGBColorBTNHigh());
CPen cp(PS_SOLID,1,GuiDrawLayer::GetRGBColorShadow());
CPen* cpa=pDC->SelectObject(&cp);
pDC->MoveTo(m_rect.left+1,m_rect.top+1);
pDC->LineTo(m_rect.right-1,m_rect.top+1);
pDC->SelectObject(cpa);
}
}
//-----------------------------------------------------------------------
//el boton esta presionado
//-----------------------------------------------------------------------
else if (m_iSelected == 1) //el mouse esta sobre el boton
{
if (STYLE_SEMIFLAT== m_iStyle)
pDC->Draw3dRect(m_rect, GuiDrawLayer::GetRGBColorBTNHigh(),GuiDrawLayer::GetRGBColorShadow());
else if (STYLE_FLAT==m_iStyle)
pDC->Draw3dRect(m_rect,GuiDrawLayer::GetRGBColorBTNHigh(), GuiDrawLayer::GetRGBColorShadow());
else if(STYLEXP == m_iStyle)
DrawBottonXP(pDC,m_rect,OVER);
else //si es 3d
{
pDC->Draw3dRect(m_rect,GuiDrawLayer::GetRGBColorBTNHigh(), GuiDrawLayer::GetRGBColorShadow());
CPen cp(PS_SOLID,1,GuiDrawLayer::GetRGBColorShadow());
CPen* cpa=pDC->SelectObject(&cp);
pDC->MoveTo(m_rect.left+1,m_rect.bottom-2);
pDC->LineTo(m_rect.right-2,m_rect.bottom-2);
pDC->MoveTo(m_rect.right-2,m_rect.top+1);
pDC->LineTo(m_rect.right-2,m_rect.bottom-2);
pDC->SelectObject(cpa);
}
}else
{
//-----------------------------------------------------------------
//el boton esta en estado normal, es decir el mouse ni lo presiona
//ni esta sobre el.
//-----------------------------------------------------------------
if (STYLE_3D==m_iStyle)
{
pDC->Draw3dRect(m_rect,GuiDrawLayer::GetRGBColorBTNHigh(), GuiDrawLayer::GetRGBColorShadow());
CPen cp(PS_SOLID,1,GuiDrawLayer::GetRGBColorShadow());
CPen* cpa=pDC->SelectObject(&cp);
pDC->MoveTo(m_rect.left+1,m_rect.bottom-2);
pDC->LineTo(m_rect.right-2,m_rect.bottom-2);
pDC->MoveTo(m_rect.right-2,m_rect.top+1);
pDC->LineTo(m_rect.right-2,m_rect.bottom-2);
pDC->SelectObject(cpa);
}
else if(STYLEXP == m_iStyle)
{
if (m_State & ODS_DISABLED)
{
DrawBottonXP(pDC,m_rect,DISABLED);
}
else
if (m_State & ODS_FOCUS)
DrawBottonXP(pDC,m_rect,FOCUS);
else
DrawBottonXP(pDC,m_rect,NORMAL);
}
else
pDC->Draw3dRect(m_rect,m_clrface, m_clrface);
}
if ( (m_State & ODS_FOCUS) && (STYLEXP != m_iStyle))
{
m_rect.DeflateRect(2,2);
pDC->DrawFocusRect(m_rect);
}
pDC->SetBkMode(nMode);
}
void CGuiButton::AutoSizeButton(BOOL m_bAutoAjust)
{
m_ReposWindow=m_bAutoAjust;
Invalidate();
UpdateWindow();
}
void CGuiButton::OnSysColorChange( )
{
CButton::OnSysColorChange( );
m_clrface=GuiDrawLayer::GetRGBColorXP();
}
//*************************************************************************
void CGuiButton::DrawBottonXP(CDC* pDC,CRect rc,int Estado)
{
COLORREF clrPress = ::GetSysColor (COLOR_HIGHLIGHT);
COLORREF clrShadow = ::GetSysColor(COLOR_BTNSHADOW);
//*****************************************************
//dibujamos los bordes del boton
CPen cp(PS_SOLID,1,clrPress);
CPen cpface(PS_SOLID,1,m_clrface);
CPen cpsxp(PS_SOLID,1,GuiDrawLayer::GetRGBColorXP());
CPen cpOver(PS_SOLID,1,RGB(255,193,111));
CPen cpShadow(PS_SOLID,1,clrShadow);
CPen* pOld;
if (Estado != DISABLED)
pOld=pDC->SelectObject(&cp);
else
pOld=pDC->SelectObject(&cpShadow);
rc.right-=4;
rc.bottom-=2;
//la linea superior
pDC->MoveTo(rc.left+2,rc.top);
pDC->LineTo(rc.right-1,rc.top);
//linea izquierda
pDC->MoveTo(rc.left,rc.top+2);
pDC->LineTo(rc.left,rc.bottom-1);
//linea derecha
pDC->MoveTo(rc.right,rc.top+2);
pDC->LineTo(rc.right,rc.bottom-1);
//linea inferior
pDC->MoveTo(rc.left+2,rc.bottom);
pDC->LineTo(rc.right-1,rc.bottom);
if (Estado != DISABLED)
{
//se dibuja algunas lineas para dar forma 3d
//linea inferior
pDC->SelectObject(&cpface);
pDC->MoveTo(rc.left+2,rc.bottom-1);
pDC->LineTo(rc.right-1,rc.bottom-1);
pDC->MoveTo(rc.right-1,rc.top+2);
pDC->LineTo(rc.right-1,rc.bottom-1);
pDC->SelectObject(&cpsxp);
pDC->MoveTo(rc.left+2,rc.bottom-2);
pDC->LineTo(rc.right-1,rc.bottom-2);
pDC->MoveTo(rc.right-2,rc.top+2);
pDC->LineTo(rc.right-2,rc.bottom-1);
//fin bordes del boton
//***************************************************************
CRect rcBordes=rc;
rcBordes.left+=1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -